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 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 274 | ret = msm_allocate_iova_address(domain_num, partition_num, |
| 275 | data->mapped_size, align, |
| 276 | &data->iova_addr); |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 277 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 278 | if (ret) |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 279 | goto out; |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 280 | |
| 281 | domain = msm_get_iommu_domain(domain_num); |
| 282 | |
| 283 | if (!domain) { |
| 284 | ret = -ENOMEM; |
| 285 | goto out1; |
| 286 | } |
| 287 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 288 | ret = iommu_map_range(domain, data->iova_addr, table->sgl, |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 289 | buffer->size, prot); |
| 290 | |
| 291 | if (ret) { |
| 292 | pr_err("%s: could not map %lx in domain %p\n", |
| 293 | __func__, data->iova_addr, domain); |
| 294 | goto out1; |
| 295 | } |
| 296 | |
| 297 | extra_iova_addr = data->iova_addr + buffer->size; |
| 298 | if (extra) { |
Mitchel Humpherys | af3b522 | 2013-01-15 15:38:52 -0800 | [diff] [blame] | 299 | unsigned long phys_addr = sg_phys(table->sgl); |
| 300 | ret = msm_iommu_map_extra(domain, extra_iova_addr, phys_addr, |
| 301 | extra, SZ_4K, prot); |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 302 | if (ret) |
| 303 | goto out2; |
| 304 | } |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 305 | return ret; |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 306 | |
| 307 | out2: |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 308 | iommu_unmap_range(domain, data->iova_addr, buffer->size); |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 309 | out1: |
| 310 | msm_free_iova_address(data->iova_addr, domain_num, partition_num, |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 311 | data->mapped_size); |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 312 | out: |
| 313 | return ret; |
| 314 | } |
| 315 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 316 | static struct ion_heap_ops vmalloc_ops = { |
| 317 | .allocate = ion_system_heap_allocate, |
| 318 | .free = ion_system_heap_free, |
| 319 | .map_dma = ion_system_heap_map_dma, |
| 320 | .unmap_dma = ion_system_heap_unmap_dma, |
| 321 | .map_kernel = ion_system_heap_map_kernel, |
| 322 | .unmap_kernel = ion_system_heap_unmap_kernel, |
| 323 | .map_user = ion_system_heap_map_user, |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 324 | .cache_op = ion_system_heap_cache_ops, |
Olav Haugan | 3d4fe1a | 2012-01-13 11:42:15 -0800 | [diff] [blame] | 325 | .print_debug = ion_system_print_debug, |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 326 | .map_iommu = ion_system_heap_map_iommu, |
| 327 | .unmap_iommu = ion_system_heap_unmap_iommu, |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 328 | }; |
| 329 | |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 330 | 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] | 331 | { |
| 332 | struct ion_heap *heap; |
| 333 | |
| 334 | heap = kzalloc(sizeof(struct ion_heap), GFP_KERNEL); |
| 335 | if (!heap) |
| 336 | return ERR_PTR(-ENOMEM); |
| 337 | heap->ops = &vmalloc_ops; |
| 338 | heap->type = ION_HEAP_TYPE_SYSTEM; |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 339 | system_heap_has_outer_cache = pheap->has_outer_cache; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 340 | return heap; |
| 341 | } |
| 342 | |
| 343 | void ion_system_heap_destroy(struct ion_heap *heap) |
| 344 | { |
| 345 | kfree(heap); |
| 346 | } |
| 347 | |
| 348 | static int ion_system_contig_heap_allocate(struct ion_heap *heap, |
| 349 | struct ion_buffer *buffer, |
| 350 | unsigned long len, |
| 351 | unsigned long align, |
| 352 | unsigned long flags) |
| 353 | { |
| 354 | buffer->priv_virt = kzalloc(len, GFP_KERNEL); |
| 355 | if (!buffer->priv_virt) |
| 356 | return -ENOMEM; |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 357 | atomic_add(len, &system_contig_heap_allocated); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | void ion_system_contig_heap_free(struct ion_buffer *buffer) |
| 362 | { |
| 363 | kfree(buffer->priv_virt); |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 364 | atomic_sub(buffer->size, &system_contig_heap_allocated); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | static int ion_system_contig_heap_phys(struct ion_heap *heap, |
| 368 | struct ion_buffer *buffer, |
| 369 | ion_phys_addr_t *addr, size_t *len) |
| 370 | { |
| 371 | *addr = virt_to_phys(buffer->priv_virt); |
| 372 | *len = buffer->size; |
| 373 | return 0; |
| 374 | } |
| 375 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 376 | struct sg_table *ion_system_contig_heap_map_dma(struct ion_heap *heap, |
Rebecca Schultz Zavin | b179067 | 2012-06-14 15:08:53 -0700 | [diff] [blame] | 377 | struct ion_buffer *buffer) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 378 | { |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 379 | struct sg_table *table; |
| 380 | int ret; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 381 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 382 | table = kzalloc(sizeof(struct sg_table), GFP_KERNEL); |
| 383 | if (!table) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 384 | return ERR_PTR(-ENOMEM); |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 385 | ret = sg_alloc_table(table, 1, GFP_KERNEL); |
| 386 | if (ret) { |
| 387 | kfree(table); |
| 388 | return ERR_PTR(ret); |
| 389 | } |
| 390 | sg_set_page(table->sgl, virt_to_page(buffer->priv_virt), buffer->size, |
| 391 | 0); |
| 392 | return table; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Rebecca Schultz Zavin | b179067 | 2012-06-14 15:08:53 -0700 | [diff] [blame] | 395 | void ion_system_contig_heap_unmap_dma(struct ion_heap *heap, |
| 396 | struct ion_buffer *buffer) |
| 397 | { |
| 398 | sg_free_table(buffer->sg_table); |
| 399 | kfree(buffer->sg_table); |
| 400 | } |
| 401 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 402 | int ion_system_contig_heap_map_user(struct ion_heap *heap, |
| 403 | struct ion_buffer *buffer, |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 404 | struct vm_area_struct *vma) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 405 | { |
| 406 | unsigned long pfn = __phys_to_pfn(virt_to_phys(buffer->priv_virt)); |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 407 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 408 | if (ION_IS_CACHED(buffer->flags)) |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 409 | 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] | 410 | vma->vm_end - vma->vm_start, |
| 411 | vma->vm_page_prot); |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 412 | else { |
| 413 | pr_err("%s: cannot map system heap uncached\n", __func__); |
| 414 | return -EINVAL; |
| 415 | } |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 416 | } |
| 417 | |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 418 | int ion_system_contig_heap_cache_ops(struct ion_heap *heap, |
| 419 | struct ion_buffer *buffer, void *vaddr, |
| 420 | unsigned int offset, unsigned int length, |
| 421 | unsigned int cmd) |
| 422 | { |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 423 | void (*outer_cache_op)(phys_addr_t, phys_addr_t); |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 424 | |
| 425 | switch (cmd) { |
| 426 | case ION_IOC_CLEAN_CACHES: |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 427 | dmac_clean_range(vaddr, vaddr + length); |
| 428 | outer_cache_op = outer_clean_range; |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 429 | break; |
| 430 | case ION_IOC_INV_CACHES: |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 431 | dmac_inv_range(vaddr, vaddr + length); |
| 432 | outer_cache_op = outer_inv_range; |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 433 | break; |
| 434 | case ION_IOC_CLEAN_INV_CACHES: |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 435 | dmac_flush_range(vaddr, vaddr + length); |
| 436 | outer_cache_op = outer_flush_range; |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 437 | break; |
| 438 | default: |
| 439 | return -EINVAL; |
| 440 | } |
| 441 | |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 442 | if (system_heap_contig_has_outer_cache) { |
| 443 | unsigned long pstart; |
| 444 | |
| 445 | pstart = virt_to_phys(buffer->priv_virt) + offset; |
| 446 | if (!pstart) { |
| 447 | WARN(1, "Could not do virt to phys translation on %p\n", |
| 448 | buffer->priv_virt); |
| 449 | return -EINVAL; |
| 450 | } |
| 451 | |
| 452 | outer_cache_op(pstart, pstart + PAGE_SIZE); |
| 453 | } |
| 454 | |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 455 | return 0; |
| 456 | } |
| 457 | |
Olav Haugan | 3d4fe1a | 2012-01-13 11:42:15 -0800 | [diff] [blame] | 458 | static int ion_system_contig_print_debug(struct ion_heap *heap, |
Olav Haugan | 0671b9a | 2012-05-25 11:58:56 -0700 | [diff] [blame] | 459 | struct seq_file *s, |
| 460 | const struct rb_root *unused) |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 461 | { |
Olav Haugan | 3d4fe1a | 2012-01-13 11:42:15 -0800 | [diff] [blame] | 462 | seq_printf(s, "total bytes currently allocated: %lx\n", |
| 463 | (unsigned long) atomic_read(&system_contig_heap_allocated)); |
| 464 | |
| 465 | return 0; |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 466 | } |
| 467 | |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 468 | int ion_system_contig_heap_map_iommu(struct ion_buffer *buffer, |
| 469 | struct ion_iommu_map *data, |
| 470 | unsigned int domain_num, |
| 471 | unsigned int partition_num, |
| 472 | unsigned long align, |
| 473 | unsigned long iova_length, |
| 474 | unsigned long flags) |
| 475 | { |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 476 | int ret = 0; |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 477 | struct iommu_domain *domain; |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 478 | unsigned long extra; |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 479 | struct scatterlist *sglist = 0; |
| 480 | struct page *page = 0; |
Olav Haugan | f310cf2 | 2012-05-08 08:42:49 -0700 | [diff] [blame] | 481 | int prot = IOMMU_WRITE | IOMMU_READ; |
| 482 | prot |= ION_IS_CACHED(flags) ? IOMMU_CACHE : 0; |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 483 | |
| 484 | if (!ION_IS_CACHED(flags)) |
| 485 | return -EINVAL; |
| 486 | |
| 487 | if (!msm_use_iommu()) { |
| 488 | data->iova_addr = virt_to_phys(buffer->vaddr); |
| 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | data->mapped_size = iova_length; |
| 493 | extra = iova_length - buffer->size; |
| 494 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 495 | ret = msm_allocate_iova_address(domain_num, partition_num, |
| 496 | data->mapped_size, align, |
| 497 | &data->iova_addr); |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 498 | |
Laura Abbott | d01221b | 2012-05-16 17:52:49 -0700 | [diff] [blame] | 499 | if (ret) |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 500 | goto out; |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 501 | |
| 502 | domain = msm_get_iommu_domain(domain_num); |
| 503 | |
| 504 | if (!domain) { |
| 505 | ret = -ENOMEM; |
| 506 | goto out1; |
| 507 | } |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 508 | page = virt_to_page(buffer->vaddr); |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 509 | |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 510 | sglist = vmalloc(sizeof(*sglist)); |
| 511 | if (!sglist) |
| 512 | goto out1; |
| 513 | |
| 514 | sg_init_table(sglist, 1); |
| 515 | sg_set_page(sglist, page, buffer->size, 0); |
| 516 | |
| 517 | ret = iommu_map_range(domain, data->iova_addr, sglist, |
| 518 | buffer->size, prot); |
| 519 | if (ret) { |
| 520 | pr_err("%s: could not map %lx in domain %p\n", |
| 521 | __func__, data->iova_addr, domain); |
| 522 | goto out1; |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 523 | } |
| 524 | |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 525 | if (extra) { |
| 526 | unsigned long extra_iova_addr = data->iova_addr + buffer->size; |
Mitchel Humpherys | af3b522 | 2013-01-15 15:38:52 -0800 | [diff] [blame] | 527 | unsigned long phys_addr = sg_phys(sglist); |
| 528 | ret = msm_iommu_map_extra(domain, extra_iova_addr, phys_addr, |
| 529 | extra, SZ_4K, prot); |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 530 | if (ret) |
| 531 | goto out2; |
| 532 | } |
| 533 | vfree(sglist); |
| 534 | return ret; |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 535 | out2: |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 536 | iommu_unmap_range(domain, data->iova_addr, buffer->size); |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 537 | |
| 538 | out1: |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 539 | vfree(sglist); |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 540 | msm_free_iova_address(data->iova_addr, domain_num, partition_num, |
| 541 | data->mapped_size); |
| 542 | out: |
| 543 | return ret; |
| 544 | } |
| 545 | |
Rohit Vaswani | 35edc88 | 2012-11-20 10:20:47 -0800 | [diff] [blame] | 546 | void *ion_system_contig_heap_map_kernel(struct ion_heap *heap, |
| 547 | struct ion_buffer *buffer) |
| 548 | { |
| 549 | return buffer->priv_virt; |
| 550 | } |
| 551 | |
| 552 | void ion_system_contig_heap_unmap_kernel(struct ion_heap *heap, |
| 553 | struct ion_buffer *buffer) |
| 554 | { |
| 555 | return; |
| 556 | } |
| 557 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 558 | static struct ion_heap_ops kmalloc_ops = { |
| 559 | .allocate = ion_system_contig_heap_allocate, |
| 560 | .free = ion_system_contig_heap_free, |
| 561 | .phys = ion_system_contig_heap_phys, |
| 562 | .map_dma = ion_system_contig_heap_map_dma, |
Rebecca Schultz Zavin | b179067 | 2012-06-14 15:08:53 -0700 | [diff] [blame] | 563 | .unmap_dma = ion_system_contig_heap_unmap_dma, |
Rohit Vaswani | 35edc88 | 2012-11-20 10:20:47 -0800 | [diff] [blame] | 564 | .map_kernel = ion_system_contig_heap_map_kernel, |
| 565 | .unmap_kernel = ion_system_contig_heap_unmap_kernel, |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 566 | .map_user = ion_system_contig_heap_map_user, |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 567 | .cache_op = ion_system_contig_heap_cache_ops, |
Olav Haugan | 3d4fe1a | 2012-01-13 11:42:15 -0800 | [diff] [blame] | 568 | .print_debug = ion_system_contig_print_debug, |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 569 | .map_iommu = ion_system_contig_heap_map_iommu, |
| 570 | .unmap_iommu = ion_system_heap_unmap_iommu, |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 571 | }; |
| 572 | |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 573 | 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] | 574 | { |
| 575 | struct ion_heap *heap; |
| 576 | |
| 577 | heap = kzalloc(sizeof(struct ion_heap), GFP_KERNEL); |
| 578 | if (!heap) |
| 579 | return ERR_PTR(-ENOMEM); |
| 580 | heap->ops = &kmalloc_ops; |
| 581 | heap->type = ION_HEAP_TYPE_SYSTEM_CONTIG; |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 582 | system_heap_contig_has_outer_cache = pheap->has_outer_cache; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 583 | return heap; |
| 584 | } |
| 585 | |
| 586 | void ion_system_contig_heap_destroy(struct ion_heap *heap) |
| 587 | { |
| 588 | kfree(heap); |
| 589 | } |
| 590 | |