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