Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * drivers/gpu/ion/ion_cp_heap.c |
| 3 | * |
| 4 | * Copyright (C) 2011 Google, Inc. |
Laura Abbott | 7db4e0b | 2013-01-03 14:20:16 -0800 | [diff] [blame] | 5 | * Copyright (c) 2011-2013, The Linux Foundation. All rights reserved. |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [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 | #include <linux/spinlock.h> |
Laura Abbott | 3180a5f | 2012-08-03 17:31:03 -0700 | [diff] [blame] | 18 | #include <linux/delay.h> |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 19 | #include <linux/err.h> |
| 20 | #include <linux/genalloc.h> |
| 21 | #include <linux/io.h> |
Mitchel Humpherys | af2e5c5 | 2012-09-06 12:16:36 -0700 | [diff] [blame] | 22 | #include <linux/msm_ion.h> |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 23 | #include <linux/mm.h> |
| 24 | #include <linux/scatterlist.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/vmalloc.h> |
| 27 | #include <linux/memory_alloc.h> |
Olav Haugan | 3d4fe1a | 2012-01-13 11:42:15 -0800 | [diff] [blame] | 28 | #include <linux/seq_file.h> |
Olav Haugan | 1c94f7b | 2012-02-08 09:45:53 -0800 | [diff] [blame] | 29 | #include <linux/iommu.h> |
Laura Abbott | 3180a5f | 2012-08-03 17:31:03 -0700 | [diff] [blame] | 30 | #include <linux/dma-mapping.h> |
Liam Mark | cc2d4bd | 2013-01-16 10:14:40 -0800 | [diff] [blame] | 31 | #include <trace/events/kmem.h> |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 32 | |
| 33 | #include <asm/mach/map.h> |
| 34 | |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 35 | #include <mach/msm_memtypes.h> |
| 36 | #include <mach/scm.h> |
Olav Haugan | 1c94f7b | 2012-02-08 09:45:53 -0800 | [diff] [blame] | 37 | #include <mach/iommu_domains.h> |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 38 | |
| 39 | #include "ion_priv.h" |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 40 | |
| 41 | #include <asm/mach/map.h> |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 42 | #include <asm/cacheflush.h> |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 43 | |
Laura Abbott | 7e44648 | 2012-06-13 15:59:39 -0700 | [diff] [blame] | 44 | #include "msm/ion_cp_common.h" |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 45 | /** |
| 46 | * struct ion_cp_heap - container for the heap and shared heap data |
| 47 | |
| 48 | * @heap: the heap information structure |
| 49 | * @pool: memory pool to allocate from. |
| 50 | * @base: the base address of the memory pool. |
| 51 | * @permission_type: Identifier for the memory used by SCM for protecting |
| 52 | * and unprotecting memory. |
Olav Haugan | 42ebe71 | 2012-01-10 16:30:58 -0800 | [diff] [blame] | 53 | * @secure_base: Base address used when securing a heap that is shared. |
| 54 | * @secure_size: Size used when securing a heap that is shared. |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 55 | * @lock: mutex to protect shared access. |
Olav Haugan | ea66e7a | 2012-01-23 17:30:27 -0800 | [diff] [blame] | 56 | * @heap_protected: Indicates whether heap has been protected or not. |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 57 | * @allocated_bytes: the total number of allocated bytes from the pool. |
| 58 | * @total_size: the total size of the memory pool. |
| 59 | * @request_region: function pointer to call when first mapping of memory |
| 60 | * occurs. |
| 61 | * @release_region: function pointer to call when last mapping of memory |
| 62 | * unmapped. |
| 63 | * @bus_id: token used with request/release region. |
Olav Haugan | 2a5404b | 2012-02-01 17:51:30 -0800 | [diff] [blame] | 64 | * @kmap_cached_count: the total number of times this heap has been mapped in |
| 65 | * kernel space (cached). |
| 66 | * @kmap_uncached_count:the total number of times this heap has been mapped in |
| 67 | * kernel space (un-cached). |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 68 | * @umap_count: the total number of times this heap has been mapped in |
| 69 | * user space. |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 70 | * @has_outer_cache: set to 1 if outer cache is used, 0 otherwise. |
| 71 | */ |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 72 | struct ion_cp_heap { |
| 73 | struct ion_heap heap; |
| 74 | struct gen_pool *pool; |
| 75 | ion_phys_addr_t base; |
| 76 | unsigned int permission_type; |
Olav Haugan | 42ebe71 | 2012-01-10 16:30:58 -0800 | [diff] [blame] | 77 | ion_phys_addr_t secure_base; |
| 78 | size_t secure_size; |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 79 | struct mutex lock; |
Olav Haugan | ea66e7a | 2012-01-23 17:30:27 -0800 | [diff] [blame] | 80 | unsigned int heap_protected; |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 81 | unsigned long allocated_bytes; |
| 82 | unsigned long total_size; |
Laura Abbott | aedbe42 | 2012-08-03 17:06:22 -0700 | [diff] [blame] | 83 | int (*heap_request_region)(void *); |
| 84 | int (*heap_release_region)(void *); |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 85 | void *bus_id; |
Olav Haugan | 2a5404b | 2012-02-01 17:51:30 -0800 | [diff] [blame] | 86 | unsigned long kmap_cached_count; |
| 87 | unsigned long kmap_uncached_count; |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 88 | unsigned long umap_count; |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 89 | unsigned int has_outer_cache; |
Laura Abbott | f68983e | 2012-06-13 16:23:23 -0700 | [diff] [blame] | 90 | atomic_t protect_cnt; |
Laura Abbott | 3180a5f | 2012-08-03 17:31:03 -0700 | [diff] [blame] | 91 | void *cpu_addr; |
| 92 | size_t heap_size; |
| 93 | dma_addr_t handle; |
| 94 | int cma; |
Mitchel Humpherys | 345f023 | 2013-01-11 10:55:25 -0800 | [diff] [blame] | 95 | int allow_non_secure_allocation; |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | enum { |
Olav Haugan | ea66e7a | 2012-01-23 17:30:27 -0800 | [diff] [blame] | 99 | HEAP_NOT_PROTECTED = 0, |
| 100 | HEAP_PROTECTED = 1, |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 101 | }; |
| 102 | |
Laura Abbott | 3180a5f | 2012-08-03 17:31:03 -0700 | [diff] [blame] | 103 | #define DMA_ALLOC_TRIES 5 |
| 104 | |
Laura Abbott | 3180a5f | 2012-08-03 17:31:03 -0700 | [diff] [blame] | 105 | static int allocate_heap_memory(struct ion_heap *heap) |
| 106 | { |
| 107 | struct device *dev = heap->priv; |
| 108 | struct ion_cp_heap *cp_heap = |
| 109 | container_of(heap, struct ion_cp_heap, heap); |
| 110 | int ret; |
| 111 | int tries = 0; |
| 112 | DEFINE_DMA_ATTRS(attrs); |
| 113 | dma_set_attr(DMA_ATTR_NO_KERNEL_MAPPING, &attrs); |
| 114 | |
| 115 | |
| 116 | if (cp_heap->cpu_addr) |
| 117 | return 0; |
| 118 | |
| 119 | while (!cp_heap->cpu_addr && (++tries < DMA_ALLOC_TRIES)) { |
| 120 | cp_heap->cpu_addr = dma_alloc_attrs(dev, |
| 121 | cp_heap->heap_size, |
| 122 | &(cp_heap->handle), |
| 123 | 0, |
| 124 | &attrs); |
Liam Mark | cc2d4bd | 2013-01-16 10:14:40 -0800 | [diff] [blame] | 125 | if (!cp_heap->cpu_addr) { |
| 126 | trace_ion_cp_alloc_retry(tries); |
Laura Abbott | 3180a5f | 2012-08-03 17:31:03 -0700 | [diff] [blame] | 127 | msleep(20); |
Liam Mark | cc2d4bd | 2013-01-16 10:14:40 -0800 | [diff] [blame] | 128 | } |
Laura Abbott | 3180a5f | 2012-08-03 17:31:03 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | if (!cp_heap->cpu_addr) |
| 132 | goto out; |
| 133 | |
| 134 | cp_heap->base = cp_heap->handle; |
| 135 | |
| 136 | cp_heap->pool = gen_pool_create(12, -1); |
| 137 | if (!cp_heap->pool) |
| 138 | goto out_free; |
| 139 | |
| 140 | ret = gen_pool_add(cp_heap->pool, cp_heap->base, |
| 141 | cp_heap->heap_size, -1); |
| 142 | if (ret < 0) |
| 143 | goto out_pool; |
| 144 | |
| 145 | return 0; |
| 146 | |
| 147 | out_pool: |
| 148 | gen_pool_destroy(cp_heap->pool); |
| 149 | out_free: |
| 150 | dma_free_coherent(dev, cp_heap->heap_size, cp_heap->cpu_addr, |
| 151 | cp_heap->handle); |
| 152 | out: |
| 153 | return ION_CP_ALLOCATE_FAIL; |
| 154 | } |
| 155 | |
| 156 | static void free_heap_memory(struct ion_heap *heap) |
| 157 | { |
| 158 | struct device *dev = heap->priv; |
| 159 | struct ion_cp_heap *cp_heap = |
| 160 | container_of(heap, struct ion_cp_heap, heap); |
| 161 | |
| 162 | /* release memory */ |
| 163 | dma_free_coherent(dev, cp_heap->heap_size, cp_heap->cpu_addr, |
| 164 | cp_heap->handle); |
| 165 | gen_pool_destroy(cp_heap->pool); |
| 166 | cp_heap->pool = NULL; |
| 167 | cp_heap->cpu_addr = 0; |
| 168 | } |
| 169 | |
| 170 | |
| 171 | |
Olav Haugan | 2a5404b | 2012-02-01 17:51:30 -0800 | [diff] [blame] | 172 | /** |
| 173 | * Get the total number of kernel mappings. |
| 174 | * Must be called with heap->lock locked. |
| 175 | */ |
| 176 | static unsigned long ion_cp_get_total_kmap_count( |
| 177 | const struct ion_cp_heap *cp_heap) |
| 178 | { |
| 179 | return cp_heap->kmap_cached_count + cp_heap->kmap_uncached_count; |
| 180 | } |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 181 | |
Laura Abbott | 94ad25e | 2012-08-03 13:56:21 -0700 | [diff] [blame] | 182 | static int ion_on_first_alloc(struct ion_heap *heap) |
| 183 | { |
| 184 | struct ion_cp_heap *cp_heap = |
| 185 | container_of(heap, struct ion_cp_heap, heap); |
Laura Abbott | 94ad25e | 2012-08-03 13:56:21 -0700 | [diff] [blame] | 186 | int ret_value; |
| 187 | |
Laura Abbott | 3180a5f | 2012-08-03 17:31:03 -0700 | [diff] [blame] | 188 | if (cp_heap->cma) { |
| 189 | ret_value = allocate_heap_memory(heap); |
| 190 | if (ret_value) |
| 191 | return 1; |
| 192 | } |
Laura Abbott | 94ad25e | 2012-08-03 13:56:21 -0700 | [diff] [blame] | 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | static void ion_on_last_free(struct ion_heap *heap) |
| 197 | { |
| 198 | struct ion_cp_heap *cp_heap = |
| 199 | container_of(heap, struct ion_cp_heap, heap); |
| 200 | |
Laura Abbott | 3180a5f | 2012-08-03 17:31:03 -0700 | [diff] [blame] | 201 | if (cp_heap->cma) |
| 202 | free_heap_memory(heap); |
Laura Abbott | 94ad25e | 2012-08-03 13:56:21 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 205 | /** |
Larry Bassel | d653d09 | 2013-06-20 10:46:21 -0700 | [diff] [blame] | 206 | * Protects memory if heap is unsecured heap. |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 207 | * Must be called with heap->lock locked. |
| 208 | */ |
Laura Abbott | 7e44648 | 2012-06-13 15:59:39 -0700 | [diff] [blame] | 209 | static int ion_cp_protect(struct ion_heap *heap, int version, void *data) |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 210 | { |
| 211 | struct ion_cp_heap *cp_heap = |
| 212 | container_of(heap, struct ion_cp_heap, heap); |
| 213 | int ret_value = 0; |
| 214 | |
Laura Abbott | f68983e | 2012-06-13 16:23:23 -0700 | [diff] [blame] | 215 | if (atomic_inc_return(&cp_heap->protect_cnt) == 1) { |
Laura Abbott | caafeea | 2011-12-13 11:43:10 -0800 | [diff] [blame] | 216 | /* Make sure we are in C state when the heap is protected. */ |
Laura Abbott | 94ad25e | 2012-08-03 13:56:21 -0700 | [diff] [blame] | 217 | if (!cp_heap->allocated_bytes) |
| 218 | if (ion_on_first_alloc(heap)) |
Laura Abbott | caafeea | 2011-12-13 11:43:10 -0800 | [diff] [blame] | 219 | goto out; |
Laura Abbott | caafeea | 2011-12-13 11:43:10 -0800 | [diff] [blame] | 220 | |
Olav Haugan | ea66e7a | 2012-01-23 17:30:27 -0800 | [diff] [blame] | 221 | ret_value = ion_cp_protect_mem(cp_heap->secure_base, |
Laura Abbott | 7e44648 | 2012-06-13 15:59:39 -0700 | [diff] [blame] | 222 | cp_heap->secure_size, cp_heap->permission_type, |
| 223 | version, data); |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 224 | if (ret_value) { |
| 225 | pr_err("Failed to protect memory for heap %s - " |
Olav Haugan | 9b2d1c2 | 2012-01-09 15:23:08 -0800 | [diff] [blame] | 226 | "error code: %d\n", heap->name, ret_value); |
Laura Abbott | caafeea | 2011-12-13 11:43:10 -0800 | [diff] [blame] | 227 | |
Laura Abbott | 94ad25e | 2012-08-03 13:56:21 -0700 | [diff] [blame] | 228 | if (!cp_heap->allocated_bytes) |
| 229 | ion_on_last_free(heap); |
| 230 | |
Laura Abbott | f68983e | 2012-06-13 16:23:23 -0700 | [diff] [blame] | 231 | atomic_dec(&cp_heap->protect_cnt); |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 232 | } else { |
Olav Haugan | ea66e7a | 2012-01-23 17:30:27 -0800 | [diff] [blame] | 233 | cp_heap->heap_protected = HEAP_PROTECTED; |
Laura Abbott | 1135c9e | 2013-03-13 15:33:40 -0700 | [diff] [blame] | 234 | pr_debug("Protected heap %s @ 0x%pa\n", |
| 235 | heap->name, &cp_heap->base); |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 236 | } |
| 237 | } |
Laura Abbott | caafeea | 2011-12-13 11:43:10 -0800 | [diff] [blame] | 238 | out: |
Laura Abbott | f68983e | 2012-06-13 16:23:23 -0700 | [diff] [blame] | 239 | pr_debug("%s: protect count is %d\n", __func__, |
| 240 | atomic_read(&cp_heap->protect_cnt)); |
| 241 | BUG_ON(atomic_read(&cp_heap->protect_cnt) < 0); |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 242 | return ret_value; |
| 243 | } |
| 244 | |
| 245 | /** |
Larry Bassel | d653d09 | 2013-06-20 10:46:21 -0700 | [diff] [blame] | 246 | * Unprotects memory if heap is secure heap. |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 247 | * Must be called with heap->lock locked. |
| 248 | */ |
Laura Abbott | 7e44648 | 2012-06-13 15:59:39 -0700 | [diff] [blame] | 249 | static void ion_cp_unprotect(struct ion_heap *heap, int version, void *data) |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 250 | { |
| 251 | struct ion_cp_heap *cp_heap = |
| 252 | container_of(heap, struct ion_cp_heap, heap); |
| 253 | |
Laura Abbott | f68983e | 2012-06-13 16:23:23 -0700 | [diff] [blame] | 254 | if (atomic_dec_and_test(&cp_heap->protect_cnt)) { |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 255 | int error_code = ion_cp_unprotect_mem( |
Olav Haugan | 42ebe71 | 2012-01-10 16:30:58 -0800 | [diff] [blame] | 256 | cp_heap->secure_base, cp_heap->secure_size, |
Laura Abbott | 7e44648 | 2012-06-13 15:59:39 -0700 | [diff] [blame] | 257 | cp_heap->permission_type, version, data); |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 258 | if (error_code) { |
| 259 | pr_err("Failed to un-protect memory for heap %s - " |
Olav Haugan | 9b2d1c2 | 2012-01-09 15:23:08 -0800 | [diff] [blame] | 260 | "error code: %d\n", heap->name, error_code); |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 261 | } else { |
Olav Haugan | ea66e7a | 2012-01-23 17:30:27 -0800 | [diff] [blame] | 262 | cp_heap->heap_protected = HEAP_NOT_PROTECTED; |
Olav Haugan | 9b2d1c2 | 2012-01-09 15:23:08 -0800 | [diff] [blame] | 263 | pr_debug("Un-protected heap %s @ 0x%x\n", heap->name, |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 264 | (unsigned int) cp_heap->base); |
Laura Abbott | caafeea | 2011-12-13 11:43:10 -0800 | [diff] [blame] | 265 | |
Laura Abbott | 94ad25e | 2012-08-03 13:56:21 -0700 | [diff] [blame] | 266 | if (!cp_heap->allocated_bytes) |
| 267 | ion_on_last_free(heap); |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 268 | } |
| 269 | } |
Laura Abbott | f68983e | 2012-06-13 16:23:23 -0700 | [diff] [blame] | 270 | pr_debug("%s: protect count is %d\n", __func__, |
| 271 | atomic_read(&cp_heap->protect_cnt)); |
| 272 | BUG_ON(atomic_read(&cp_heap->protect_cnt) < 0); |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | ion_phys_addr_t ion_cp_allocate(struct ion_heap *heap, |
| 276 | unsigned long size, |
| 277 | unsigned long align, |
| 278 | unsigned long flags) |
| 279 | { |
| 280 | unsigned long offset; |
Adrian Alexei | 9253859 | 2013-03-27 10:53:43 -0700 | [diff] [blame] | 281 | unsigned long secure_allocation = flags & ION_FLAG_SECURE; |
| 282 | unsigned long force_contig = flags & ION_FLAG_FORCE_CONTIGUOUS; |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 283 | |
| 284 | struct ion_cp_heap *cp_heap = |
| 285 | container_of(heap, struct ion_cp_heap, heap); |
| 286 | |
| 287 | mutex_lock(&cp_heap->lock); |
Olav Haugan | ea66e7a | 2012-01-23 17:30:27 -0800 | [diff] [blame] | 288 | if (!secure_allocation && cp_heap->heap_protected == HEAP_PROTECTED) { |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 289 | mutex_unlock(&cp_heap->lock); |
| 290 | pr_err("ION cannot allocate un-secure memory from protected" |
Olav Haugan | 9b2d1c2 | 2012-01-09 15:23:08 -0800 | [diff] [blame] | 291 | " heap %s\n", heap->name); |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 292 | return ION_CP_ALLOCATE_FAIL; |
| 293 | } |
| 294 | |
Laura Abbott | 7db4e0b | 2013-01-03 14:20:16 -0800 | [diff] [blame] | 295 | if (!force_contig && !secure_allocation && |
Mitchel Humpherys | 345f023 | 2013-01-11 10:55:25 -0800 | [diff] [blame] | 296 | !cp_heap->allow_non_secure_allocation) { |
Laura Abbott | ac96331 | 2012-12-11 15:09:03 -0800 | [diff] [blame] | 297 | mutex_unlock(&cp_heap->lock); |
| 298 | pr_debug("%s: non-secure allocation disallowed from this heap\n", |
| 299 | __func__); |
| 300 | return ION_CP_ALLOCATE_FAIL; |
| 301 | } |
| 302 | |
Laura Abbott | 087db59 | 2012-11-01 09:41:37 -0700 | [diff] [blame] | 303 | /* |
| 304 | * The check above already checked for non-secure allocations when the |
| 305 | * heap is protected. HEAP_PROTECTED implies that this must be a secure |
| 306 | * allocation. If the heap is protected and there are userspace or |
| 307 | * cached kernel mappings, something has gone wrong in the security |
| 308 | * model. |
| 309 | */ |
| 310 | if (cp_heap->heap_protected == HEAP_PROTECTED) { |
| 311 | BUG_ON(cp_heap->umap_count != 0); |
| 312 | BUG_ON(cp_heap->kmap_cached_count != 0); |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 313 | } |
| 314 | |
Laura Abbott | caafeea | 2011-12-13 11:43:10 -0800 | [diff] [blame] | 315 | /* |
| 316 | * if this is the first reusable allocation, transition |
| 317 | * the heap |
| 318 | */ |
Laura Abbott | 94ad25e | 2012-08-03 13:56:21 -0700 | [diff] [blame] | 319 | if (!cp_heap->allocated_bytes) |
| 320 | if (ion_on_first_alloc(heap)) { |
Laura Abbott | caafeea | 2011-12-13 11:43:10 -0800 | [diff] [blame] | 321 | mutex_unlock(&cp_heap->lock); |
| 322 | return ION_RESERVED_ALLOCATE_FAIL; |
| 323 | } |
Laura Abbott | caafeea | 2011-12-13 11:43:10 -0800 | [diff] [blame] | 324 | |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 325 | cp_heap->allocated_bytes += size; |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 326 | mutex_unlock(&cp_heap->lock); |
| 327 | |
| 328 | offset = gen_pool_alloc_aligned(cp_heap->pool, |
| 329 | size, ilog2(align)); |
| 330 | |
| 331 | if (!offset) { |
| 332 | mutex_lock(&cp_heap->lock); |
Olav Haugan | d710ed1 | 2012-04-19 14:23:04 -0700 | [diff] [blame] | 333 | cp_heap->allocated_bytes -= size; |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 334 | if ((cp_heap->total_size - |
Olav Haugan | d710ed1 | 2012-04-19 14:23:04 -0700 | [diff] [blame] | 335 | cp_heap->allocated_bytes) >= size) |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 336 | pr_debug("%s: heap %s has enough memory (%lx) but" |
| 337 | " the allocation of size %lx still failed." |
Olav Haugan | 9b2d1c2 | 2012-01-09 15:23:08 -0800 | [diff] [blame] | 338 | " Memory is probably fragmented.\n", |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 339 | __func__, heap->name, |
| 340 | cp_heap->total_size - |
| 341 | cp_heap->allocated_bytes, size); |
Laura Abbott | 94ad25e | 2012-08-03 13:56:21 -0700 | [diff] [blame] | 342 | if (!cp_heap->allocated_bytes && |
| 343 | cp_heap->heap_protected == HEAP_NOT_PROTECTED) |
| 344 | ion_on_last_free(heap); |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 345 | mutex_unlock(&cp_heap->lock); |
| 346 | |
| 347 | return ION_CP_ALLOCATE_FAIL; |
| 348 | } |
| 349 | |
| 350 | return offset; |
| 351 | } |
| 352 | |
| 353 | void ion_cp_free(struct ion_heap *heap, ion_phys_addr_t addr, |
| 354 | unsigned long size) |
| 355 | { |
| 356 | struct ion_cp_heap *cp_heap = |
| 357 | container_of(heap, struct ion_cp_heap, heap); |
| 358 | |
| 359 | if (addr == ION_CP_ALLOCATE_FAIL) |
| 360 | return; |
| 361 | gen_pool_free(cp_heap->pool, addr, size); |
| 362 | |
| 363 | mutex_lock(&cp_heap->lock); |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 364 | cp_heap->allocated_bytes -= size; |
Laura Abbott | caafeea | 2011-12-13 11:43:10 -0800 | [diff] [blame] | 365 | |
Laura Abbott | 94ad25e | 2012-08-03 13:56:21 -0700 | [diff] [blame] | 366 | if (!cp_heap->allocated_bytes && |
| 367 | cp_heap->heap_protected == HEAP_NOT_PROTECTED) |
| 368 | ion_on_last_free(heap); |
Olav Haugan | 8726caf | 2012-05-10 15:11:35 -0700 | [diff] [blame] | 369 | |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 370 | mutex_unlock(&cp_heap->lock); |
| 371 | } |
| 372 | |
| 373 | static int ion_cp_heap_phys(struct ion_heap *heap, |
| 374 | struct ion_buffer *buffer, |
| 375 | ion_phys_addr_t *addr, size_t *len) |
| 376 | { |
Laura Abbott | 60ae937 | 2012-10-10 16:28:59 -0700 | [diff] [blame] | 377 | struct ion_cp_buffer *buf = buffer->priv_virt; |
| 378 | |
| 379 | *addr = buf->buffer; |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 380 | *len = buffer->size; |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | static int ion_cp_heap_allocate(struct ion_heap *heap, |
| 385 | struct ion_buffer *buffer, |
| 386 | unsigned long size, unsigned long align, |
| 387 | unsigned long flags) |
| 388 | { |
Laura Abbott | 60ae937 | 2012-10-10 16:28:59 -0700 | [diff] [blame] | 389 | struct ion_cp_buffer *buf; |
| 390 | phys_addr_t addr; |
| 391 | |
Mitchel Humpherys | 8fd2ac2 | 2013-02-27 14:41:36 -0800 | [diff] [blame] | 392 | /* |
| 393 | * we never want Ion to fault pages in for us with this |
| 394 | * heap. We want to set up the mappings ourselves in .map_user |
| 395 | */ |
| 396 | flags |= ION_FLAG_CACHED_NEEDS_SYNC; |
| 397 | |
Laura Abbott | 60ae937 | 2012-10-10 16:28:59 -0700 | [diff] [blame] | 398 | buf = kzalloc(sizeof(*buf), GFP_KERNEL); |
| 399 | if (!buf) |
| 400 | return ION_CP_ALLOCATE_FAIL; |
| 401 | |
| 402 | addr = ion_cp_allocate(heap, size, align, flags); |
| 403 | if (addr == ION_CP_ALLOCATE_FAIL) |
| 404 | return -ENOMEM; |
| 405 | |
| 406 | buf->buffer = addr; |
Laura Abbott | 9361930 | 2012-10-11 11:51:40 -0700 | [diff] [blame] | 407 | buf->want_delayed_unsecure = 0; |
Laura Abbott | 60ae937 | 2012-10-10 16:28:59 -0700 | [diff] [blame] | 408 | atomic_set(&buf->secure_cnt, 0); |
| 409 | mutex_init(&buf->lock); |
Adrian Alexei | 9253859 | 2013-03-27 10:53:43 -0700 | [diff] [blame] | 410 | buf->is_secure = flags & ION_FLAG_SECURE ? 1 : 0; |
Laura Abbott | 60ae937 | 2012-10-10 16:28:59 -0700 | [diff] [blame] | 411 | buffer->priv_virt = buf; |
| 412 | |
| 413 | return 0; |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | static void ion_cp_heap_free(struct ion_buffer *buffer) |
| 417 | { |
| 418 | struct ion_heap *heap = buffer->heap; |
Laura Abbott | 60ae937 | 2012-10-10 16:28:59 -0700 | [diff] [blame] | 419 | struct ion_cp_buffer *buf = buffer->priv_virt; |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 420 | |
Laura Abbott | 60ae937 | 2012-10-10 16:28:59 -0700 | [diff] [blame] | 421 | ion_cp_free(heap, buf->buffer, buffer->size); |
| 422 | WARN_ON(atomic_read(&buf->secure_cnt)); |
| 423 | WARN_ON(atomic_read(&buf->map_cnt)); |
| 424 | kfree(buf); |
| 425 | |
| 426 | buffer->priv_virt = NULL; |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 427 | } |
| 428 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 429 | struct sg_table *ion_cp_heap_create_sg_table(struct ion_buffer *buffer) |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 430 | { |
Mitchel Humpherys | c0aab03 | 2013-01-07 14:21:33 -0800 | [diff] [blame] | 431 | size_t chunk_size = buffer->size; |
Laura Abbott | 60ae937 | 2012-10-10 16:28:59 -0700 | [diff] [blame] | 432 | struct ion_cp_buffer *buf = buffer->priv_virt; |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 433 | |
Mitchel Humpherys | c0aab03 | 2013-01-07 14:21:33 -0800 | [diff] [blame] | 434 | if (ION_IS_CACHED(buffer->flags)) |
| 435 | chunk_size = PAGE_SIZE; |
| 436 | else if (buf->is_secure && IS_ALIGNED(buffer->size, SZ_1M)) |
| 437 | chunk_size = SZ_1M; |
| 438 | |
Mitchel Humpherys | 0432d69 | 2013-01-08 17:03:10 -0800 | [diff] [blame] | 439 | return ion_create_chunked_sg_table(buf->buffer, chunk_size, |
| 440 | buffer->size); |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 441 | } |
| 442 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 443 | struct sg_table *ion_cp_heap_map_dma(struct ion_heap *heap, |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 444 | struct ion_buffer *buffer) |
| 445 | { |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 446 | return ion_cp_heap_create_sg_table(buffer); |
Olav Haugan | 16cdb41 | 2012-03-27 13:02:17 -0700 | [diff] [blame] | 447 | } |
| 448 | |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 449 | void ion_cp_heap_unmap_dma(struct ion_heap *heap, |
| 450 | struct ion_buffer *buffer) |
| 451 | { |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 452 | if (buffer->sg_table) |
| 453 | sg_free_table(buffer->sg_table); |
| 454 | kfree(buffer->sg_table); |
| 455 | buffer->sg_table = 0; |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | /** |
| 459 | * Call request region for SMI memory of this is the first mapping. |
| 460 | */ |
| 461 | static int ion_cp_request_region(struct ion_cp_heap *cp_heap) |
| 462 | { |
| 463 | int ret_value = 0; |
Olav Haugan | 2a5404b | 2012-02-01 17:51:30 -0800 | [diff] [blame] | 464 | if ((cp_heap->umap_count + ion_cp_get_total_kmap_count(cp_heap)) == 0) |
Laura Abbott | aedbe42 | 2012-08-03 17:06:22 -0700 | [diff] [blame] | 465 | if (cp_heap->heap_request_region) |
| 466 | ret_value = cp_heap->heap_request_region( |
| 467 | cp_heap->bus_id); |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 468 | return ret_value; |
| 469 | } |
| 470 | |
| 471 | /** |
| 472 | * Call release region for SMI memory of this is the last un-mapping. |
| 473 | */ |
| 474 | static int ion_cp_release_region(struct ion_cp_heap *cp_heap) |
| 475 | { |
| 476 | int ret_value = 0; |
Olav Haugan | 2a5404b | 2012-02-01 17:51:30 -0800 | [diff] [blame] | 477 | if ((cp_heap->umap_count + ion_cp_get_total_kmap_count(cp_heap)) == 0) |
Laura Abbott | aedbe42 | 2012-08-03 17:06:22 -0700 | [diff] [blame] | 478 | if (cp_heap->heap_release_region) |
| 479 | ret_value = cp_heap->heap_release_region( |
| 480 | cp_heap->bus_id); |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 481 | return ret_value; |
| 482 | } |
| 483 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 484 | void *ion_cp_heap_map_kernel(struct ion_heap *heap, struct ion_buffer *buffer) |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 485 | { |
| 486 | struct ion_cp_heap *cp_heap = |
| 487 | container_of(heap, struct ion_cp_heap, heap); |
Olav Haugan | ea66e7a | 2012-01-23 17:30:27 -0800 | [diff] [blame] | 488 | void *ret_value = NULL; |
Laura Abbott | 60ae937 | 2012-10-10 16:28:59 -0700 | [diff] [blame] | 489 | struct ion_cp_buffer *buf = buffer->priv_virt; |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 490 | |
| 491 | mutex_lock(&cp_heap->lock); |
Olav Haugan | ea66e7a | 2012-01-23 17:30:27 -0800 | [diff] [blame] | 492 | if ((cp_heap->heap_protected == HEAP_NOT_PROTECTED) || |
| 493 | ((cp_heap->heap_protected == HEAP_PROTECTED) && |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 494 | !ION_IS_CACHED(buffer->flags))) { |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 495 | |
Olav Haugan | ea66e7a | 2012-01-23 17:30:27 -0800 | [diff] [blame] | 496 | if (ion_cp_request_region(cp_heap)) { |
| 497 | mutex_unlock(&cp_heap->lock); |
| 498 | return NULL; |
| 499 | } |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 500 | |
Laura Abbott | 7fe43d6 | 2013-03-25 16:44:58 -0700 | [diff] [blame] | 501 | if (cp_heap->cma) { |
Laura Abbott | 3180a5f | 2012-08-03 17:31:03 -0700 | [diff] [blame] | 502 | int npages = PAGE_ALIGN(buffer->size) / PAGE_SIZE; |
| 503 | struct page **pages = vmalloc( |
| 504 | sizeof(struct page *) * npages); |
| 505 | int i; |
| 506 | pgprot_t pgprot; |
Laura Abbott | caafeea | 2011-12-13 11:43:10 -0800 | [diff] [blame] | 507 | |
Olav Haugan | 245db99 | 2013-03-26 11:25:41 -0700 | [diff] [blame] | 508 | if (!pages) { |
| 509 | mutex_unlock(&cp_heap->lock); |
| 510 | return ERR_PTR(-ENOMEM); |
| 511 | } |
| 512 | |
Laura Abbott | 3180a5f | 2012-08-03 17:31:03 -0700 | [diff] [blame] | 513 | if (ION_IS_CACHED(buffer->flags)) |
| 514 | pgprot = PAGE_KERNEL; |
| 515 | else |
| 516 | pgprot = pgprot_writecombine(PAGE_KERNEL); |
| 517 | |
| 518 | for (i = 0; i < npages; i++) { |
| 519 | pages[i] = phys_to_page(buf->buffer + |
| 520 | i * PAGE_SIZE); |
| 521 | } |
| 522 | ret_value = vmap(pages, npages, VM_IOREMAP, pgprot); |
| 523 | vfree(pages); |
Laura Abbott | caafeea | 2011-12-13 11:43:10 -0800 | [diff] [blame] | 524 | } else { |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 525 | if (ION_IS_CACHED(buffer->flags)) |
Laura Abbott | 60ae937 | 2012-10-10 16:28:59 -0700 | [diff] [blame] | 526 | ret_value = ioremap_cached(buf->buffer, |
Laura Abbott | caafeea | 2011-12-13 11:43:10 -0800 | [diff] [blame] | 527 | buffer->size); |
| 528 | else |
Laura Abbott | 60ae937 | 2012-10-10 16:28:59 -0700 | [diff] [blame] | 529 | ret_value = ioremap(buf->buffer, |
Laura Abbott | caafeea | 2011-12-13 11:43:10 -0800 | [diff] [blame] | 530 | buffer->size); |
| 531 | } |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 532 | |
Olav Haugan | 2a5404b | 2012-02-01 17:51:30 -0800 | [diff] [blame] | 533 | if (!ret_value) { |
Olav Haugan | ea66e7a | 2012-01-23 17:30:27 -0800 | [diff] [blame] | 534 | ion_cp_release_region(cp_heap); |
Olav Haugan | 2a5404b | 2012-02-01 17:51:30 -0800 | [diff] [blame] | 535 | } else { |
| 536 | if (ION_IS_CACHED(buffer->flags)) |
| 537 | ++cp_heap->kmap_cached_count; |
| 538 | else |
| 539 | ++cp_heap->kmap_uncached_count; |
Laura Abbott | 60ae937 | 2012-10-10 16:28:59 -0700 | [diff] [blame] | 540 | atomic_inc(&buf->map_cnt); |
Olav Haugan | 2a5404b | 2012-02-01 17:51:30 -0800 | [diff] [blame] | 541 | } |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 542 | } |
| 543 | mutex_unlock(&cp_heap->lock); |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 544 | return ret_value; |
| 545 | } |
| 546 | |
| 547 | void ion_cp_heap_unmap_kernel(struct ion_heap *heap, |
| 548 | struct ion_buffer *buffer) |
| 549 | { |
| 550 | struct ion_cp_heap *cp_heap = |
| 551 | container_of(heap, struct ion_cp_heap, heap); |
Laura Abbott | 60ae937 | 2012-10-10 16:28:59 -0700 | [diff] [blame] | 552 | struct ion_cp_buffer *buf = buffer->priv_virt; |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 553 | |
Laura Abbott | 7fe43d6 | 2013-03-25 16:44:58 -0700 | [diff] [blame] | 554 | if (cp_heap->cma) |
Laura Abbott | 3180a5f | 2012-08-03 17:31:03 -0700 | [diff] [blame] | 555 | vunmap(buffer->vaddr); |
Laura Abbott | caafeea | 2011-12-13 11:43:10 -0800 | [diff] [blame] | 556 | else |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 557 | __arm_iounmap(buffer->vaddr); |
Laura Abbott | caafeea | 2011-12-13 11:43:10 -0800 | [diff] [blame] | 558 | |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 559 | buffer->vaddr = NULL; |
| 560 | |
| 561 | mutex_lock(&cp_heap->lock); |
Olav Haugan | 2a5404b | 2012-02-01 17:51:30 -0800 | [diff] [blame] | 562 | if (ION_IS_CACHED(buffer->flags)) |
| 563 | --cp_heap->kmap_cached_count; |
| 564 | else |
| 565 | --cp_heap->kmap_uncached_count; |
Laura Abbott | 60ae937 | 2012-10-10 16:28:59 -0700 | [diff] [blame] | 566 | |
| 567 | atomic_dec(&buf->map_cnt); |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 568 | ion_cp_release_region(cp_heap); |
| 569 | mutex_unlock(&cp_heap->lock); |
| 570 | |
| 571 | return; |
| 572 | } |
| 573 | |
| 574 | int ion_cp_heap_map_user(struct ion_heap *heap, struct ion_buffer *buffer, |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 575 | struct vm_area_struct *vma) |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 576 | { |
| 577 | int ret_value = -EAGAIN; |
Olav Haugan | ea66e7a | 2012-01-23 17:30:27 -0800 | [diff] [blame] | 578 | struct ion_cp_heap *cp_heap = |
| 579 | container_of(heap, struct ion_cp_heap, heap); |
Laura Abbott | 60ae937 | 2012-10-10 16:28:59 -0700 | [diff] [blame] | 580 | struct ion_cp_buffer *buf = buffer->priv_virt; |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 581 | |
Olav Haugan | ea66e7a | 2012-01-23 17:30:27 -0800 | [diff] [blame] | 582 | mutex_lock(&cp_heap->lock); |
Mitchel Humpherys | 8d0a492 | 2013-01-21 16:49:09 -0800 | [diff] [blame] | 583 | if (cp_heap->heap_protected == HEAP_NOT_PROTECTED && !buf->is_secure) { |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 584 | if (ion_cp_request_region(cp_heap)) { |
| 585 | mutex_unlock(&cp_heap->lock); |
| 586 | return -EINVAL; |
| 587 | } |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 588 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 589 | if (!ION_IS_CACHED(buffer->flags)) |
Olav Haugan | de074a7 | 2012-02-22 15:39:54 -0800 | [diff] [blame] | 590 | vma->vm_page_prot = pgprot_writecombine( |
| 591 | vma->vm_page_prot); |
| 592 | |
| 593 | ret_value = remap_pfn_range(vma, vma->vm_start, |
Laura Abbott | 60ae937 | 2012-10-10 16:28:59 -0700 | [diff] [blame] | 594 | __phys_to_pfn(buf->buffer) + vma->vm_pgoff, |
Olav Haugan | de074a7 | 2012-02-22 15:39:54 -0800 | [diff] [blame] | 595 | vma->vm_end - vma->vm_start, |
| 596 | vma->vm_page_prot); |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 597 | |
Laura Abbott | 60ae937 | 2012-10-10 16:28:59 -0700 | [diff] [blame] | 598 | if (ret_value) { |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 599 | ion_cp_release_region(cp_heap); |
Laura Abbott | 60ae937 | 2012-10-10 16:28:59 -0700 | [diff] [blame] | 600 | } else { |
| 601 | atomic_inc(&buf->map_cnt); |
Olav Haugan | ea66e7a | 2012-01-23 17:30:27 -0800 | [diff] [blame] | 602 | ++cp_heap->umap_count; |
Laura Abbott | 60ae937 | 2012-10-10 16:28:59 -0700 | [diff] [blame] | 603 | } |
| 604 | |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 605 | } |
Olav Haugan | ea66e7a | 2012-01-23 17:30:27 -0800 | [diff] [blame] | 606 | mutex_unlock(&cp_heap->lock); |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 607 | return ret_value; |
| 608 | } |
| 609 | |
| 610 | void ion_cp_heap_unmap_user(struct ion_heap *heap, |
| 611 | struct ion_buffer *buffer) |
| 612 | { |
| 613 | struct ion_cp_heap *cp_heap = |
| 614 | container_of(heap, struct ion_cp_heap, heap); |
Laura Abbott | 60ae937 | 2012-10-10 16:28:59 -0700 | [diff] [blame] | 615 | struct ion_cp_buffer *buf = buffer->priv_virt; |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 616 | |
| 617 | mutex_lock(&cp_heap->lock); |
| 618 | --cp_heap->umap_count; |
Laura Abbott | 60ae937 | 2012-10-10 16:28:59 -0700 | [diff] [blame] | 619 | atomic_dec(&buf->map_cnt); |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 620 | ion_cp_release_region(cp_heap); |
| 621 | mutex_unlock(&cp_heap->lock); |
| 622 | } |
| 623 | |
Olav Haugan | 0671b9a | 2012-05-25 11:58:56 -0700 | [diff] [blame] | 624 | static int ion_cp_print_debug(struct ion_heap *heap, struct seq_file *s, |
| 625 | const struct rb_root *mem_map) |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 626 | { |
Olav Haugan | 3d4fe1a | 2012-01-13 11:42:15 -0800 | [diff] [blame] | 627 | unsigned long total_alloc; |
| 628 | unsigned long total_size; |
Olav Haugan | 3d4fe1a | 2012-01-13 11:42:15 -0800 | [diff] [blame] | 629 | unsigned long umap_count; |
| 630 | unsigned long kmap_count; |
Olav Haugan | ea66e7a | 2012-01-23 17:30:27 -0800 | [diff] [blame] | 631 | unsigned long heap_protected; |
Olav Haugan | e1f5d83 | 2011-12-13 15:16:28 -0800 | [diff] [blame] | 632 | struct ion_cp_heap *cp_heap = |
| 633 | container_of(heap, struct ion_cp_heap, heap); |
Olav Haugan | e1f5d83 | 2011-12-13 15:16:28 -0800 | [diff] [blame] | 634 | |
| 635 | mutex_lock(&cp_heap->lock); |
Olav Haugan | 3d4fe1a | 2012-01-13 11:42:15 -0800 | [diff] [blame] | 636 | total_alloc = cp_heap->allocated_bytes; |
| 637 | total_size = cp_heap->total_size; |
Olav Haugan | 3d4fe1a | 2012-01-13 11:42:15 -0800 | [diff] [blame] | 638 | umap_count = cp_heap->umap_count; |
Olav Haugan | 2a5404b | 2012-02-01 17:51:30 -0800 | [diff] [blame] | 639 | kmap_count = ion_cp_get_total_kmap_count(cp_heap); |
Olav Haugan | ea66e7a | 2012-01-23 17:30:27 -0800 | [diff] [blame] | 640 | heap_protected = cp_heap->heap_protected == HEAP_PROTECTED; |
Olav Haugan | e1f5d83 | 2011-12-13 15:16:28 -0800 | [diff] [blame] | 641 | mutex_unlock(&cp_heap->lock); |
| 642 | |
Olav Haugan | 3d4fe1a | 2012-01-13 11:42:15 -0800 | [diff] [blame] | 643 | seq_printf(s, "total bytes currently allocated: %lx\n", total_alloc); |
| 644 | seq_printf(s, "total heap size: %lx\n", total_size); |
Olav Haugan | 3d4fe1a | 2012-01-13 11:42:15 -0800 | [diff] [blame] | 645 | seq_printf(s, "umapping count: %lx\n", umap_count); |
| 646 | seq_printf(s, "kmapping count: %lx\n", kmap_count); |
Olav Haugan | ea66e7a | 2012-01-23 17:30:27 -0800 | [diff] [blame] | 647 | seq_printf(s, "heap protected: %s\n", heap_protected ? "Yes" : "No"); |
Olav Haugan | 3d4fe1a | 2012-01-13 11:42:15 -0800 | [diff] [blame] | 648 | |
Olav Haugan | 0671b9a | 2012-05-25 11:58:56 -0700 | [diff] [blame] | 649 | if (mem_map) { |
| 650 | unsigned long base = cp_heap->base; |
| 651 | unsigned long size = cp_heap->total_size; |
| 652 | unsigned long end = base+size; |
| 653 | unsigned long last_end = base; |
| 654 | struct rb_node *n; |
| 655 | |
| 656 | seq_printf(s, "\nMemory Map\n"); |
| 657 | seq_printf(s, "%16.s %14.s %14.s %14.s\n", |
| 658 | "client", "start address", "end address", |
| 659 | "size (hex)"); |
| 660 | |
| 661 | for (n = rb_first(mem_map); n; n = rb_next(n)) { |
| 662 | struct mem_map_data *data = |
| 663 | rb_entry(n, struct mem_map_data, node); |
| 664 | const char *client_name = "(null)"; |
| 665 | |
| 666 | if (last_end < data->addr) { |
Laura Abbott | 1135c9e | 2013-03-13 15:33:40 -0700 | [diff] [blame] | 667 | phys_addr_t da; |
| 668 | |
| 669 | da = data->addr-1; |
| 670 | seq_printf(s, "%16.s %14pa %14pa %14lu (%lx)\n", |
| 671 | "FREE", &last_end, &da, |
Olav Haugan | 0671b9a | 2012-05-25 11:58:56 -0700 | [diff] [blame] | 672 | data->addr-last_end, |
| 673 | data->addr-last_end); |
| 674 | } |
| 675 | |
| 676 | if (data->client_name) |
| 677 | client_name = data->client_name; |
| 678 | |
Laura Abbott | 1135c9e | 2013-03-13 15:33:40 -0700 | [diff] [blame] | 679 | seq_printf(s, "%16.s %14pa %14pa %14lu (%lx)\n", |
| 680 | client_name, &data->addr, |
| 681 | &data->addr_end, |
Olav Haugan | 0671b9a | 2012-05-25 11:58:56 -0700 | [diff] [blame] | 682 | data->size, data->size); |
| 683 | last_end = data->addr_end+1; |
| 684 | } |
| 685 | if (last_end < end) { |
| 686 | seq_printf(s, "%16.s %14lx %14lx %14lu (%lx)\n", "FREE", |
| 687 | last_end, end-1, end-last_end, end-last_end); |
| 688 | } |
| 689 | } |
| 690 | |
Olav Haugan | 3d4fe1a | 2012-01-13 11:42:15 -0800 | [diff] [blame] | 691 | return 0; |
Olav Haugan | e1f5d83 | 2011-12-13 15:16:28 -0800 | [diff] [blame] | 692 | } |
| 693 | |
Laura Abbott | 7e44648 | 2012-06-13 15:59:39 -0700 | [diff] [blame] | 694 | int ion_cp_secure_heap(struct ion_heap *heap, int version, void *data) |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 695 | { |
| 696 | int ret_value; |
| 697 | struct ion_cp_heap *cp_heap = |
| 698 | container_of(heap, struct ion_cp_heap, heap); |
| 699 | mutex_lock(&cp_heap->lock); |
Olav Haugan | 2a5404b | 2012-02-01 17:51:30 -0800 | [diff] [blame] | 700 | if (cp_heap->umap_count == 0 && cp_heap->kmap_cached_count == 0) { |
Laura Abbott | 7e44648 | 2012-06-13 15:59:39 -0700 | [diff] [blame] | 701 | ret_value = ion_cp_protect(heap, version, data); |
Olav Haugan | ea66e7a | 2012-01-23 17:30:27 -0800 | [diff] [blame] | 702 | } else { |
| 703 | pr_err("ION cannot secure heap with outstanding mappings: " |
Olav Haugan | 2a5404b | 2012-02-01 17:51:30 -0800 | [diff] [blame] | 704 | "User space: %lu, kernel space (cached): %lu\n", |
| 705 | cp_heap->umap_count, cp_heap->kmap_cached_count); |
Olav Haugan | ea66e7a | 2012-01-23 17:30:27 -0800 | [diff] [blame] | 706 | ret_value = -EINVAL; |
| 707 | } |
| 708 | |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 709 | mutex_unlock(&cp_heap->lock); |
| 710 | return ret_value; |
| 711 | } |
| 712 | |
Laura Abbott | 7e44648 | 2012-06-13 15:59:39 -0700 | [diff] [blame] | 713 | int ion_cp_unsecure_heap(struct ion_heap *heap, int version, void *data) |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 714 | { |
| 715 | int ret_value = 0; |
| 716 | struct ion_cp_heap *cp_heap = |
| 717 | container_of(heap, struct ion_cp_heap, heap); |
| 718 | mutex_lock(&cp_heap->lock); |
Laura Abbott | 7e44648 | 2012-06-13 15:59:39 -0700 | [diff] [blame] | 719 | ion_cp_unprotect(heap, version, data); |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 720 | mutex_unlock(&cp_heap->lock); |
| 721 | return ret_value; |
| 722 | } |
| 723 | |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 724 | static struct ion_heap_ops cp_heap_ops = { |
| 725 | .allocate = ion_cp_heap_allocate, |
| 726 | .free = ion_cp_heap_free, |
| 727 | .phys = ion_cp_heap_phys, |
| 728 | .map_user = ion_cp_heap_map_user, |
| 729 | .unmap_user = ion_cp_heap_unmap_user, |
| 730 | .map_kernel = ion_cp_heap_map_kernel, |
| 731 | .unmap_kernel = ion_cp_heap_unmap_kernel, |
| 732 | .map_dma = ion_cp_heap_map_dma, |
| 733 | .unmap_dma = ion_cp_heap_unmap_dma, |
Olav Haugan | 3d4fe1a | 2012-01-13 11:42:15 -0800 | [diff] [blame] | 734 | .print_debug = ion_cp_print_debug, |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 735 | .secure_heap = ion_cp_secure_heap, |
| 736 | .unsecure_heap = ion_cp_unsecure_heap, |
Laura Abbott | 9361930 | 2012-10-11 11:51:40 -0700 | [diff] [blame] | 737 | .secure_buffer = ion_cp_secure_buffer, |
| 738 | .unsecure_buffer = ion_cp_unsecure_buffer, |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 739 | }; |
| 740 | |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 741 | struct ion_heap *ion_cp_heap_create(struct ion_platform_heap *heap_data) |
| 742 | { |
| 743 | struct ion_cp_heap *cp_heap; |
| 744 | int ret; |
| 745 | |
| 746 | cp_heap = kzalloc(sizeof(*cp_heap), GFP_KERNEL); |
| 747 | if (!cp_heap) |
| 748 | return ERR_PTR(-ENOMEM); |
| 749 | |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 750 | mutex_init(&cp_heap->lock); |
| 751 | |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 752 | |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 753 | cp_heap->allocated_bytes = 0; |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 754 | cp_heap->umap_count = 0; |
Olav Haugan | 2a5404b | 2012-02-01 17:51:30 -0800 | [diff] [blame] | 755 | cp_heap->kmap_cached_count = 0; |
| 756 | cp_heap->kmap_uncached_count = 0; |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 757 | cp_heap->total_size = heap_data->size; |
| 758 | cp_heap->heap.ops = &cp_heap_ops; |
Mitchel Humpherys | 362b52b | 2012-09-13 10:53:22 -0700 | [diff] [blame] | 759 | cp_heap->heap.type = (enum ion_heap_type) ION_HEAP_TYPE_CP; |
Olav Haugan | ea66e7a | 2012-01-23 17:30:27 -0800 | [diff] [blame] | 760 | cp_heap->heap_protected = HEAP_NOT_PROTECTED; |
Laura Abbott | 3180a5f | 2012-08-03 17:31:03 -0700 | [diff] [blame] | 761 | cp_heap->secure_base = heap_data->base; |
Olav Haugan | 42ebe71 | 2012-01-10 16:30:58 -0800 | [diff] [blame] | 762 | cp_heap->secure_size = heap_data->size; |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 763 | cp_heap->has_outer_cache = heap_data->has_outer_cache; |
Laura Abbott | 3180a5f | 2012-08-03 17:31:03 -0700 | [diff] [blame] | 764 | cp_heap->heap_size = heap_data->size; |
| 765 | |
Laura Abbott | f68983e | 2012-06-13 16:23:23 -0700 | [diff] [blame] | 766 | atomic_set(&cp_heap->protect_cnt, 0); |
Olav Haugan | 0703dbf | 2011-12-19 17:53:38 -0800 | [diff] [blame] | 767 | if (heap_data->extra_data) { |
| 768 | struct ion_cp_heap_pdata *extra_data = |
| 769 | heap_data->extra_data; |
| 770 | cp_heap->permission_type = extra_data->permission_type; |
Olav Haugan | 42ebe71 | 2012-01-10 16:30:58 -0800 | [diff] [blame] | 771 | if (extra_data->secure_size) { |
| 772 | cp_heap->secure_base = extra_data->secure_base; |
| 773 | cp_heap->secure_size = extra_data->secure_size; |
| 774 | } |
Olav Haugan | 0703dbf | 2011-12-19 17:53:38 -0800 | [diff] [blame] | 775 | if (extra_data->setup_region) |
| 776 | cp_heap->bus_id = extra_data->setup_region(); |
| 777 | if (extra_data->request_region) |
Laura Abbott | aedbe42 | 2012-08-03 17:06:22 -0700 | [diff] [blame] | 778 | cp_heap->heap_request_region = |
| 779 | extra_data->request_region; |
Olav Haugan | 0703dbf | 2011-12-19 17:53:38 -0800 | [diff] [blame] | 780 | if (extra_data->release_region) |
Laura Abbott | aedbe42 | 2012-08-03 17:06:22 -0700 | [diff] [blame] | 781 | cp_heap->heap_release_region = |
| 782 | extra_data->release_region; |
Laura Abbott | 3180a5f | 2012-08-03 17:31:03 -0700 | [diff] [blame] | 783 | cp_heap->cma = extra_data->is_cma; |
Mitchel Humpherys | 345f023 | 2013-01-11 10:55:25 -0800 | [diff] [blame] | 784 | cp_heap->allow_non_secure_allocation = |
| 785 | extra_data->allow_nonsecure_alloc; |
Olav Haugan | 8726caf | 2012-05-10 15:11:35 -0700 | [diff] [blame] | 786 | |
Olav Haugan | 0703dbf | 2011-12-19 17:53:38 -0800 | [diff] [blame] | 787 | } |
Olav Haugan | 8726caf | 2012-05-10 15:11:35 -0700 | [diff] [blame] | 788 | |
Laura Abbott | 3180a5f | 2012-08-03 17:31:03 -0700 | [diff] [blame] | 789 | if (cp_heap->cma) { |
| 790 | cp_heap->pool = NULL; |
| 791 | cp_heap->cpu_addr = 0; |
| 792 | cp_heap->heap.priv = heap_data->priv; |
| 793 | } else { |
| 794 | cp_heap->pool = gen_pool_create(12, -1); |
| 795 | if (!cp_heap->pool) |
| 796 | goto free_heap; |
| 797 | |
| 798 | cp_heap->base = heap_data->base; |
| 799 | ret = gen_pool_add(cp_heap->pool, cp_heap->base, |
| 800 | heap_data->size, -1); |
| 801 | if (ret < 0) |
| 802 | goto destroy_pool; |
| 803 | |
| 804 | } |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 805 | return &cp_heap->heap; |
| 806 | |
| 807 | destroy_pool: |
| 808 | gen_pool_destroy(cp_heap->pool); |
| 809 | |
| 810 | free_heap: |
| 811 | kfree(cp_heap); |
| 812 | |
| 813 | return ERR_PTR(-ENOMEM); |
| 814 | } |
| 815 | |
| 816 | void ion_cp_heap_destroy(struct ion_heap *heap) |
| 817 | { |
| 818 | struct ion_cp_heap *cp_heap = |
| 819 | container_of(heap, struct ion_cp_heap, heap); |
| 820 | |
| 821 | gen_pool_destroy(cp_heap->pool); |
| 822 | kfree(cp_heap); |
| 823 | cp_heap = NULL; |
| 824 | } |
| 825 | |
Olav Haugan | 0671b9a | 2012-05-25 11:58:56 -0700 | [diff] [blame] | 826 | void ion_cp_heap_get_base(struct ion_heap *heap, unsigned long *base, |
| 827 | unsigned long *size) \ |
| 828 | { |
| 829 | struct ion_cp_heap *cp_heap = |
| 830 | container_of(heap, struct ion_cp_heap, heap); |
| 831 | *base = cp_heap->base; |
| 832 | *size = cp_heap->total_size; |
| 833 | } |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 834 | |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 835 | |