Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/gpu/ion/ion_system_heap.c |
| 3 | * |
| 4 | * Copyright (C) 2011 Google, Inc. |
Mitchel Humpherys | af3b522 | 2013-01-15 15:38:52 -0800 | [diff] [blame] | 5 | * Copyright (c) 2011-2013, The Linux Foundation. All rights reserved. |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 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/err.h> |
| 19 | #include <linux/ion.h> |
| 20 | #include <linux/mm.h> |
| 21 | #include <linux/scatterlist.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/vmalloc.h> |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 24 | #include <linux/iommu.h> |
Olav Haugan | 3d4fe1a | 2012-01-13 11:42:15 -0800 | [diff] [blame] | 25 | #include <linux/seq_file.h> |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 26 | #include <mach/iommu_domains.h> |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 27 | #include "ion_priv.h" |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 28 | #include <mach/memory.h> |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 29 | #include <asm/cacheflush.h> |
Mitchel Humpherys | af2e5c5 | 2012-09-06 12:16:36 -0700 | [diff] [blame] | 30 | #include <linux/msm_ion.h> |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 31 | |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 32 | static atomic_t system_heap_allocated; |
| 33 | static atomic_t system_contig_heap_allocated; |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 34 | static unsigned int system_heap_has_outer_cache; |
| 35 | static unsigned int system_heap_contig_has_outer_cache; |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 36 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 37 | static int ion_system_heap_allocate(struct ion_heap *heap, |
| 38 | struct ion_buffer *buffer, |
| 39 | unsigned long size, unsigned long align, |
| 40 | unsigned long flags) |
| 41 | { |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 42 | struct sg_table *table; |
| 43 | struct scatterlist *sg; |
| 44 | int i, j; |
| 45 | int npages = PAGE_ALIGN(size) / PAGE_SIZE; |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 46 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 47 | table = kmalloc(sizeof(struct sg_table), GFP_KERNEL); |
| 48 | if (!table) |
| 49 | return -ENOMEM; |
| 50 | i = sg_alloc_table(table, npages, GFP_KERNEL); |
| 51 | if (i) |
| 52 | goto err0; |
| 53 | for_each_sg(table->sgl, sg, table->nents, i) { |
| 54 | struct page *page; |
| 55 | page = alloc_page(GFP_KERNEL|__GFP_ZERO); |
| 56 | if (!page) |
| 57 | goto err1; |
| 58 | sg_set_page(sg, page, PAGE_SIZE, 0); |
| 59 | } |
| 60 | buffer->priv_virt = table; |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 61 | atomic_add(size, &system_heap_allocated); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 62 | return 0; |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 63 | err1: |
| 64 | for_each_sg(table->sgl, sg, i, j) |
| 65 | __free_page(sg_page(sg)); |
| 66 | sg_free_table(table); |
| 67 | err0: |
| 68 | kfree(table); |
| 69 | return -ENOMEM; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void ion_system_heap_free(struct ion_buffer *buffer) |
| 73 | { |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 74 | int i; |
| 75 | struct scatterlist *sg; |
| 76 | struct sg_table *table = buffer->priv_virt; |
| 77 | |
| 78 | for_each_sg(table->sgl, sg, table->nents, i) |
| 79 | __free_page(sg_page(sg)); |
| 80 | if (buffer->sg_table) |
| 81 | sg_free_table(buffer->sg_table); |
| 82 | kfree(buffer->sg_table); |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 83 | atomic_sub(buffer->size, &system_heap_allocated); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 86 | struct sg_table *ion_system_heap_map_dma(struct ion_heap *heap, |
| 87 | struct ion_buffer *buffer) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 88 | { |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 89 | return buffer->priv_virt; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | void ion_system_heap_unmap_dma(struct ion_heap *heap, |
| 93 | struct ion_buffer *buffer) |
| 94 | { |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 95 | return; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void *ion_system_heap_map_kernel(struct ion_heap *heap, |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 99 | struct ion_buffer *buffer) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 100 | { |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 101 | if (!ION_IS_CACHED(buffer->flags)) { |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 102 | pr_err("%s: cannot map system heap uncached\n", __func__); |
| 103 | return ERR_PTR(-EINVAL); |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 104 | } else { |
| 105 | struct scatterlist *sg; |
| 106 | int i; |
| 107 | void *vaddr; |
| 108 | struct sg_table *table = buffer->priv_virt; |
| 109 | struct page **pages = kmalloc( |
| 110 | sizeof(struct page *) * table->nents, |
| 111 | GFP_KERNEL); |
| 112 | |
| 113 | for_each_sg(table->sgl, sg, table->nents, i) |
| 114 | pages[i] = sg_page(sg); |
| 115 | vaddr = vmap(pages, table->nents, VM_MAP, PAGE_KERNEL); |
| 116 | kfree(pages); |
| 117 | |
| 118 | return vaddr; |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 119 | } |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | void ion_system_heap_unmap_kernel(struct ion_heap *heap, |
| 123 | struct ion_buffer *buffer) |
| 124 | { |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 125 | vunmap(buffer->vaddr); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 128 | void ion_system_heap_unmap_iommu(struct ion_iommu_map *data) |
| 129 | { |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 130 | unsigned int domain_num; |
| 131 | unsigned int partition_num; |
| 132 | struct iommu_domain *domain; |
| 133 | |
| 134 | if (!msm_use_iommu()) |
| 135 | return; |
| 136 | |
| 137 | domain_num = iommu_map_domain(data); |
| 138 | partition_num = iommu_map_partition(data); |
| 139 | |
| 140 | domain = msm_get_iommu_domain(domain_num); |
| 141 | |
| 142 | if (!domain) { |
| 143 | WARN(1, "Could not get domain %d. Corruption?\n", domain_num); |
| 144 | return; |
| 145 | } |
| 146 | |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 147 | iommu_unmap_range(domain, data->iova_addr, data->mapped_size); |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 148 | msm_free_iova_address(data->iova_addr, domain_num, partition_num, |
| 149 | data->mapped_size); |
| 150 | |
| 151 | return; |
| 152 | } |
| 153 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 154 | int ion_system_heap_map_user(struct ion_heap *heap, struct ion_buffer *buffer, |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 155 | struct vm_area_struct *vma) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 156 | { |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 157 | if (!ION_IS_CACHED(buffer->flags)) { |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 158 | pr_err("%s: cannot map system heap uncached\n", __func__); |
| 159 | return -EINVAL; |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 160 | } else { |
| 161 | struct sg_table *table = buffer->priv_virt; |
| 162 | unsigned long addr = vma->vm_start; |
| 163 | unsigned long offset = vma->vm_pgoff; |
| 164 | struct scatterlist *sg; |
| 165 | int i; |
| 166 | |
| 167 | for_each_sg(table->sgl, sg, table->nents, i) { |
| 168 | if (offset) { |
| 169 | offset--; |
| 170 | continue; |
| 171 | } |
| 172 | vm_insert_page(vma, addr, sg_page(sg)); |
| 173 | addr += PAGE_SIZE; |
| 174 | } |
| 175 | return 0; |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 176 | } |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 179 | int ion_system_heap_cache_ops(struct ion_heap *heap, struct ion_buffer *buffer, |
| 180 | void *vaddr, unsigned int offset, unsigned int length, |
| 181 | unsigned int cmd) |
| 182 | { |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 183 | void (*outer_cache_op)(phys_addr_t, phys_addr_t); |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 184 | |
| 185 | switch (cmd) { |
| 186 | case ION_IOC_CLEAN_CACHES: |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 187 | dmac_clean_range(vaddr, vaddr + length); |
| 188 | outer_cache_op = outer_clean_range; |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 189 | break; |
| 190 | case ION_IOC_INV_CACHES: |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 191 | dmac_inv_range(vaddr, vaddr + length); |
| 192 | outer_cache_op = outer_inv_range; |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 193 | break; |
| 194 | case ION_IOC_CLEAN_INV_CACHES: |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 195 | dmac_flush_range(vaddr, vaddr + length); |
| 196 | outer_cache_op = outer_flush_range; |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 197 | break; |
| 198 | default: |
| 199 | return -EINVAL; |
| 200 | } |
| 201 | |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 202 | if (system_heap_has_outer_cache) { |
| 203 | unsigned long pstart; |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 204 | struct sg_table *table = buffer->priv_virt; |
| 205 | struct scatterlist *sg; |
| 206 | int i; |
| 207 | for_each_sg(table->sgl, sg, table->nents, i) { |
| 208 | struct page *page = sg_page(sg); |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 209 | pstart = page_to_phys(page); |
| 210 | /* |
| 211 | * If page -> phys is returning NULL, something |
| 212 | * has really gone wrong... |
| 213 | */ |
| 214 | if (!pstart) { |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 215 | WARN(1, "Could not translate virtual address to physical address\n"); |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 216 | return -EINVAL; |
| 217 | } |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 218 | outer_cache_op(pstart, pstart + PAGE_SIZE); |
| 219 | } |
| 220 | } |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 221 | return 0; |
| 222 | } |
| 223 | |
Olav Haugan | 0671b9a | 2012-05-25 11:58:56 -0700 | [diff] [blame] | 224 | static int ion_system_print_debug(struct ion_heap *heap, struct seq_file *s, |
| 225 | const struct rb_root *unused) |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 226 | { |
Olav Haugan | 3d4fe1a | 2012-01-13 11:42:15 -0800 | [diff] [blame] | 227 | seq_printf(s, "total bytes currently allocated: %lx\n", |
| 228 | (unsigned long) atomic_read(&system_heap_allocated)); |
| 229 | |
| 230 | return 0; |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 233 | int ion_system_heap_map_iommu(struct ion_buffer *buffer, |
| 234 | struct ion_iommu_map *data, |
| 235 | unsigned int domain_num, |
| 236 | unsigned int partition_num, |
| 237 | unsigned long align, |
| 238 | unsigned long iova_length, |
| 239 | unsigned long flags) |
| 240 | { |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 241 | int ret = 0; |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 242 | struct iommu_domain *domain; |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 243 | unsigned long extra; |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 244 | unsigned long extra_iova_addr; |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 245 | struct sg_table *table = buffer->priv_virt; |
Olav Haugan | f310cf2 | 2012-05-08 08:42:49 -0700 | [diff] [blame] | 246 | int prot = IOMMU_WRITE | IOMMU_READ; |
| 247 | prot |= ION_IS_CACHED(flags) ? IOMMU_CACHE : 0; |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 248 | |
| 249 | if (!ION_IS_CACHED(flags)) |
| 250 | return -EINVAL; |
| 251 | |
| 252 | if (!msm_use_iommu()) |
| 253 | return -EINVAL; |
| 254 | |
| 255 | data->mapped_size = iova_length; |
| 256 | extra = iova_length - buffer->size; |
| 257 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 258 | ret = msm_allocate_iova_address(domain_num, partition_num, |
| 259 | data->mapped_size, align, |
| 260 | &data->iova_addr); |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 261 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 262 | if (ret) |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 263 | goto out; |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 264 | |
| 265 | domain = msm_get_iommu_domain(domain_num); |
| 266 | |
| 267 | if (!domain) { |
| 268 | ret = -ENOMEM; |
| 269 | goto out1; |
| 270 | } |
| 271 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 272 | ret = iommu_map_range(domain, data->iova_addr, table->sgl, |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 273 | buffer->size, prot); |
| 274 | |
| 275 | if (ret) { |
| 276 | pr_err("%s: could not map %lx in domain %p\n", |
| 277 | __func__, data->iova_addr, domain); |
| 278 | goto out1; |
| 279 | } |
| 280 | |
| 281 | extra_iova_addr = data->iova_addr + buffer->size; |
| 282 | if (extra) { |
Mitchel Humpherys | af3b522 | 2013-01-15 15:38:52 -0800 | [diff] [blame] | 283 | unsigned long phys_addr = sg_phys(table->sgl); |
| 284 | ret = msm_iommu_map_extra(domain, extra_iova_addr, phys_addr, |
| 285 | extra, SZ_4K, prot); |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 286 | if (ret) |
| 287 | goto out2; |
| 288 | } |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 289 | return ret; |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 290 | |
| 291 | out2: |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 292 | iommu_unmap_range(domain, data->iova_addr, buffer->size); |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 293 | out1: |
| 294 | msm_free_iova_address(data->iova_addr, domain_num, partition_num, |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 295 | data->mapped_size); |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 296 | out: |
| 297 | return ret; |
| 298 | } |
| 299 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 300 | static struct ion_heap_ops vmalloc_ops = { |
| 301 | .allocate = ion_system_heap_allocate, |
| 302 | .free = ion_system_heap_free, |
| 303 | .map_dma = ion_system_heap_map_dma, |
| 304 | .unmap_dma = ion_system_heap_unmap_dma, |
| 305 | .map_kernel = ion_system_heap_map_kernel, |
| 306 | .unmap_kernel = ion_system_heap_unmap_kernel, |
| 307 | .map_user = ion_system_heap_map_user, |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 308 | .cache_op = ion_system_heap_cache_ops, |
Olav Haugan | 3d4fe1a | 2012-01-13 11:42:15 -0800 | [diff] [blame] | 309 | .print_debug = ion_system_print_debug, |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 310 | .map_iommu = ion_system_heap_map_iommu, |
| 311 | .unmap_iommu = ion_system_heap_unmap_iommu, |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 312 | }; |
| 313 | |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 314 | struct ion_heap *ion_system_heap_create(struct ion_platform_heap *pheap) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 315 | { |
| 316 | struct ion_heap *heap; |
| 317 | |
| 318 | heap = kzalloc(sizeof(struct ion_heap), GFP_KERNEL); |
| 319 | if (!heap) |
| 320 | return ERR_PTR(-ENOMEM); |
| 321 | heap->ops = &vmalloc_ops; |
| 322 | heap->type = ION_HEAP_TYPE_SYSTEM; |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 323 | system_heap_has_outer_cache = pheap->has_outer_cache; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 324 | return heap; |
| 325 | } |
| 326 | |
| 327 | void ion_system_heap_destroy(struct ion_heap *heap) |
| 328 | { |
| 329 | kfree(heap); |
| 330 | } |
| 331 | |
| 332 | static int ion_system_contig_heap_allocate(struct ion_heap *heap, |
| 333 | struct ion_buffer *buffer, |
| 334 | unsigned long len, |
| 335 | unsigned long align, |
| 336 | unsigned long flags) |
| 337 | { |
| 338 | buffer->priv_virt = kzalloc(len, GFP_KERNEL); |
| 339 | if (!buffer->priv_virt) |
| 340 | return -ENOMEM; |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 341 | atomic_add(len, &system_contig_heap_allocated); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | void ion_system_contig_heap_free(struct ion_buffer *buffer) |
| 346 | { |
| 347 | kfree(buffer->priv_virt); |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 348 | atomic_sub(buffer->size, &system_contig_heap_allocated); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | static int ion_system_contig_heap_phys(struct ion_heap *heap, |
| 352 | struct ion_buffer *buffer, |
| 353 | ion_phys_addr_t *addr, size_t *len) |
| 354 | { |
| 355 | *addr = virt_to_phys(buffer->priv_virt); |
| 356 | *len = buffer->size; |
| 357 | return 0; |
| 358 | } |
| 359 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 360 | struct sg_table *ion_system_contig_heap_map_dma(struct ion_heap *heap, |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 361 | struct ion_buffer *buffer) |
| 362 | { |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 363 | struct sg_table *table; |
| 364 | int ret; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 365 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 366 | table = kzalloc(sizeof(struct sg_table), GFP_KERNEL); |
| 367 | if (!table) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 368 | return ERR_PTR(-ENOMEM); |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 369 | ret = sg_alloc_table(table, 1, GFP_KERNEL); |
| 370 | if (ret) { |
| 371 | kfree(table); |
| 372 | return ERR_PTR(ret); |
| 373 | } |
| 374 | sg_set_page(table->sgl, virt_to_page(buffer->priv_virt), buffer->size, |
| 375 | 0); |
| 376 | return table; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | int ion_system_contig_heap_map_user(struct ion_heap *heap, |
| 380 | struct ion_buffer *buffer, |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 381 | struct vm_area_struct *vma) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 382 | { |
| 383 | unsigned long pfn = __phys_to_pfn(virt_to_phys(buffer->priv_virt)); |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 384 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 385 | if (ION_IS_CACHED(buffer->flags)) |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 386 | return remap_pfn_range(vma, vma->vm_start, pfn + vma->vm_pgoff, |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 387 | vma->vm_end - vma->vm_start, |
| 388 | vma->vm_page_prot); |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 389 | else { |
| 390 | pr_err("%s: cannot map system heap uncached\n", __func__); |
| 391 | return -EINVAL; |
| 392 | } |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 395 | int ion_system_contig_heap_cache_ops(struct ion_heap *heap, |
| 396 | struct ion_buffer *buffer, void *vaddr, |
| 397 | unsigned int offset, unsigned int length, |
| 398 | unsigned int cmd) |
| 399 | { |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 400 | void (*outer_cache_op)(phys_addr_t, phys_addr_t); |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 401 | |
| 402 | switch (cmd) { |
| 403 | case ION_IOC_CLEAN_CACHES: |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 404 | dmac_clean_range(vaddr, vaddr + length); |
| 405 | outer_cache_op = outer_clean_range; |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 406 | break; |
| 407 | case ION_IOC_INV_CACHES: |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 408 | dmac_inv_range(vaddr, vaddr + length); |
| 409 | outer_cache_op = outer_inv_range; |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 410 | break; |
| 411 | case ION_IOC_CLEAN_INV_CACHES: |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 412 | dmac_flush_range(vaddr, vaddr + length); |
| 413 | outer_cache_op = outer_flush_range; |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 414 | break; |
| 415 | default: |
| 416 | return -EINVAL; |
| 417 | } |
| 418 | |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 419 | if (system_heap_contig_has_outer_cache) { |
| 420 | unsigned long pstart; |
| 421 | |
| 422 | pstart = virt_to_phys(buffer->priv_virt) + offset; |
| 423 | if (!pstart) { |
| 424 | WARN(1, "Could not do virt to phys translation on %p\n", |
| 425 | buffer->priv_virt); |
| 426 | return -EINVAL; |
| 427 | } |
| 428 | |
| 429 | outer_cache_op(pstart, pstart + PAGE_SIZE); |
| 430 | } |
| 431 | |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 432 | return 0; |
| 433 | } |
| 434 | |
Olav Haugan | 3d4fe1a | 2012-01-13 11:42:15 -0800 | [diff] [blame] | 435 | static int ion_system_contig_print_debug(struct ion_heap *heap, |
Olav Haugan | 0671b9a | 2012-05-25 11:58:56 -0700 | [diff] [blame] | 436 | struct seq_file *s, |
| 437 | const struct rb_root *unused) |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 438 | { |
Olav Haugan | 3d4fe1a | 2012-01-13 11:42:15 -0800 | [diff] [blame] | 439 | seq_printf(s, "total bytes currently allocated: %lx\n", |
| 440 | (unsigned long) atomic_read(&system_contig_heap_allocated)); |
| 441 | |
| 442 | return 0; |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 443 | } |
| 444 | |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 445 | int ion_system_contig_heap_map_iommu(struct ion_buffer *buffer, |
| 446 | struct ion_iommu_map *data, |
| 447 | unsigned int domain_num, |
| 448 | unsigned int partition_num, |
| 449 | unsigned long align, |
| 450 | unsigned long iova_length, |
| 451 | unsigned long flags) |
| 452 | { |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 453 | int ret = 0; |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 454 | struct iommu_domain *domain; |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 455 | unsigned long extra; |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 456 | struct scatterlist *sglist = 0; |
| 457 | struct page *page = 0; |
Olav Haugan | f310cf2 | 2012-05-08 08:42:49 -0700 | [diff] [blame] | 458 | int prot = IOMMU_WRITE | IOMMU_READ; |
| 459 | prot |= ION_IS_CACHED(flags) ? IOMMU_CACHE : 0; |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 460 | |
| 461 | if (!ION_IS_CACHED(flags)) |
| 462 | return -EINVAL; |
| 463 | |
| 464 | if (!msm_use_iommu()) { |
| 465 | data->iova_addr = virt_to_phys(buffer->vaddr); |
| 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | data->mapped_size = iova_length; |
| 470 | extra = iova_length - buffer->size; |
| 471 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 472 | ret = msm_allocate_iova_address(domain_num, partition_num, |
| 473 | data->mapped_size, align, |
| 474 | &data->iova_addr); |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 475 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 476 | if (ret) |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 477 | goto out; |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 478 | |
| 479 | domain = msm_get_iommu_domain(domain_num); |
| 480 | |
| 481 | if (!domain) { |
| 482 | ret = -ENOMEM; |
| 483 | goto out1; |
| 484 | } |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 485 | page = virt_to_page(buffer->vaddr); |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 486 | |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 487 | sglist = vmalloc(sizeof(*sglist)); |
| 488 | if (!sglist) |
| 489 | goto out1; |
| 490 | |
| 491 | sg_init_table(sglist, 1); |
| 492 | sg_set_page(sglist, page, buffer->size, 0); |
| 493 | |
| 494 | ret = iommu_map_range(domain, data->iova_addr, sglist, |
| 495 | buffer->size, prot); |
| 496 | if (ret) { |
| 497 | pr_err("%s: could not map %lx in domain %p\n", |
| 498 | __func__, data->iova_addr, domain); |
| 499 | goto out1; |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 500 | } |
| 501 | |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 502 | if (extra) { |
| 503 | unsigned long extra_iova_addr = data->iova_addr + buffer->size; |
Mitchel Humpherys | af3b522 | 2013-01-15 15:38:52 -0800 | [diff] [blame] | 504 | unsigned long phys_addr = sg_phys(sglist); |
| 505 | ret = msm_iommu_map_extra(domain, extra_iova_addr, phys_addr, |
| 506 | extra, SZ_4K, prot); |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 507 | if (ret) |
| 508 | goto out2; |
| 509 | } |
| 510 | vfree(sglist); |
| 511 | return ret; |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 512 | out2: |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 513 | iommu_unmap_range(domain, data->iova_addr, buffer->size); |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 514 | |
| 515 | out1: |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 516 | vfree(sglist); |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 517 | msm_free_iova_address(data->iova_addr, domain_num, partition_num, |
| 518 | data->mapped_size); |
| 519 | out: |
| 520 | return ret; |
| 521 | } |
| 522 | |
Rohit Vaswani | 35edc88 | 2012-11-20 10:20:47 -0800 | [diff] [blame] | 523 | void *ion_system_contig_heap_map_kernel(struct ion_heap *heap, |
| 524 | struct ion_buffer *buffer) |
| 525 | { |
| 526 | return buffer->priv_virt; |
| 527 | } |
| 528 | |
| 529 | void ion_system_contig_heap_unmap_kernel(struct ion_heap *heap, |
| 530 | struct ion_buffer *buffer) |
| 531 | { |
| 532 | return; |
| 533 | } |
| 534 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 535 | static struct ion_heap_ops kmalloc_ops = { |
| 536 | .allocate = ion_system_contig_heap_allocate, |
| 537 | .free = ion_system_contig_heap_free, |
| 538 | .phys = ion_system_contig_heap_phys, |
| 539 | .map_dma = ion_system_contig_heap_map_dma, |
| 540 | .unmap_dma = ion_system_heap_unmap_dma, |
Rohit Vaswani | 35edc88 | 2012-11-20 10:20:47 -0800 | [diff] [blame] | 541 | .map_kernel = ion_system_contig_heap_map_kernel, |
| 542 | .unmap_kernel = ion_system_contig_heap_unmap_kernel, |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 543 | .map_user = ion_system_contig_heap_map_user, |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 544 | .cache_op = ion_system_contig_heap_cache_ops, |
Olav Haugan | 3d4fe1a | 2012-01-13 11:42:15 -0800 | [diff] [blame] | 545 | .print_debug = ion_system_contig_print_debug, |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 546 | .map_iommu = ion_system_contig_heap_map_iommu, |
| 547 | .unmap_iommu = ion_system_heap_unmap_iommu, |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 548 | }; |
| 549 | |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 550 | struct ion_heap *ion_system_contig_heap_create(struct ion_platform_heap *pheap) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 551 | { |
| 552 | struct ion_heap *heap; |
| 553 | |
| 554 | heap = kzalloc(sizeof(struct ion_heap), GFP_KERNEL); |
| 555 | if (!heap) |
| 556 | return ERR_PTR(-ENOMEM); |
| 557 | heap->ops = &kmalloc_ops; |
| 558 | heap->type = ION_HEAP_TYPE_SYSTEM_CONTIG; |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 559 | system_heap_contig_has_outer_cache = pheap->has_outer_cache; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 560 | return heap; |
| 561 | } |
| 562 | |
| 563 | void ion_system_contig_heap_destroy(struct ion_heap *heap) |
| 564 | { |
| 565 | kfree(heap); |
| 566 | } |
| 567 | |