Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/gpu/ion/ion.c |
| 3 | * |
| 4 | * Copyright (C) 2011 Google, Inc. |
Liam Mark | cc2d4bd | 2013-01-16 10:14:40 -0800 | [diff] [blame] | 5 | * Copyright (c) 2011-2013, The Linux Foundation. All rights reserved. |
Rebecca Schultz Zavin | 0c38bfd | 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 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 18 | #include <linux/module.h> |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 19 | #include <linux/device.h> |
| 20 | #include <linux/file.h> |
| 21 | #include <linux/fs.h> |
| 22 | #include <linux/anon_inodes.h> |
| 23 | #include <linux/ion.h> |
| 24 | #include <linux/list.h> |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 25 | #include <linux/memblock.h> |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 26 | #include <linux/miscdevice.h> |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 27 | #include <linux/mm.h> |
| 28 | #include <linux/mm_types.h> |
| 29 | #include <linux/rbtree.h> |
| 30 | #include <linux/sched.h> |
| 31 | #include <linux/slab.h> |
| 32 | #include <linux/seq_file.h> |
| 33 | #include <linux/uaccess.h> |
| 34 | #include <linux/debugfs.h> |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 35 | #include <linux/dma-buf.h> |
Mitchel Humpherys | d88b8eb | 2012-09-04 17:00:29 -0700 | [diff] [blame] | 36 | #include <linux/msm_ion.h> |
Liam Mark | cc2d4bd | 2013-01-16 10:14:40 -0800 | [diff] [blame] | 37 | #include <trace/events/kmem.h> |
| 38 | |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 39 | |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 40 | #include <mach/iommu_domains.h> |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 41 | #include "ion_priv.h" |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * struct ion_device - the metadata of the ion device node |
| 45 | * @dev: the actual misc device |
| 46 | * @buffers: an rb tree of all the existing buffers |
| 47 | * @lock: lock protecting the buffers & heaps trees |
| 48 | * @heaps: list of all the heaps in the system |
| 49 | * @user_clients: list of all the clients created from userspace |
| 50 | */ |
| 51 | struct ion_device { |
| 52 | struct miscdevice dev; |
| 53 | struct rb_root buffers; |
| 54 | struct mutex lock; |
| 55 | struct rb_root heaps; |
| 56 | long (*custom_ioctl) (struct ion_client *client, unsigned int cmd, |
| 57 | unsigned long arg); |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 58 | struct rb_root clients; |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 59 | struct dentry *debug_root; |
| 60 | }; |
| 61 | |
| 62 | /** |
| 63 | * struct ion_client - a process/hw block local address space |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 64 | * @node: node in the tree of all clients |
| 65 | * @dev: backpointer to ion device |
| 66 | * @handles: an rb tree of all the handles in this client |
| 67 | * @lock: lock protecting the tree of handles |
| 68 | * @heap_mask: mask of all supported heaps |
| 69 | * @name: used for debugging |
| 70 | * @task: used for debugging |
| 71 | * |
| 72 | * A client represents a list of buffers this client may access. |
| 73 | * The mutex stored here is used to protect both handles tree |
| 74 | * as well as the handles themselves, and should be held while modifying either. |
| 75 | */ |
| 76 | struct ion_client { |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 77 | struct rb_node node; |
| 78 | struct ion_device *dev; |
| 79 | struct rb_root handles; |
| 80 | struct mutex lock; |
| 81 | unsigned int heap_mask; |
Olav Haugan | 63e5f3b | 2012-01-11 16:42:37 -0800 | [diff] [blame] | 82 | char *name; |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 83 | struct task_struct *task; |
| 84 | pid_t pid; |
| 85 | struct dentry *debug_root; |
| 86 | }; |
| 87 | |
| 88 | /** |
| 89 | * ion_handle - a client local reference to a buffer |
| 90 | * @ref: reference count |
| 91 | * @client: back pointer to the client the buffer resides in |
| 92 | * @buffer: pointer to the buffer |
| 93 | * @node: node in the client's handle rbtree |
| 94 | * @kmap_cnt: count of times this client has mapped to kernel |
| 95 | * @dmap_cnt: count of times this client has mapped for dma |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 96 | * |
| 97 | * Modifications to node, map_cnt or mapping should be protected by the |
| 98 | * lock in the client. Other fields are never changed after initialization. |
| 99 | */ |
| 100 | struct ion_handle { |
| 101 | struct kref ref; |
| 102 | struct ion_client *client; |
| 103 | struct ion_buffer *buffer; |
| 104 | struct rb_node node; |
| 105 | unsigned int kmap_cnt; |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 106 | unsigned int iommu_map_cnt; |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 107 | }; |
| 108 | |
Rebecca Schultz Zavin | f858ba4 | 2012-09-21 11:46:06 -0700 | [diff] [blame] | 109 | bool ion_buffer_fault_user_mappings(struct ion_buffer *buffer) |
| 110 | { |
| 111 | return ((buffer->flags & ION_FLAG_CACHED) && |
| 112 | !(buffer->flags & ION_FLAG_CACHED_NEEDS_SYNC)); |
| 113 | } |
| 114 | |
Olav Haugan | b367659 | 2012-03-02 15:02:25 -0800 | [diff] [blame] | 115 | static void ion_iommu_release(struct kref *kref); |
| 116 | |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 117 | /* this function should only be called while dev->lock is held */ |
| 118 | static void ion_buffer_add(struct ion_device *dev, |
| 119 | struct ion_buffer *buffer) |
| 120 | { |
| 121 | struct rb_node **p = &dev->buffers.rb_node; |
| 122 | struct rb_node *parent = NULL; |
| 123 | struct ion_buffer *entry; |
| 124 | |
| 125 | while (*p) { |
| 126 | parent = *p; |
| 127 | entry = rb_entry(parent, struct ion_buffer, node); |
| 128 | |
| 129 | if (buffer < entry) { |
| 130 | p = &(*p)->rb_left; |
| 131 | } else if (buffer > entry) { |
| 132 | p = &(*p)->rb_right; |
| 133 | } else { |
| 134 | pr_err("%s: buffer already found.", __func__); |
| 135 | BUG(); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | rb_link_node(&buffer->node, parent, p); |
| 140 | rb_insert_color(&buffer->node, &dev->buffers); |
| 141 | } |
| 142 | |
Olav Haugan | 0fa9b60 | 2012-01-25 11:50:38 -0800 | [diff] [blame] | 143 | static void ion_iommu_add(struct ion_buffer *buffer, |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 144 | struct ion_iommu_map *iommu) |
| 145 | { |
| 146 | struct rb_node **p = &buffer->iommu_maps.rb_node; |
| 147 | struct rb_node *parent = NULL; |
| 148 | struct ion_iommu_map *entry; |
| 149 | |
| 150 | while (*p) { |
| 151 | parent = *p; |
| 152 | entry = rb_entry(parent, struct ion_iommu_map, node); |
| 153 | |
| 154 | if (iommu->key < entry->key) { |
| 155 | p = &(*p)->rb_left; |
| 156 | } else if (iommu->key > entry->key) { |
| 157 | p = &(*p)->rb_right; |
| 158 | } else { |
| 159 | pr_err("%s: buffer %p already has mapping for domain %d" |
| 160 | " and partition %d\n", __func__, |
| 161 | buffer, |
| 162 | iommu_map_domain(iommu), |
| 163 | iommu_map_partition(iommu)); |
| 164 | BUG(); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | rb_link_node(&iommu->node, parent, p); |
| 169 | rb_insert_color(&iommu->node, &buffer->iommu_maps); |
| 170 | |
| 171 | } |
| 172 | |
| 173 | static struct ion_iommu_map *ion_iommu_lookup(struct ion_buffer *buffer, |
| 174 | unsigned int domain_no, |
| 175 | unsigned int partition_no) |
| 176 | { |
| 177 | struct rb_node **p = &buffer->iommu_maps.rb_node; |
| 178 | struct rb_node *parent = NULL; |
| 179 | struct ion_iommu_map *entry; |
| 180 | uint64_t key = domain_no; |
| 181 | key = key << 32 | partition_no; |
| 182 | |
| 183 | while (*p) { |
| 184 | parent = *p; |
| 185 | entry = rb_entry(parent, struct ion_iommu_map, node); |
| 186 | |
| 187 | if (key < entry->key) |
| 188 | p = &(*p)->rb_left; |
| 189 | else if (key > entry->key) |
| 190 | p = &(*p)->rb_right; |
| 191 | else |
| 192 | return entry; |
| 193 | } |
| 194 | |
| 195 | return NULL; |
| 196 | } |
| 197 | |
Rebecca Schultz Zavin | b179067 | 2012-06-14 15:08:53 -0700 | [diff] [blame] | 198 | static int ion_buffer_alloc_dirty(struct ion_buffer *buffer); |
| 199 | |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 200 | /* this function should only be called while dev->lock is held */ |
| 201 | static struct ion_buffer *ion_buffer_create(struct ion_heap *heap, |
| 202 | struct ion_device *dev, |
| 203 | unsigned long len, |
| 204 | unsigned long align, |
| 205 | unsigned long flags) |
| 206 | { |
| 207 | struct ion_buffer *buffer; |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 208 | struct sg_table *table; |
Rebecca Schultz Zavin | f3f59ed | 2012-06-07 14:51:21 -0700 | [diff] [blame] | 209 | struct scatterlist *sg; |
| 210 | int i, ret; |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 211 | |
| 212 | buffer = kzalloc(sizeof(struct ion_buffer), GFP_KERNEL); |
| 213 | if (!buffer) |
| 214 | return ERR_PTR(-ENOMEM); |
| 215 | |
| 216 | buffer->heap = heap; |
Rebecca Schultz Zavin | f858ba4 | 2012-09-21 11:46:06 -0700 | [diff] [blame] | 217 | buffer->flags = flags; |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 218 | kref_init(&buffer->ref); |
| 219 | |
| 220 | ret = heap->ops->allocate(heap, buffer, len, align, flags); |
| 221 | if (ret) { |
| 222 | kfree(buffer); |
| 223 | return ERR_PTR(ret); |
| 224 | } |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 225 | |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 226 | buffer->dev = dev; |
| 227 | buffer->size = len; |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 228 | |
Rebecca Schultz Zavin | b179067 | 2012-06-14 15:08:53 -0700 | [diff] [blame] | 229 | table = heap->ops->map_dma(heap, buffer); |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 230 | if (IS_ERR_OR_NULL(table)) { |
| 231 | heap->ops->free(buffer); |
| 232 | kfree(buffer); |
| 233 | return ERR_PTR(PTR_ERR(table)); |
| 234 | } |
| 235 | buffer->sg_table = table; |
Rebecca Schultz Zavin | f858ba4 | 2012-09-21 11:46:06 -0700 | [diff] [blame] | 236 | if (ion_buffer_fault_user_mappings(buffer)) { |
Rebecca Schultz Zavin | b179067 | 2012-06-14 15:08:53 -0700 | [diff] [blame] | 237 | for_each_sg(buffer->sg_table->sgl, sg, buffer->sg_table->nents, |
| 238 | i) { |
| 239 | if (sg_dma_len(sg) == PAGE_SIZE) |
| 240 | continue; |
Rebecca Schultz Zavin | f858ba4 | 2012-09-21 11:46:06 -0700 | [diff] [blame] | 241 | pr_err("%s: cached mappings that will be faulted in " |
| 242 | "must have pagewise sg_lists\n", __func__); |
Rebecca Schultz Zavin | 7aea56a | 2012-09-10 16:12:01 -0700 | [diff] [blame] | 243 | ret = -EINVAL; |
| 244 | goto err; |
Rebecca Schultz Zavin | b179067 | 2012-06-14 15:08:53 -0700 | [diff] [blame] | 245 | } |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 246 | |
Rebecca Schultz Zavin | 7aea56a | 2012-09-10 16:12:01 -0700 | [diff] [blame] | 247 | ret = ion_buffer_alloc_dirty(buffer); |
| 248 | if (ret) |
| 249 | goto err; |
Rebecca Schultz Zavin | b179067 | 2012-06-14 15:08:53 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | buffer->dev = dev; |
| 253 | buffer->size = len; |
| 254 | INIT_LIST_HEAD(&buffer->vmas); |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 255 | mutex_init(&buffer->lock); |
Rebecca Schultz Zavin | f3f59ed | 2012-06-07 14:51:21 -0700 | [diff] [blame] | 256 | /* this will set up dma addresses for the sglist -- it is not |
| 257 | technically correct as per the dma api -- a specific |
| 258 | device isn't really taking ownership here. However, in practice on |
| 259 | our systems the only dma_address space is physical addresses. |
| 260 | Additionally, we can't afford the overhead of invalidating every |
| 261 | allocation via dma_map_sg. The implicit contract here is that |
| 262 | memory comming from the heaps is ready for dma, ie if it has a |
| 263 | cached mapping that mapping has been invalidated */ |
Laura Abbott | 6c13b982 | 2013-03-29 18:29:03 -0700 | [diff] [blame] | 264 | for_each_sg(buffer->sg_table->sgl, sg, buffer->sg_table->nents, i) { |
| 265 | if (sg_dma_address(sg) == 0) |
| 266 | sg_dma_address(sg) = sg_phys(sg); |
| 267 | } |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 268 | ion_buffer_add(dev, buffer); |
| 269 | return buffer; |
Rebecca Schultz Zavin | 7aea56a | 2012-09-10 16:12:01 -0700 | [diff] [blame] | 270 | |
| 271 | err: |
| 272 | heap->ops->unmap_dma(heap, buffer); |
| 273 | heap->ops->free(buffer); |
| 274 | kfree(buffer); |
| 275 | return ERR_PTR(ret); |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Olav Haugan | b367659 | 2012-03-02 15:02:25 -0800 | [diff] [blame] | 278 | /** |
| 279 | * Check for delayed IOMMU unmapping. Also unmap any outstanding |
| 280 | * mappings which would otherwise have been leaked. |
| 281 | */ |
| 282 | static void ion_iommu_delayed_unmap(struct ion_buffer *buffer) |
| 283 | { |
| 284 | struct ion_iommu_map *iommu_map; |
| 285 | struct rb_node *node; |
| 286 | const struct rb_root *rb = &(buffer->iommu_maps); |
| 287 | unsigned long ref_count; |
| 288 | unsigned int delayed_unmap; |
| 289 | |
| 290 | mutex_lock(&buffer->lock); |
| 291 | |
| 292 | while ((node = rb_first(rb)) != 0) { |
| 293 | iommu_map = rb_entry(node, struct ion_iommu_map, node); |
| 294 | ref_count = atomic_read(&iommu_map->ref.refcount); |
| 295 | delayed_unmap = iommu_map->flags & ION_IOMMU_UNMAP_DELAYED; |
| 296 | |
| 297 | if ((delayed_unmap && ref_count > 1) || !delayed_unmap) { |
| 298 | pr_err("%s: Virtual memory address leak in domain %u, partition %u\n", |
| 299 | __func__, iommu_map->domain_info[DI_DOMAIN_NUM], |
| 300 | iommu_map->domain_info[DI_PARTITION_NUM]); |
| 301 | } |
| 302 | /* set ref count to 1 to force release */ |
| 303 | kref_init(&iommu_map->ref); |
| 304 | kref_put(&iommu_map->ref, ion_iommu_release); |
| 305 | } |
| 306 | |
| 307 | mutex_unlock(&buffer->lock); |
| 308 | } |
| 309 | |
Laura Abbott | 9361930 | 2012-10-11 11:51:40 -0700 | [diff] [blame] | 310 | static void ion_delayed_unsecure(struct ion_buffer *buffer) |
| 311 | { |
| 312 | if (buffer->heap->ops->unsecure_buffer) |
| 313 | buffer->heap->ops->unsecure_buffer(buffer, 1); |
| 314 | } |
| 315 | |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 316 | static void ion_buffer_destroy(struct kref *kref) |
| 317 | { |
| 318 | struct ion_buffer *buffer = container_of(kref, struct ion_buffer, ref); |
| 319 | struct ion_device *dev = buffer->dev; |
| 320 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 321 | if (WARN_ON(buffer->kmap_cnt > 0)) |
| 322 | buffer->heap->ops->unmap_kernel(buffer->heap, buffer); |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 323 | buffer->heap->ops->unmap_dma(buffer->heap, buffer); |
| 324 | |
Laura Abbott | 9361930 | 2012-10-11 11:51:40 -0700 | [diff] [blame] | 325 | ion_delayed_unsecure(buffer); |
Olav Haugan | b367659 | 2012-03-02 15:02:25 -0800 | [diff] [blame] | 326 | ion_iommu_delayed_unmap(buffer); |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 327 | buffer->heap->ops->free(buffer); |
| 328 | mutex_lock(&dev->lock); |
| 329 | rb_erase(&buffer->node, &dev->buffers); |
| 330 | mutex_unlock(&dev->lock); |
Rebecca Schultz Zavin | 7aea56a | 2012-09-10 16:12:01 -0700 | [diff] [blame] | 331 | if (buffer->flags & ION_FLAG_CACHED) |
| 332 | kfree(buffer->dirty); |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 333 | kfree(buffer); |
| 334 | } |
| 335 | |
| 336 | static void ion_buffer_get(struct ion_buffer *buffer) |
| 337 | { |
| 338 | kref_get(&buffer->ref); |
| 339 | } |
| 340 | |
| 341 | static int ion_buffer_put(struct ion_buffer *buffer) |
| 342 | { |
| 343 | return kref_put(&buffer->ref, ion_buffer_destroy); |
| 344 | } |
| 345 | |
| 346 | static struct ion_handle *ion_handle_create(struct ion_client *client, |
| 347 | struct ion_buffer *buffer) |
| 348 | { |
| 349 | struct ion_handle *handle; |
| 350 | |
| 351 | handle = kzalloc(sizeof(struct ion_handle), GFP_KERNEL); |
| 352 | if (!handle) |
| 353 | return ERR_PTR(-ENOMEM); |
| 354 | kref_init(&handle->ref); |
| 355 | rb_init_node(&handle->node); |
| 356 | handle->client = client; |
| 357 | ion_buffer_get(buffer); |
| 358 | handle->buffer = buffer; |
| 359 | |
| 360 | return handle; |
| 361 | } |
| 362 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 363 | static void ion_handle_kmap_put(struct ion_handle *); |
| 364 | |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 365 | static void ion_handle_destroy(struct kref *kref) |
| 366 | { |
| 367 | struct ion_handle *handle = container_of(kref, struct ion_handle, ref); |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 368 | struct ion_client *client = handle->client; |
| 369 | struct ion_buffer *buffer = handle->buffer; |
| 370 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 371 | mutex_lock(&buffer->lock); |
| 372 | while (handle->kmap_cnt) |
| 373 | ion_handle_kmap_put(handle); |
| 374 | mutex_unlock(&buffer->lock); |
| 375 | |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 376 | if (!RB_EMPTY_NODE(&handle->node)) |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 377 | rb_erase(&handle->node, &client->handles); |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 378 | |
| 379 | ion_buffer_put(buffer); |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 380 | kfree(handle); |
| 381 | } |
| 382 | |
| 383 | struct ion_buffer *ion_handle_buffer(struct ion_handle *handle) |
| 384 | { |
| 385 | return handle->buffer; |
| 386 | } |
| 387 | |
| 388 | static void ion_handle_get(struct ion_handle *handle) |
| 389 | { |
| 390 | kref_get(&handle->ref); |
| 391 | } |
| 392 | |
| 393 | static int ion_handle_put(struct ion_handle *handle) |
| 394 | { |
| 395 | return kref_put(&handle->ref, ion_handle_destroy); |
| 396 | } |
| 397 | |
| 398 | static struct ion_handle *ion_handle_lookup(struct ion_client *client, |
| 399 | struct ion_buffer *buffer) |
| 400 | { |
| 401 | struct rb_node *n; |
| 402 | |
| 403 | for (n = rb_first(&client->handles); n; n = rb_next(n)) { |
| 404 | struct ion_handle *handle = rb_entry(n, struct ion_handle, |
| 405 | node); |
| 406 | if (handle->buffer == buffer) |
| 407 | return handle; |
| 408 | } |
| 409 | return NULL; |
| 410 | } |
| 411 | |
| 412 | static bool ion_handle_validate(struct ion_client *client, struct ion_handle *handle) |
| 413 | { |
| 414 | struct rb_node *n = client->handles.rb_node; |
| 415 | |
| 416 | while (n) { |
| 417 | struct ion_handle *handle_node = rb_entry(n, struct ion_handle, |
| 418 | node); |
| 419 | if (handle < handle_node) |
| 420 | n = n->rb_left; |
| 421 | else if (handle > handle_node) |
| 422 | n = n->rb_right; |
| 423 | else |
| 424 | return true; |
| 425 | } |
| 426 | return false; |
| 427 | } |
| 428 | |
| 429 | static void ion_handle_add(struct ion_client *client, struct ion_handle *handle) |
| 430 | { |
| 431 | struct rb_node **p = &client->handles.rb_node; |
| 432 | struct rb_node *parent = NULL; |
| 433 | struct ion_handle *entry; |
| 434 | |
| 435 | while (*p) { |
| 436 | parent = *p; |
| 437 | entry = rb_entry(parent, struct ion_handle, node); |
| 438 | |
| 439 | if (handle < entry) |
| 440 | p = &(*p)->rb_left; |
| 441 | else if (handle > entry) |
| 442 | p = &(*p)->rb_right; |
| 443 | else |
| 444 | WARN(1, "%s: buffer already found.", __func__); |
| 445 | } |
| 446 | |
| 447 | rb_link_node(&handle->node, parent, p); |
| 448 | rb_insert_color(&handle->node, &client->handles); |
| 449 | } |
| 450 | |
| 451 | struct ion_handle *ion_alloc(struct ion_client *client, size_t len, |
Hanumant Singh | 7d72bad | 2012-08-29 18:39:44 -0700 | [diff] [blame] | 452 | size_t align, unsigned int heap_mask, |
| 453 | unsigned int flags) |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 454 | { |
| 455 | struct rb_node *n; |
| 456 | struct ion_handle *handle; |
| 457 | struct ion_device *dev = client->dev; |
| 458 | struct ion_buffer *buffer = NULL; |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 459 | unsigned long secure_allocation = flags & ION_SECURE; |
Olav Haugan | 35e2f2f | 2012-01-11 17:31:47 -0800 | [diff] [blame] | 460 | const unsigned int MAX_DBG_STR_LEN = 64; |
| 461 | char dbg_str[MAX_DBG_STR_LEN]; |
| 462 | unsigned int dbg_str_idx = 0; |
| 463 | |
| 464 | dbg_str[0] = '\0'; |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 465 | |
Mitchel Humpherys | 485650f | 2013-03-15 16:06:07 -0700 | [diff] [blame] | 466 | /* |
| 467 | * For now, we don't want to fault in pages individually since |
| 468 | * clients are already doing manual cache maintenance. In |
| 469 | * other words, the implicit caching infrastructure is in |
| 470 | * place (in code) but should not be used. |
| 471 | */ |
| 472 | flags |= ION_FLAG_CACHED_NEEDS_SYNC; |
| 473 | |
Rebecca Schultz Zavin | b179067 | 2012-06-14 15:08:53 -0700 | [diff] [blame] | 474 | pr_debug("%s: len %d align %d heap_mask %u flags %x\n", __func__, len, |
| 475 | align, heap_mask, flags); |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 476 | /* |
| 477 | * traverse the list of heaps available in this system in priority |
| 478 | * order. If the heap type is supported by the client, and matches the |
| 479 | * request of the caller allocate from it. Repeat until allocate has |
| 480 | * succeeded or all heaps have been tried |
| 481 | */ |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 482 | if (WARN_ON(!len)) |
| 483 | return ERR_PTR(-EINVAL); |
| 484 | |
| 485 | len = PAGE_ALIGN(len); |
| 486 | |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 487 | mutex_lock(&dev->lock); |
| 488 | for (n = rb_first(&dev->heaps); n != NULL; n = rb_next(n)) { |
| 489 | struct ion_heap *heap = rb_entry(n, struct ion_heap, node); |
| 490 | /* if the client doesn't support this heap type */ |
| 491 | if (!((1 << heap->type) & client->heap_mask)) |
| 492 | continue; |
| 493 | /* if the caller didn't specify this heap type */ |
Hanumant Singh | 7d72bad | 2012-08-29 18:39:44 -0700 | [diff] [blame] | 494 | if (!((1 << heap->id) & heap_mask)) |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 495 | continue; |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 496 | /* Do not allow un-secure heap if secure is specified */ |
Mitchel Humpherys | 362b52b | 2012-09-13 10:53:22 -0700 | [diff] [blame] | 497 | if (secure_allocation && |
Laura Abbott | 4afbd8b | 2013-02-15 09:21:33 -0800 | [diff] [blame] | 498 | !ion_heap_allow_secure_allocation(heap->type)) |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 499 | continue; |
Liam Mark | cc2d4bd | 2013-01-16 10:14:40 -0800 | [diff] [blame] | 500 | trace_ion_alloc_buffer_start(client->name, heap->name, len, |
| 501 | heap_mask, flags); |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 502 | buffer = ion_buffer_create(heap, dev, len, align, flags); |
Liam Mark | cc2d4bd | 2013-01-16 10:14:40 -0800 | [diff] [blame] | 503 | trace_ion_alloc_buffer_end(client->name, heap->name, len, |
| 504 | heap_mask, flags); |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 505 | if (!IS_ERR_OR_NULL(buffer)) |
| 506 | break; |
Liam Mark | cc2d4bd | 2013-01-16 10:14:40 -0800 | [diff] [blame] | 507 | |
| 508 | trace_ion_alloc_buffer_fallback(client->name, heap->name, len, |
| 509 | heap_mask, flags, PTR_ERR(buffer)); |
Olav Haugan | 35e2f2f | 2012-01-11 17:31:47 -0800 | [diff] [blame] | 510 | if (dbg_str_idx < MAX_DBG_STR_LEN) { |
| 511 | unsigned int len_left = MAX_DBG_STR_LEN-dbg_str_idx-1; |
| 512 | int ret_value = snprintf(&dbg_str[dbg_str_idx], |
| 513 | len_left, "%s ", heap->name); |
| 514 | if (ret_value >= len_left) { |
| 515 | /* overflow */ |
| 516 | dbg_str[MAX_DBG_STR_LEN-1] = '\0'; |
| 517 | dbg_str_idx = MAX_DBG_STR_LEN; |
| 518 | } else if (ret_value >= 0) { |
| 519 | dbg_str_idx += ret_value; |
| 520 | } else { |
| 521 | /* error */ |
| 522 | dbg_str[MAX_DBG_STR_LEN-1] = '\0'; |
| 523 | } |
| 524 | } |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 525 | } |
| 526 | mutex_unlock(&dev->lock); |
| 527 | |
Liam Mark | cc2d4bd | 2013-01-16 10:14:40 -0800 | [diff] [blame] | 528 | if (buffer == NULL) { |
| 529 | trace_ion_alloc_buffer_fail(client->name, dbg_str, len, |
| 530 | heap_mask, flags, -ENODEV); |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 531 | return ERR_PTR(-ENODEV); |
Liam Mark | cc2d4bd | 2013-01-16 10:14:40 -0800 | [diff] [blame] | 532 | } |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 533 | |
| 534 | if (IS_ERR(buffer)) { |
Liam Mark | cc2d4bd | 2013-01-16 10:14:40 -0800 | [diff] [blame] | 535 | trace_ion_alloc_buffer_fail(client->name, dbg_str, len, |
| 536 | heap_mask, flags, PTR_ERR(buffer)); |
Olav Haugan | 35e2f2f | 2012-01-11 17:31:47 -0800 | [diff] [blame] | 537 | pr_debug("ION is unable to allocate 0x%x bytes (alignment: " |
| 538 | "0x%x) from heap(s) %sfor client %s with heap " |
| 539 | "mask 0x%x\n", |
| 540 | len, align, dbg_str, client->name, client->heap_mask); |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 541 | return ERR_PTR(PTR_ERR(buffer)); |
Olav Haugan | 35e2f2f | 2012-01-11 17:31:47 -0800 | [diff] [blame] | 542 | } |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 543 | |
| 544 | handle = ion_handle_create(client, buffer); |
| 545 | |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 546 | /* |
| 547 | * ion_buffer_create will create a buffer with a ref_cnt of 1, |
| 548 | * and ion_handle_create will take a second reference, drop one here |
| 549 | */ |
| 550 | ion_buffer_put(buffer); |
| 551 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 552 | if (!IS_ERR(handle)) { |
| 553 | mutex_lock(&client->lock); |
| 554 | ion_handle_add(client, handle); |
| 555 | mutex_unlock(&client->lock); |
| 556 | } |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 557 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 558 | |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 559 | return handle; |
| 560 | } |
Olav Haugan | bd2b692 | 2012-01-25 09:28:55 -0800 | [diff] [blame] | 561 | EXPORT_SYMBOL(ion_alloc); |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 562 | |
| 563 | void ion_free(struct ion_client *client, struct ion_handle *handle) |
| 564 | { |
| 565 | bool valid_handle; |
| 566 | |
| 567 | BUG_ON(client != handle->client); |
| 568 | |
| 569 | mutex_lock(&client->lock); |
| 570 | valid_handle = ion_handle_validate(client, handle); |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 571 | if (!valid_handle) { |
Laura Abbott | ec149ff | 2012-01-26 13:33:11 -0800 | [diff] [blame] | 572 | mutex_unlock(&client->lock); |
Olav Haugan | 6ede567 | 2012-04-19 10:20:22 -0700 | [diff] [blame] | 573 | WARN(1, "%s: invalid handle passed to free.\n", __func__); |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 574 | return; |
| 575 | } |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 576 | ion_handle_put(handle); |
Rebecca Schultz Zavin | aad11cb | 2012-08-20 15:41:11 -0700 | [diff] [blame] | 577 | mutex_unlock(&client->lock); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 578 | } |
Olav Haugan | bd2b692 | 2012-01-25 09:28:55 -0800 | [diff] [blame] | 579 | EXPORT_SYMBOL(ion_free); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 580 | |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 581 | int ion_phys(struct ion_client *client, struct ion_handle *handle, |
| 582 | ion_phys_addr_t *addr, size_t *len) |
| 583 | { |
| 584 | struct ion_buffer *buffer; |
| 585 | int ret; |
| 586 | |
| 587 | mutex_lock(&client->lock); |
| 588 | if (!ion_handle_validate(client, handle)) { |
| 589 | mutex_unlock(&client->lock); |
| 590 | return -EINVAL; |
| 591 | } |
| 592 | |
| 593 | buffer = handle->buffer; |
| 594 | |
| 595 | if (!buffer->heap->ops->phys) { |
| 596 | pr_err("%s: ion_phys is not implemented by this heap.\n", |
| 597 | __func__); |
| 598 | mutex_unlock(&client->lock); |
| 599 | return -ENODEV; |
| 600 | } |
| 601 | mutex_unlock(&client->lock); |
| 602 | ret = buffer->heap->ops->phys(buffer->heap, buffer, addr, len); |
| 603 | return ret; |
| 604 | } |
Olav Haugan | bd2b692 | 2012-01-25 09:28:55 -0800 | [diff] [blame] | 605 | EXPORT_SYMBOL(ion_phys); |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 606 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 607 | static void *ion_buffer_kmap_get(struct ion_buffer *buffer) |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 608 | { |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 609 | void *vaddr; |
| 610 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 611 | if (buffer->kmap_cnt) { |
| 612 | buffer->kmap_cnt++; |
| 613 | return buffer->vaddr; |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 614 | } |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 615 | vaddr = buffer->heap->ops->map_kernel(buffer->heap, buffer); |
| 616 | if (IS_ERR_OR_NULL(vaddr)) |
| 617 | return vaddr; |
| 618 | buffer->vaddr = vaddr; |
| 619 | buffer->kmap_cnt++; |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 620 | return vaddr; |
| 621 | } |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 622 | |
| 623 | static void *ion_handle_kmap_get(struct ion_handle *handle) |
| 624 | { |
| 625 | struct ion_buffer *buffer = handle->buffer; |
| 626 | void *vaddr; |
| 627 | |
| 628 | if (handle->kmap_cnt) { |
| 629 | handle->kmap_cnt++; |
| 630 | return buffer->vaddr; |
| 631 | } |
| 632 | vaddr = ion_buffer_kmap_get(buffer); |
| 633 | if (IS_ERR_OR_NULL(vaddr)) |
| 634 | return vaddr; |
| 635 | handle->kmap_cnt++; |
| 636 | return vaddr; |
| 637 | } |
| 638 | |
| 639 | static void ion_buffer_kmap_put(struct ion_buffer *buffer) |
| 640 | { |
| 641 | buffer->kmap_cnt--; |
| 642 | if (!buffer->kmap_cnt) { |
| 643 | buffer->heap->ops->unmap_kernel(buffer->heap, buffer); |
| 644 | buffer->vaddr = NULL; |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | static void ion_handle_kmap_put(struct ion_handle *handle) |
| 649 | { |
| 650 | struct ion_buffer *buffer = handle->buffer; |
| 651 | |
| 652 | handle->kmap_cnt--; |
| 653 | if (!handle->kmap_cnt) |
| 654 | ion_buffer_kmap_put(buffer); |
| 655 | } |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 656 | |
Olav Haugan | b367659 | 2012-03-02 15:02:25 -0800 | [diff] [blame] | 657 | static struct ion_iommu_map *__ion_iommu_map(struct ion_buffer *buffer, |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 658 | int domain_num, int partition_num, unsigned long align, |
| 659 | unsigned long iova_length, unsigned long flags, |
| 660 | unsigned long *iova) |
| 661 | { |
| 662 | struct ion_iommu_map *data; |
| 663 | int ret; |
| 664 | |
| 665 | data = kmalloc(sizeof(*data), GFP_ATOMIC); |
| 666 | |
| 667 | if (!data) |
Olav Haugan | b367659 | 2012-03-02 15:02:25 -0800 | [diff] [blame] | 668 | return ERR_PTR(-ENOMEM); |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 669 | |
| 670 | data->buffer = buffer; |
| 671 | iommu_map_domain(data) = domain_num; |
| 672 | iommu_map_partition(data) = partition_num; |
| 673 | |
| 674 | ret = buffer->heap->ops->map_iommu(buffer, data, |
| 675 | domain_num, |
| 676 | partition_num, |
| 677 | align, |
| 678 | iova_length, |
| 679 | flags); |
| 680 | |
| 681 | if (ret) |
| 682 | goto out; |
| 683 | |
| 684 | kref_init(&data->ref); |
| 685 | *iova = data->iova_addr; |
| 686 | |
| 687 | ion_iommu_add(buffer, data); |
| 688 | |
Olav Haugan | b367659 | 2012-03-02 15:02:25 -0800 | [diff] [blame] | 689 | return data; |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 690 | |
| 691 | out: |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 692 | kfree(data); |
Olav Haugan | b367659 | 2012-03-02 15:02:25 -0800 | [diff] [blame] | 693 | return ERR_PTR(ret); |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | int ion_map_iommu(struct ion_client *client, struct ion_handle *handle, |
| 697 | int domain_num, int partition_num, unsigned long align, |
| 698 | unsigned long iova_length, unsigned long *iova, |
| 699 | unsigned long *buffer_size, |
Olav Haugan | b367659 | 2012-03-02 15:02:25 -0800 | [diff] [blame] | 700 | unsigned long flags, unsigned long iommu_flags) |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 701 | { |
| 702 | struct ion_buffer *buffer; |
| 703 | struct ion_iommu_map *iommu_map; |
| 704 | int ret = 0; |
| 705 | |
Huaibin Yang | 8939970 | 2013-01-25 15:32:59 -0800 | [diff] [blame] | 706 | if (IS_ERR_OR_NULL(client)) { |
| 707 | pr_err("%s: client pointer is invalid\n", __func__); |
| 708 | return -EINVAL; |
| 709 | } |
| 710 | if (IS_ERR_OR_NULL(handle)) { |
| 711 | pr_err("%s: handle pointer is invalid\n", __func__); |
| 712 | return -EINVAL; |
| 713 | } |
| 714 | if (IS_ERR_OR_NULL(handle->buffer)) { |
| 715 | pr_err("%s: buffer pointer is invalid\n", __func__); |
| 716 | return -EINVAL; |
| 717 | } |
| 718 | |
Olav Haugan | 79e9ffa | 2012-02-24 13:11:10 -0800 | [diff] [blame] | 719 | if (ION_IS_CACHED(flags)) { |
| 720 | pr_err("%s: Cannot map iommu as cached.\n", __func__); |
| 721 | return -EINVAL; |
| 722 | } |
| 723 | |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 724 | mutex_lock(&client->lock); |
| 725 | if (!ion_handle_validate(client, handle)) { |
| 726 | pr_err("%s: invalid handle passed to map_kernel.\n", |
| 727 | __func__); |
| 728 | mutex_unlock(&client->lock); |
| 729 | return -EINVAL; |
| 730 | } |
| 731 | |
| 732 | buffer = handle->buffer; |
| 733 | mutex_lock(&buffer->lock); |
| 734 | |
| 735 | if (!handle->buffer->heap->ops->map_iommu) { |
| 736 | pr_err("%s: map_iommu is not implemented by this heap.\n", |
| 737 | __func__); |
| 738 | ret = -ENODEV; |
| 739 | goto out; |
| 740 | } |
| 741 | |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 742 | /* |
| 743 | * If clients don't want a custom iova length, just use whatever |
| 744 | * the buffer size is |
| 745 | */ |
| 746 | if (!iova_length) |
| 747 | iova_length = buffer->size; |
| 748 | |
| 749 | if (buffer->size > iova_length) { |
| 750 | pr_debug("%s: iova length %lx is not at least buffer size" |
| 751 | " %x\n", __func__, iova_length, buffer->size); |
| 752 | ret = -EINVAL; |
| 753 | goto out; |
| 754 | } |
| 755 | |
| 756 | if (buffer->size & ~PAGE_MASK) { |
| 757 | pr_debug("%s: buffer size %x is not aligned to %lx", __func__, |
| 758 | buffer->size, PAGE_SIZE); |
| 759 | ret = -EINVAL; |
| 760 | goto out; |
| 761 | } |
| 762 | |
| 763 | if (iova_length & ~PAGE_MASK) { |
| 764 | pr_debug("%s: iova_length %lx is not aligned to %lx", __func__, |
| 765 | iova_length, PAGE_SIZE); |
| 766 | ret = -EINVAL; |
| 767 | goto out; |
| 768 | } |
| 769 | |
| 770 | iommu_map = ion_iommu_lookup(buffer, domain_num, partition_num); |
Olav Haugan | b367659 | 2012-03-02 15:02:25 -0800 | [diff] [blame] | 771 | if (!iommu_map) { |
| 772 | iommu_map = __ion_iommu_map(buffer, domain_num, partition_num, |
| 773 | align, iova_length, flags, iova); |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 774 | if (!IS_ERR_OR_NULL(iommu_map)) { |
Olav Haugan | b367659 | 2012-03-02 15:02:25 -0800 | [diff] [blame] | 775 | iommu_map->flags = iommu_flags; |
| 776 | |
| 777 | if (iommu_map->flags & ION_IOMMU_UNMAP_DELAYED) |
| 778 | kref_get(&iommu_map->ref); |
Laura Abbott | 11bca60 | 2012-09-14 12:48:18 -0700 | [diff] [blame] | 779 | } else { |
| 780 | ret = PTR_ERR(iommu_map); |
Olav Haugan | b367659 | 2012-03-02 15:02:25 -0800 | [diff] [blame] | 781 | } |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 782 | } else { |
Olav Haugan | b367659 | 2012-03-02 15:02:25 -0800 | [diff] [blame] | 783 | if (iommu_map->flags != iommu_flags) { |
| 784 | pr_err("%s: handle %p is already mapped with iommu flags %lx, trying to map with flags %lx\n", |
| 785 | __func__, handle, |
| 786 | iommu_map->flags, iommu_flags); |
Olav Haugan | b367659 | 2012-03-02 15:02:25 -0800 | [diff] [blame] | 787 | ret = -EINVAL; |
| 788 | } else if (iommu_map->mapped_size != iova_length) { |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 789 | pr_err("%s: handle %p is already mapped with length" |
Olav Haugan | b367659 | 2012-03-02 15:02:25 -0800 | [diff] [blame] | 790 | " %x, trying to map with length %lx\n", |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 791 | __func__, handle, iommu_map->mapped_size, |
| 792 | iova_length); |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 793 | ret = -EINVAL; |
| 794 | } else { |
| 795 | kref_get(&iommu_map->ref); |
| 796 | *iova = iommu_map->iova_addr; |
| 797 | } |
| 798 | } |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 799 | if (!ret) |
| 800 | buffer->iommu_map_cnt++; |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 801 | *buffer_size = buffer->size; |
| 802 | out: |
| 803 | mutex_unlock(&buffer->lock); |
| 804 | mutex_unlock(&client->lock); |
| 805 | return ret; |
| 806 | } |
| 807 | EXPORT_SYMBOL(ion_map_iommu); |
| 808 | |
| 809 | static void ion_iommu_release(struct kref *kref) |
| 810 | { |
| 811 | struct ion_iommu_map *map = container_of(kref, struct ion_iommu_map, |
| 812 | ref); |
| 813 | struct ion_buffer *buffer = map->buffer; |
| 814 | |
| 815 | rb_erase(&map->node, &buffer->iommu_maps); |
| 816 | buffer->heap->ops->unmap_iommu(map); |
| 817 | kfree(map); |
| 818 | } |
| 819 | |
| 820 | void ion_unmap_iommu(struct ion_client *client, struct ion_handle *handle, |
| 821 | int domain_num, int partition_num) |
| 822 | { |
| 823 | struct ion_iommu_map *iommu_map; |
| 824 | struct ion_buffer *buffer; |
| 825 | |
Huaibin Yang | 8939970 | 2013-01-25 15:32:59 -0800 | [diff] [blame] | 826 | if (IS_ERR_OR_NULL(client)) { |
| 827 | pr_err("%s: client pointer is invalid\n", __func__); |
| 828 | return; |
| 829 | } |
| 830 | if (IS_ERR_OR_NULL(handle)) { |
| 831 | pr_err("%s: handle pointer is invalid\n", __func__); |
| 832 | return; |
| 833 | } |
| 834 | if (IS_ERR_OR_NULL(handle->buffer)) { |
| 835 | pr_err("%s: buffer pointer is invalid\n", __func__); |
| 836 | return; |
| 837 | } |
| 838 | |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 839 | mutex_lock(&client->lock); |
| 840 | buffer = handle->buffer; |
| 841 | |
| 842 | mutex_lock(&buffer->lock); |
| 843 | |
| 844 | iommu_map = ion_iommu_lookup(buffer, domain_num, partition_num); |
| 845 | |
| 846 | if (!iommu_map) { |
| 847 | WARN(1, "%s: (%d,%d) was never mapped for %p\n", __func__, |
| 848 | domain_num, partition_num, buffer); |
| 849 | goto out; |
| 850 | } |
| 851 | |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 852 | kref_put(&iommu_map->ref, ion_iommu_release); |
| 853 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 854 | buffer->iommu_map_cnt--; |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 855 | out: |
| 856 | mutex_unlock(&buffer->lock); |
| 857 | |
| 858 | mutex_unlock(&client->lock); |
| 859 | |
| 860 | } |
| 861 | EXPORT_SYMBOL(ion_unmap_iommu); |
| 862 | |
Mitchel Humpherys | 911b4b7 | 2012-09-12 14:42:50 -0700 | [diff] [blame] | 863 | void *ion_map_kernel(struct ion_client *client, struct ion_handle *handle) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 864 | { |
| 865 | struct ion_buffer *buffer; |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 866 | void *vaddr; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 867 | |
| 868 | mutex_lock(&client->lock); |
| 869 | if (!ion_handle_validate(client, handle)) { |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 870 | pr_err("%s: invalid handle passed to map_kernel.\n", |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 871 | __func__); |
Rebecca Schultz Zavin | e6ee124 | 2011-06-30 12:19:55 -0700 | [diff] [blame] | 872 | mutex_unlock(&client->lock); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 873 | return ERR_PTR(-EINVAL); |
| 874 | } |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 875 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 876 | buffer = handle->buffer; |
| 877 | |
| 878 | if (!handle->buffer->heap->ops->map_kernel) { |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 879 | pr_err("%s: map_kernel is not implemented by this heap.\n", |
| 880 | __func__); |
Rebecca Schultz Zavin | e6ee124 | 2011-06-30 12:19:55 -0700 | [diff] [blame] | 881 | mutex_unlock(&client->lock); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 882 | return ERR_PTR(-ENODEV); |
| 883 | } |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 884 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 885 | mutex_lock(&buffer->lock); |
| 886 | vaddr = ion_handle_kmap_get(handle); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 887 | mutex_unlock(&buffer->lock); |
| 888 | mutex_unlock(&client->lock); |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 889 | return vaddr; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 890 | } |
Olav Haugan | bd453a9 | 2012-07-05 14:21:34 -0700 | [diff] [blame] | 891 | EXPORT_SYMBOL(ion_map_kernel); |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 892 | |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 893 | void ion_unmap_kernel(struct ion_client *client, struct ion_handle *handle) |
| 894 | { |
| 895 | struct ion_buffer *buffer; |
| 896 | |
| 897 | mutex_lock(&client->lock); |
| 898 | buffer = handle->buffer; |
| 899 | mutex_lock(&buffer->lock); |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 900 | ion_handle_kmap_put(handle); |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 901 | mutex_unlock(&buffer->lock); |
| 902 | mutex_unlock(&client->lock); |
| 903 | } |
Olav Haugan | bd453a9 | 2012-07-05 14:21:34 -0700 | [diff] [blame] | 904 | EXPORT_SYMBOL(ion_unmap_kernel); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 905 | |
Olav Haugan | 41f8579 | 2012-02-08 15:28:05 -0800 | [diff] [blame] | 906 | int ion_do_cache_op(struct ion_client *client, struct ion_handle *handle, |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 907 | void *uaddr, unsigned long offset, unsigned long len, |
| 908 | unsigned int cmd) |
| 909 | { |
| 910 | struct ion_buffer *buffer; |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 911 | int ret = -EINVAL; |
| 912 | |
| 913 | mutex_lock(&client->lock); |
| 914 | if (!ion_handle_validate(client, handle)) { |
| 915 | pr_err("%s: invalid handle passed to do_cache_op.\n", |
| 916 | __func__); |
| 917 | mutex_unlock(&client->lock); |
| 918 | return -EINVAL; |
| 919 | } |
| 920 | buffer = handle->buffer; |
| 921 | mutex_lock(&buffer->lock); |
| 922 | |
Laura Abbott | cbaa668 | 2011-10-19 12:14:14 -0700 | [diff] [blame] | 923 | if (!ION_IS_CACHED(buffer->flags)) { |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 924 | ret = 0; |
| 925 | goto out; |
| 926 | } |
| 927 | |
| 928 | if (!handle->buffer->heap->ops->cache_op) { |
| 929 | pr_err("%s: cache_op is not implemented by this heap.\n", |
| 930 | __func__); |
| 931 | ret = -ENODEV; |
| 932 | goto out; |
| 933 | } |
| 934 | |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 935 | |
| 936 | ret = buffer->heap->ops->cache_op(buffer->heap, buffer, uaddr, |
| 937 | offset, len, cmd); |
| 938 | |
| 939 | out: |
| 940 | mutex_unlock(&buffer->lock); |
| 941 | mutex_unlock(&client->lock); |
| 942 | return ret; |
| 943 | |
| 944 | } |
Olav Haugan | bd453a9 | 2012-07-05 14:21:34 -0700 | [diff] [blame] | 945 | EXPORT_SYMBOL(ion_do_cache_op); |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 946 | |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 947 | static int ion_debug_client_show(struct seq_file *s, void *unused) |
| 948 | { |
| 949 | struct ion_client *client = s->private; |
| 950 | struct rb_node *n; |
Olav Haugan | 854c9e1 | 2012-05-16 16:34:28 -0700 | [diff] [blame] | 951 | struct rb_node *n2; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 952 | |
Olav Haugan | 854c9e1 | 2012-05-16 16:34:28 -0700 | [diff] [blame] | 953 | seq_printf(s, "%16.16s: %16.16s : %16.16s : %12.12s : %12.12s : %s\n", |
| 954 | "heap_name", "size_in_bytes", "handle refcount", |
| 955 | "buffer", "physical", "[domain,partition] - virt"); |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 956 | |
| 957 | mutex_lock(&client->lock); |
| 958 | for (n = rb_first(&client->handles); n; n = rb_next(n)) { |
| 959 | struct ion_handle *handle = rb_entry(n, struct ion_handle, |
| 960 | node); |
| 961 | enum ion_heap_type type = handle->buffer->heap->type; |
| 962 | |
Olav Haugan | 854c9e1 | 2012-05-16 16:34:28 -0700 | [diff] [blame] | 963 | seq_printf(s, "%16.16s: %16x : %16d : %12p", |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 964 | handle->buffer->heap->name, |
| 965 | handle->buffer->size, |
| 966 | atomic_read(&handle->ref.refcount), |
| 967 | handle->buffer); |
Olav Haugan | 854c9e1 | 2012-05-16 16:34:28 -0700 | [diff] [blame] | 968 | |
| 969 | if (type == ION_HEAP_TYPE_SYSTEM_CONTIG || |
| 970 | type == ION_HEAP_TYPE_CARVEOUT || |
Mitchel Humpherys | 362b52b | 2012-09-13 10:53:22 -0700 | [diff] [blame] | 971 | type == (enum ion_heap_type) ION_HEAP_TYPE_CP) |
Laura Abbott | 1135c9e | 2013-03-13 15:33:40 -0700 | [diff] [blame] | 972 | seq_printf(s, " : %12pa", &handle->buffer->priv_phys); |
Olav Haugan | 854c9e1 | 2012-05-16 16:34:28 -0700 | [diff] [blame] | 973 | else |
| 974 | seq_printf(s, " : %12s", "N/A"); |
| 975 | |
| 976 | for (n2 = rb_first(&handle->buffer->iommu_maps); n2; |
| 977 | n2 = rb_next(n2)) { |
| 978 | struct ion_iommu_map *imap = |
| 979 | rb_entry(n2, struct ion_iommu_map, node); |
| 980 | seq_printf(s, " : [%d,%d] - %8lx", |
| 981 | imap->domain_info[DI_DOMAIN_NUM], |
| 982 | imap->domain_info[DI_PARTITION_NUM], |
| 983 | imap->iova_addr); |
| 984 | } |
| 985 | seq_printf(s, "\n"); |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 986 | } |
| 987 | mutex_unlock(&client->lock); |
| 988 | |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 989 | return 0; |
| 990 | } |
| 991 | |
| 992 | static int ion_debug_client_open(struct inode *inode, struct file *file) |
| 993 | { |
| 994 | return single_open(file, ion_debug_client_show, inode->i_private); |
| 995 | } |
| 996 | |
| 997 | static const struct file_operations debug_client_fops = { |
| 998 | .open = ion_debug_client_open, |
| 999 | .read = seq_read, |
| 1000 | .llseek = seq_lseek, |
| 1001 | .release = single_release, |
| 1002 | }; |
| 1003 | |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1004 | struct ion_client *ion_client_create(struct ion_device *dev, |
| 1005 | unsigned int heap_mask, |
| 1006 | const char *name) |
| 1007 | { |
| 1008 | struct ion_client *client; |
| 1009 | struct task_struct *task; |
| 1010 | struct rb_node **p; |
| 1011 | struct rb_node *parent = NULL; |
| 1012 | struct ion_client *entry; |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1013 | pid_t pid; |
Olav Haugan | e8a3197 | 2012-05-16 13:11:41 -0700 | [diff] [blame] | 1014 | unsigned int name_len; |
| 1015 | |
| 1016 | if (!name) { |
| 1017 | pr_err("%s: Name cannot be null\n", __func__); |
| 1018 | return ERR_PTR(-EINVAL); |
| 1019 | } |
| 1020 | name_len = strnlen(name, 64); |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1021 | |
| 1022 | get_task_struct(current->group_leader); |
| 1023 | task_lock(current->group_leader); |
| 1024 | pid = task_pid_nr(current->group_leader); |
| 1025 | /* don't bother to store task struct for kernel threads, |
| 1026 | they can't be killed anyway */ |
| 1027 | if (current->group_leader->flags & PF_KTHREAD) { |
| 1028 | put_task_struct(current->group_leader); |
| 1029 | task = NULL; |
| 1030 | } else { |
| 1031 | task = current->group_leader; |
| 1032 | } |
| 1033 | task_unlock(current->group_leader); |
| 1034 | |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1035 | client = kzalloc(sizeof(struct ion_client), GFP_KERNEL); |
| 1036 | if (!client) { |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 1037 | if (task) |
| 1038 | put_task_struct(current->group_leader); |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1039 | return ERR_PTR(-ENOMEM); |
| 1040 | } |
| 1041 | |
| 1042 | client->dev = dev; |
| 1043 | client->handles = RB_ROOT; |
| 1044 | mutex_init(&client->lock); |
Olav Haugan | 63e5f3b | 2012-01-11 16:42:37 -0800 | [diff] [blame] | 1045 | |
Olav Haugan | 6625c7d1 | 2012-01-24 13:50:43 -0800 | [diff] [blame] | 1046 | client->name = kzalloc(name_len+1, GFP_KERNEL); |
Olav Haugan | 63e5f3b | 2012-01-11 16:42:37 -0800 | [diff] [blame] | 1047 | if (!client->name) { |
| 1048 | put_task_struct(current->group_leader); |
| 1049 | kfree(client); |
| 1050 | return ERR_PTR(-ENOMEM); |
| 1051 | } else { |
Olav Haugan | 6625c7d1 | 2012-01-24 13:50:43 -0800 | [diff] [blame] | 1052 | strlcpy(client->name, name, name_len+1); |
Olav Haugan | 63e5f3b | 2012-01-11 16:42:37 -0800 | [diff] [blame] | 1053 | } |
| 1054 | |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1055 | client->heap_mask = heap_mask; |
| 1056 | client->task = task; |
| 1057 | client->pid = pid; |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1058 | |
| 1059 | mutex_lock(&dev->lock); |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 1060 | p = &dev->clients.rb_node; |
| 1061 | while (*p) { |
| 1062 | parent = *p; |
| 1063 | entry = rb_entry(parent, struct ion_client, node); |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1064 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 1065 | if (client < entry) |
| 1066 | p = &(*p)->rb_left; |
| 1067 | else if (client > entry) |
| 1068 | p = &(*p)->rb_right; |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1069 | } |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 1070 | rb_link_node(&client->node, parent, p); |
| 1071 | rb_insert_color(&client->node, &dev->clients); |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1072 | |
Laura Abbott | eed8603 | 2011-12-05 15:32:36 -0800 | [diff] [blame] | 1073 | |
| 1074 | client->debug_root = debugfs_create_file(name, 0664, |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1075 | dev->debug_root, client, |
| 1076 | &debug_client_fops); |
| 1077 | mutex_unlock(&dev->lock); |
| 1078 | |
| 1079 | return client; |
| 1080 | } |
| 1081 | |
Mitchel Humpherys | a75e4eb | 2012-12-14 16:12:23 -0800 | [diff] [blame] | 1082 | /** |
| 1083 | * ion_mark_dangling_buffers_locked() - Mark dangling buffers |
| 1084 | * @dev: the ion device whose buffers will be searched |
| 1085 | * |
| 1086 | * Sets marked=1 for all known buffers associated with `dev' that no |
| 1087 | * longer have a handle pointing to them. dev->lock should be held |
| 1088 | * across a call to this function (and should only be unlocked after |
| 1089 | * checking for marked buffers). |
| 1090 | */ |
| 1091 | static void ion_mark_dangling_buffers_locked(struct ion_device *dev) |
| 1092 | { |
| 1093 | struct rb_node *n, *n2; |
| 1094 | /* mark all buffers as 1 */ |
| 1095 | for (n = rb_first(&dev->buffers); n; n = rb_next(n)) { |
| 1096 | struct ion_buffer *buf = rb_entry(n, struct ion_buffer, |
| 1097 | node); |
| 1098 | |
| 1099 | buf->marked = 1; |
| 1100 | } |
| 1101 | |
| 1102 | /* now see which buffers we can access */ |
| 1103 | for (n = rb_first(&dev->clients); n; n = rb_next(n)) { |
| 1104 | struct ion_client *client = rb_entry(n, struct ion_client, |
| 1105 | node); |
| 1106 | |
| 1107 | mutex_lock(&client->lock); |
| 1108 | for (n2 = rb_first(&client->handles); n2; n2 = rb_next(n2)) { |
| 1109 | struct ion_handle *handle |
| 1110 | = rb_entry(n2, struct ion_handle, node); |
| 1111 | |
| 1112 | handle->buffer->marked = 0; |
| 1113 | |
| 1114 | } |
| 1115 | mutex_unlock(&client->lock); |
| 1116 | |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | #ifdef CONFIG_ION_LEAK_CHECK |
| 1121 | static u32 ion_debug_check_leaks_on_destroy; |
| 1122 | |
| 1123 | static int ion_check_for_and_print_leaks(struct ion_device *dev) |
| 1124 | { |
| 1125 | struct rb_node *n; |
| 1126 | int num_leaks = 0; |
| 1127 | |
| 1128 | if (!ion_debug_check_leaks_on_destroy) |
| 1129 | return 0; |
| 1130 | |
| 1131 | /* check for leaked buffers (those that no longer have a |
| 1132 | * handle pointing to them) */ |
| 1133 | ion_mark_dangling_buffers_locked(dev); |
| 1134 | |
| 1135 | /* Anyone still marked as a 1 means a leaked handle somewhere */ |
| 1136 | for (n = rb_first(&dev->buffers); n; n = rb_next(n)) { |
| 1137 | struct ion_buffer *buf = rb_entry(n, struct ion_buffer, |
| 1138 | node); |
| 1139 | |
| 1140 | if (buf->marked == 1) { |
| 1141 | pr_info("Leaked ion buffer at %p\n", buf); |
| 1142 | num_leaks++; |
| 1143 | } |
| 1144 | } |
| 1145 | return num_leaks; |
| 1146 | } |
| 1147 | static void setup_ion_leak_check(struct dentry *debug_root) |
| 1148 | { |
| 1149 | debugfs_create_bool("check_leaks_on_destroy", 0664, debug_root, |
| 1150 | &ion_debug_check_leaks_on_destroy); |
| 1151 | } |
| 1152 | #else |
| 1153 | static int ion_check_for_and_print_leaks(struct ion_device *dev) |
| 1154 | { |
| 1155 | return 0; |
| 1156 | } |
| 1157 | static void setup_ion_leak_check(struct dentry *debug_root) |
| 1158 | { |
| 1159 | } |
| 1160 | #endif |
| 1161 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 1162 | void ion_client_destroy(struct ion_client *client) |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1163 | { |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1164 | struct ion_device *dev = client->dev; |
| 1165 | struct rb_node *n; |
Mitchel Humpherys | a75e4eb | 2012-12-14 16:12:23 -0800 | [diff] [blame] | 1166 | int num_leaks; |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1167 | |
| 1168 | pr_debug("%s: %d\n", __func__, __LINE__); |
| 1169 | while ((n = rb_first(&client->handles))) { |
| 1170 | struct ion_handle *handle = rb_entry(n, struct ion_handle, |
| 1171 | node); |
| 1172 | ion_handle_destroy(&handle->ref); |
| 1173 | } |
| 1174 | mutex_lock(&dev->lock); |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 1175 | if (client->task) |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1176 | put_task_struct(client->task); |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 1177 | rb_erase(&client->node, &dev->clients); |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1178 | debugfs_remove_recursive(client->debug_root); |
Mitchel Humpherys | a75e4eb | 2012-12-14 16:12:23 -0800 | [diff] [blame] | 1179 | |
| 1180 | num_leaks = ion_check_for_and_print_leaks(dev); |
| 1181 | |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1182 | mutex_unlock(&dev->lock); |
| 1183 | |
Mitchel Humpherys | a75e4eb | 2012-12-14 16:12:23 -0800 | [diff] [blame] | 1184 | if (num_leaks) { |
| 1185 | struct task_struct *current_task = current; |
| 1186 | char current_task_name[TASK_COMM_LEN]; |
| 1187 | get_task_comm(current_task_name, current_task); |
| 1188 | WARN(1, "%s: Detected %d leaked ion buffer%s.\n", |
| 1189 | __func__, num_leaks, num_leaks == 1 ? "" : "s"); |
| 1190 | pr_info("task name at time of leak: %s, pid: %d\n", |
| 1191 | current_task_name, current_task->pid); |
| 1192 | } |
| 1193 | |
Olav Haugan | 63e5f3b | 2012-01-11 16:42:37 -0800 | [diff] [blame] | 1194 | kfree(client->name); |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1195 | kfree(client); |
| 1196 | } |
Olav Haugan | bd453a9 | 2012-07-05 14:21:34 -0700 | [diff] [blame] | 1197 | EXPORT_SYMBOL(ion_client_destroy); |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1198 | |
Laura Abbott | 273dd8e | 2011-10-12 14:26:33 -0700 | [diff] [blame] | 1199 | int ion_handle_get_flags(struct ion_client *client, struct ion_handle *handle, |
| 1200 | unsigned long *flags) |
Rebecca Schultz Zavin | 46d7133 | 2012-05-07 16:06:32 -0700 | [diff] [blame] | 1201 | { |
| 1202 | struct ion_buffer *buffer; |
Rebecca Schultz Zavin | 46d7133 | 2012-05-07 16:06:32 -0700 | [diff] [blame] | 1203 | |
| 1204 | mutex_lock(&client->lock); |
| 1205 | if (!ion_handle_validate(client, handle)) { |
Laura Abbott | 273dd8e | 2011-10-12 14:26:33 -0700 | [diff] [blame] | 1206 | pr_err("%s: invalid handle passed to %s.\n", |
| 1207 | __func__, __func__); |
Rebecca Schultz Zavin | 46d7133 | 2012-05-07 16:06:32 -0700 | [diff] [blame] | 1208 | mutex_unlock(&client->lock); |
Rebecca Schultz Zavin | 043a614 | 2012-02-01 11:09:46 -0800 | [diff] [blame] | 1209 | return -EINVAL; |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1210 | } |
Laura Abbott | 273dd8e | 2011-10-12 14:26:33 -0700 | [diff] [blame] | 1211 | buffer = handle->buffer; |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1212 | mutex_lock(&buffer->lock); |
Laura Abbott | 273dd8e | 2011-10-12 14:26:33 -0700 | [diff] [blame] | 1213 | *flags = buffer->flags; |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1214 | mutex_unlock(&buffer->lock); |
Laura Abbott | 273dd8e | 2011-10-12 14:26:33 -0700 | [diff] [blame] | 1215 | mutex_unlock(&client->lock); |
Rebecca Schultz Zavin | 043a614 | 2012-02-01 11:09:46 -0800 | [diff] [blame] | 1216 | |
Laura Abbott | 273dd8e | 2011-10-12 14:26:33 -0700 | [diff] [blame] | 1217 | return 0; |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1218 | } |
Laura Abbott | 273dd8e | 2011-10-12 14:26:33 -0700 | [diff] [blame] | 1219 | EXPORT_SYMBOL(ion_handle_get_flags); |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1220 | |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 1221 | int ion_handle_get_size(struct ion_client *client, struct ion_handle *handle, |
| 1222 | unsigned long *size) |
Rebecca Schultz Zavin | 043a614 | 2012-02-01 11:09:46 -0800 | [diff] [blame] | 1223 | { |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 1224 | struct ion_buffer *buffer; |
Rebecca Schultz Zavin | 043a614 | 2012-02-01 11:09:46 -0800 | [diff] [blame] | 1225 | |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 1226 | mutex_lock(&client->lock); |
| 1227 | if (!ion_handle_validate(client, handle)) { |
| 1228 | pr_err("%s: invalid handle passed to %s.\n", |
| 1229 | __func__, __func__); |
| 1230 | mutex_unlock(&client->lock); |
| 1231 | return -EINVAL; |
Rebecca Schultz Zavin | be4a1ee | 2012-04-26 20:44:10 -0700 | [diff] [blame] | 1232 | } |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 1233 | buffer = handle->buffer; |
Rebecca Schultz Zavin | be4a1ee | 2012-04-26 20:44:10 -0700 | [diff] [blame] | 1234 | mutex_lock(&buffer->lock); |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 1235 | *size = buffer->size; |
Rebecca Schultz Zavin | be4a1ee | 2012-04-26 20:44:10 -0700 | [diff] [blame] | 1236 | mutex_unlock(&buffer->lock); |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 1237 | mutex_unlock(&client->lock); |
| 1238 | |
| 1239 | return 0; |
| 1240 | } |
| 1241 | EXPORT_SYMBOL(ion_handle_get_size); |
| 1242 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 1243 | struct sg_table *ion_sg_table(struct ion_client *client, |
| 1244 | struct ion_handle *handle) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1245 | { |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 1246 | struct ion_buffer *buffer; |
| 1247 | struct sg_table *table; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1248 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 1249 | mutex_lock(&client->lock); |
| 1250 | if (!ion_handle_validate(client, handle)) { |
| 1251 | pr_err("%s: invalid handle passed to map_dma.\n", |
| 1252 | __func__); |
| 1253 | mutex_unlock(&client->lock); |
| 1254 | return ERR_PTR(-EINVAL); |
| 1255 | } |
| 1256 | buffer = handle->buffer; |
| 1257 | table = buffer->sg_table; |
| 1258 | mutex_unlock(&client->lock); |
| 1259 | return table; |
| 1260 | } |
Olav Haugan | bd453a9 | 2012-07-05 14:21:34 -0700 | [diff] [blame] | 1261 | EXPORT_SYMBOL(ion_sg_table); |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 1262 | |
Mitchel Humpherys | 0432d69 | 2013-01-08 17:03:10 -0800 | [diff] [blame] | 1263 | struct sg_table *ion_create_chunked_sg_table(phys_addr_t buffer_base, |
| 1264 | size_t chunk_size, size_t total_size) |
| 1265 | { |
| 1266 | struct sg_table *table; |
| 1267 | int i, n_chunks, ret; |
| 1268 | struct scatterlist *sg; |
| 1269 | |
| 1270 | table = kzalloc(sizeof(struct sg_table), GFP_KERNEL); |
| 1271 | if (!table) |
| 1272 | return ERR_PTR(-ENOMEM); |
| 1273 | |
| 1274 | n_chunks = DIV_ROUND_UP(total_size, chunk_size); |
| 1275 | pr_debug("creating sg_table with %d chunks\n", n_chunks); |
| 1276 | |
| 1277 | ret = sg_alloc_table(table, n_chunks, GFP_KERNEL); |
| 1278 | if (ret) |
| 1279 | goto err0; |
| 1280 | |
| 1281 | for_each_sg(table->sgl, sg, table->nents, i) { |
| 1282 | dma_addr_t addr = buffer_base + i * chunk_size; |
| 1283 | sg_dma_address(sg) = addr; |
Olav Haugan | bbdc30a | 2013-03-30 06:48:35 -0700 | [diff] [blame] | 1284 | sg_dma_len(sg) = chunk_size; |
Mitchel Humpherys | 0432d69 | 2013-01-08 17:03:10 -0800 | [diff] [blame] | 1285 | } |
| 1286 | |
| 1287 | return table; |
| 1288 | err0: |
| 1289 | kfree(table); |
| 1290 | return ERR_PTR(ret); |
| 1291 | } |
| 1292 | |
Rebecca Schultz Zavin | b179067 | 2012-06-14 15:08:53 -0700 | [diff] [blame] | 1293 | static void ion_buffer_sync_for_device(struct ion_buffer *buffer, |
| 1294 | struct device *dev, |
| 1295 | enum dma_data_direction direction); |
| 1296 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 1297 | static struct sg_table *ion_map_dma_buf(struct dma_buf_attachment *attachment, |
| 1298 | enum dma_data_direction direction) |
| 1299 | { |
| 1300 | struct dma_buf *dmabuf = attachment->dmabuf; |
| 1301 | struct ion_buffer *buffer = dmabuf->priv; |
| 1302 | |
Rebecca Schultz Zavin | f441922 | 2012-06-26 13:17:34 -0700 | [diff] [blame] | 1303 | ion_buffer_sync_for_device(buffer, attachment->dev, direction); |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 1304 | return buffer->sg_table; |
| 1305 | } |
| 1306 | |
| 1307 | static void ion_unmap_dma_buf(struct dma_buf_attachment *attachment, |
| 1308 | struct sg_table *table, |
| 1309 | enum dma_data_direction direction) |
| 1310 | { |
Rebecca Schultz Zavin | 043a614 | 2012-02-01 11:09:46 -0800 | [diff] [blame] | 1311 | } |
| 1312 | |
Rebecca Schultz Zavin | b179067 | 2012-06-14 15:08:53 -0700 | [diff] [blame] | 1313 | static int ion_buffer_alloc_dirty(struct ion_buffer *buffer) |
| 1314 | { |
| 1315 | unsigned long pages = buffer->sg_table->nents; |
| 1316 | unsigned long length = (pages + BITS_PER_LONG - 1)/BITS_PER_LONG; |
| 1317 | |
| 1318 | buffer->dirty = kzalloc(length * sizeof(unsigned long), GFP_KERNEL); |
| 1319 | if (!buffer->dirty) |
| 1320 | return -ENOMEM; |
| 1321 | return 0; |
| 1322 | } |
| 1323 | |
| 1324 | struct ion_vma_list { |
| 1325 | struct list_head list; |
| 1326 | struct vm_area_struct *vma; |
| 1327 | }; |
| 1328 | |
| 1329 | static void ion_buffer_sync_for_device(struct ion_buffer *buffer, |
| 1330 | struct device *dev, |
| 1331 | enum dma_data_direction dir) |
| 1332 | { |
| 1333 | struct scatterlist *sg; |
| 1334 | int i; |
| 1335 | struct ion_vma_list *vma_list; |
| 1336 | |
| 1337 | pr_debug("%s: syncing for device %s\n", __func__, |
| 1338 | dev ? dev_name(dev) : "null"); |
Rebecca Schultz Zavin | f441922 | 2012-06-26 13:17:34 -0700 | [diff] [blame] | 1339 | |
Rebecca Schultz Zavin | f858ba4 | 2012-09-21 11:46:06 -0700 | [diff] [blame] | 1340 | if (!ion_buffer_fault_user_mappings(buffer)) |
Rebecca Schultz Zavin | f441922 | 2012-06-26 13:17:34 -0700 | [diff] [blame] | 1341 | return; |
| 1342 | |
Rebecca Schultz Zavin | b179067 | 2012-06-14 15:08:53 -0700 | [diff] [blame] | 1343 | mutex_lock(&buffer->lock); |
| 1344 | for_each_sg(buffer->sg_table->sgl, sg, buffer->sg_table->nents, i) { |
| 1345 | if (!test_bit(i, buffer->dirty)) |
| 1346 | continue; |
| 1347 | dma_sync_sg_for_device(dev, sg, 1, dir); |
| 1348 | clear_bit(i, buffer->dirty); |
| 1349 | } |
| 1350 | list_for_each_entry(vma_list, &buffer->vmas, list) { |
| 1351 | struct vm_area_struct *vma = vma_list->vma; |
| 1352 | |
| 1353 | zap_page_range(vma, vma->vm_start, vma->vm_end - vma->vm_start, |
| 1354 | NULL); |
| 1355 | } |
| 1356 | mutex_unlock(&buffer->lock); |
| 1357 | } |
| 1358 | |
| 1359 | int ion_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1360 | { |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 1361 | struct ion_buffer *buffer = vma->vm_private_data; |
Rebecca Schultz Zavin | b179067 | 2012-06-14 15:08:53 -0700 | [diff] [blame] | 1362 | struct scatterlist *sg; |
| 1363 | int i; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1364 | |
Rebecca Schultz Zavin | b179067 | 2012-06-14 15:08:53 -0700 | [diff] [blame] | 1365 | mutex_lock(&buffer->lock); |
| 1366 | set_bit(vmf->pgoff, buffer->dirty); |
| 1367 | |
| 1368 | for_each_sg(buffer->sg_table->sgl, sg, buffer->sg_table->nents, i) { |
| 1369 | if (i != vmf->pgoff) |
| 1370 | continue; |
| 1371 | dma_sync_sg_for_cpu(NULL, sg, 1, DMA_BIDIRECTIONAL); |
| 1372 | vm_insert_page(vma, (unsigned long)vmf->virtual_address, |
| 1373 | sg_page(sg)); |
| 1374 | break; |
| 1375 | } |
| 1376 | mutex_unlock(&buffer->lock); |
| 1377 | return VM_FAULT_NOPAGE; |
| 1378 | } |
| 1379 | |
| 1380 | static void ion_vm_open(struct vm_area_struct *vma) |
| 1381 | { |
| 1382 | struct ion_buffer *buffer = vma->vm_private_data; |
| 1383 | struct ion_vma_list *vma_list; |
| 1384 | |
| 1385 | vma_list = kmalloc(sizeof(struct ion_vma_list), GFP_KERNEL); |
| 1386 | if (!vma_list) |
| 1387 | return; |
| 1388 | vma_list->vma = vma; |
| 1389 | mutex_lock(&buffer->lock); |
| 1390 | list_add(&vma_list->list, &buffer->vmas); |
| 1391 | mutex_unlock(&buffer->lock); |
| 1392 | pr_debug("%s: adding %p\n", __func__, vma); |
| 1393 | } |
| 1394 | |
| 1395 | static void ion_vm_close(struct vm_area_struct *vma) |
| 1396 | { |
| 1397 | struct ion_buffer *buffer = vma->vm_private_data; |
| 1398 | struct ion_vma_list *vma_list, *tmp; |
| 1399 | |
| 1400 | pr_debug("%s\n", __func__); |
| 1401 | mutex_lock(&buffer->lock); |
| 1402 | list_for_each_entry_safe(vma_list, tmp, &buffer->vmas, list) { |
| 1403 | if (vma_list->vma != vma) |
| 1404 | continue; |
| 1405 | list_del(&vma_list->list); |
| 1406 | kfree(vma_list); |
| 1407 | pr_debug("%s: deleting %p\n", __func__, vma); |
| 1408 | break; |
| 1409 | } |
| 1410 | mutex_unlock(&buffer->lock); |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 1411 | |
Laura Abbott | a683509 | 2011-11-14 15:27:02 -0800 | [diff] [blame] | 1412 | if (buffer->heap->ops->unmap_user) |
| 1413 | buffer->heap->ops->unmap_user(buffer->heap, buffer); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1414 | } |
| 1415 | |
Rebecca Schultz Zavin | b179067 | 2012-06-14 15:08:53 -0700 | [diff] [blame] | 1416 | struct vm_operations_struct ion_vma_ops = { |
| 1417 | .open = ion_vm_open, |
| 1418 | .close = ion_vm_close, |
| 1419 | .fault = ion_vm_fault, |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1420 | }; |
| 1421 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 1422 | static int ion_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma) |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1423 | { |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 1424 | struct ion_buffer *buffer = dmabuf->priv; |
Rebecca Schultz Zavin | b179067 | 2012-06-14 15:08:53 -0700 | [diff] [blame] | 1425 | int ret = 0; |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1426 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 1427 | if (!buffer->heap->ops->map_user) { |
| 1428 | pr_err("%s: this heap does not define a method for mapping " |
| 1429 | "to userspace\n", __func__); |
Rebecca Schultz Zavin | 043a614 | 2012-02-01 11:09:46 -0800 | [diff] [blame] | 1430 | return -EINVAL; |
| 1431 | } |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1432 | |
Rebecca Schultz Zavin | f858ba4 | 2012-09-21 11:46:06 -0700 | [diff] [blame] | 1433 | if (ion_buffer_fault_user_mappings(buffer)) { |
Rebecca Schultz Zavin | b179067 | 2012-06-14 15:08:53 -0700 | [diff] [blame] | 1434 | vma->vm_private_data = buffer; |
| 1435 | vma->vm_ops = &ion_vma_ops; |
Mitchel Humpherys | 7ce0fe4 | 2013-01-10 11:30:26 -0800 | [diff] [blame] | 1436 | vma->vm_flags |= VM_MIXEDMAP; |
Rebecca Schultz Zavin | b179067 | 2012-06-14 15:08:53 -0700 | [diff] [blame] | 1437 | ion_vm_open(vma); |
Rebecca Schultz Zavin | 3edb900 | 2012-09-19 23:31:05 -0700 | [diff] [blame] | 1438 | return 0; |
Rebecca Schultz Zavin | b179067 | 2012-06-14 15:08:53 -0700 | [diff] [blame] | 1439 | } |
Laura Abbott | e8bc7aa | 2011-12-09 14:49:33 -0800 | [diff] [blame] | 1440 | |
Rebecca Schultz Zavin | 3edb900 | 2012-09-19 23:31:05 -0700 | [diff] [blame] | 1441 | if (!(buffer->flags & ION_FLAG_CACHED)) |
| 1442 | vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot); |
| 1443 | |
| 1444 | mutex_lock(&buffer->lock); |
| 1445 | /* now map it to userspace */ |
| 1446 | ret = buffer->heap->ops->map_user(buffer->heap, buffer, vma); |
| 1447 | mutex_unlock(&buffer->lock); |
| 1448 | |
Rebecca Schultz Zavin | b179067 | 2012-06-14 15:08:53 -0700 | [diff] [blame] | 1449 | if (ret) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1450 | pr_err("%s: failure mapping buffer to userspace\n", |
| 1451 | __func__); |
Rebecca Schultz Zavin | b179067 | 2012-06-14 15:08:53 -0700 | [diff] [blame] | 1452 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1453 | return ret; |
Rebecca Schultz Zavin | 043a614 | 2012-02-01 11:09:46 -0800 | [diff] [blame] | 1454 | } |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1455 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 1456 | static void ion_dma_buf_release(struct dma_buf *dmabuf) |
| 1457 | { |
| 1458 | struct ion_buffer *buffer = dmabuf->priv; |
| 1459 | ion_buffer_put(buffer); |
| 1460 | } |
| 1461 | |
| 1462 | static void *ion_dma_buf_kmap(struct dma_buf *dmabuf, unsigned long offset) |
| 1463 | { |
| 1464 | struct ion_buffer *buffer = dmabuf->priv; |
| 1465 | return buffer->vaddr + offset; |
| 1466 | } |
| 1467 | |
| 1468 | static void ion_dma_buf_kunmap(struct dma_buf *dmabuf, unsigned long offset, |
| 1469 | void *ptr) |
| 1470 | { |
| 1471 | return; |
| 1472 | } |
| 1473 | |
| 1474 | static int ion_dma_buf_begin_cpu_access(struct dma_buf *dmabuf, size_t start, |
| 1475 | size_t len, |
| 1476 | enum dma_data_direction direction) |
| 1477 | { |
| 1478 | struct ion_buffer *buffer = dmabuf->priv; |
| 1479 | void *vaddr; |
| 1480 | |
| 1481 | if (!buffer->heap->ops->map_kernel) { |
| 1482 | pr_err("%s: map kernel is not implemented by this heap.\n", |
| 1483 | __func__); |
| 1484 | return -ENODEV; |
| 1485 | } |
| 1486 | |
| 1487 | mutex_lock(&buffer->lock); |
| 1488 | vaddr = ion_buffer_kmap_get(buffer); |
| 1489 | mutex_unlock(&buffer->lock); |
| 1490 | if (IS_ERR(vaddr)) |
| 1491 | return PTR_ERR(vaddr); |
| 1492 | if (!vaddr) |
| 1493 | return -ENOMEM; |
| 1494 | return 0; |
| 1495 | } |
| 1496 | |
| 1497 | static void ion_dma_buf_end_cpu_access(struct dma_buf *dmabuf, size_t start, |
| 1498 | size_t len, |
| 1499 | enum dma_data_direction direction) |
| 1500 | { |
| 1501 | struct ion_buffer *buffer = dmabuf->priv; |
| 1502 | |
| 1503 | mutex_lock(&buffer->lock); |
| 1504 | ion_buffer_kmap_put(buffer); |
| 1505 | mutex_unlock(&buffer->lock); |
| 1506 | } |
| 1507 | |
| 1508 | struct dma_buf_ops dma_buf_ops = { |
| 1509 | .map_dma_buf = ion_map_dma_buf, |
| 1510 | .unmap_dma_buf = ion_unmap_dma_buf, |
| 1511 | .mmap = ion_mmap, |
| 1512 | .release = ion_dma_buf_release, |
| 1513 | .begin_cpu_access = ion_dma_buf_begin_cpu_access, |
| 1514 | .end_cpu_access = ion_dma_buf_end_cpu_access, |
| 1515 | .kmap_atomic = ion_dma_buf_kmap, |
| 1516 | .kunmap_atomic = ion_dma_buf_kunmap, |
| 1517 | .kmap = ion_dma_buf_kmap, |
| 1518 | .kunmap = ion_dma_buf_kunmap, |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1519 | }; |
| 1520 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 1521 | int ion_share_dma_buf(struct ion_client *client, struct ion_handle *handle) |
| 1522 | { |
| 1523 | struct ion_buffer *buffer; |
| 1524 | struct dma_buf *dmabuf; |
| 1525 | bool valid_handle; |
| 1526 | int fd; |
| 1527 | |
| 1528 | mutex_lock(&client->lock); |
| 1529 | valid_handle = ion_handle_validate(client, handle); |
| 1530 | mutex_unlock(&client->lock); |
| 1531 | if (!valid_handle) { |
Olav Haugan | 0df5994 | 2012-07-05 14:27:30 -0700 | [diff] [blame] | 1532 | WARN(1, "%s: invalid handle passed to share.\n", __func__); |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 1533 | return -EINVAL; |
| 1534 | } |
| 1535 | |
| 1536 | buffer = handle->buffer; |
| 1537 | ion_buffer_get(buffer); |
| 1538 | dmabuf = dma_buf_export(buffer, &dma_buf_ops, buffer->size, O_RDWR); |
| 1539 | if (IS_ERR(dmabuf)) { |
| 1540 | ion_buffer_put(buffer); |
| 1541 | return PTR_ERR(dmabuf); |
| 1542 | } |
| 1543 | fd = dma_buf_fd(dmabuf, O_CLOEXEC); |
Laura Abbott | c2641f7 | 2012-08-01 18:06:18 -0700 | [diff] [blame] | 1544 | if (fd < 0) |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 1545 | dma_buf_put(dmabuf); |
Laura Abbott | c2641f7 | 2012-08-01 18:06:18 -0700 | [diff] [blame] | 1546 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1547 | return fd; |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 1548 | } |
Olav Haugan | bd453a9 | 2012-07-05 14:21:34 -0700 | [diff] [blame] | 1549 | EXPORT_SYMBOL(ion_share_dma_buf); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1550 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 1551 | struct ion_handle *ion_import_dma_buf(struct ion_client *client, int fd) |
| 1552 | { |
| 1553 | struct dma_buf *dmabuf; |
| 1554 | struct ion_buffer *buffer; |
| 1555 | struct ion_handle *handle; |
| 1556 | |
| 1557 | dmabuf = dma_buf_get(fd); |
| 1558 | if (IS_ERR_OR_NULL(dmabuf)) |
| 1559 | return ERR_PTR(PTR_ERR(dmabuf)); |
| 1560 | /* if this memory came from ion */ |
| 1561 | |
| 1562 | if (dmabuf->ops != &dma_buf_ops) { |
| 1563 | pr_err("%s: can not import dmabuf from another exporter\n", |
| 1564 | __func__); |
| 1565 | dma_buf_put(dmabuf); |
| 1566 | return ERR_PTR(-EINVAL); |
| 1567 | } |
| 1568 | buffer = dmabuf->priv; |
| 1569 | |
| 1570 | mutex_lock(&client->lock); |
| 1571 | /* if a handle exists for this buffer just take a reference to it */ |
| 1572 | handle = ion_handle_lookup(client, buffer); |
| 1573 | if (!IS_ERR_OR_NULL(handle)) { |
| 1574 | ion_handle_get(handle); |
| 1575 | goto end; |
| 1576 | } |
| 1577 | handle = ion_handle_create(client, buffer); |
| 1578 | if (IS_ERR_OR_NULL(handle)) |
| 1579 | goto end; |
| 1580 | ion_handle_add(client, handle); |
| 1581 | end: |
| 1582 | mutex_unlock(&client->lock); |
| 1583 | dma_buf_put(dmabuf); |
| 1584 | return handle; |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1585 | } |
Olav Haugan | bd453a9 | 2012-07-05 14:21:34 -0700 | [diff] [blame] | 1586 | EXPORT_SYMBOL(ion_import_dma_buf); |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1587 | |
Rebecca Schultz Zavin | f441922 | 2012-06-26 13:17:34 -0700 | [diff] [blame] | 1588 | static int ion_sync_for_device(struct ion_client *client, int fd) |
| 1589 | { |
| 1590 | struct dma_buf *dmabuf; |
| 1591 | struct ion_buffer *buffer; |
| 1592 | |
| 1593 | dmabuf = dma_buf_get(fd); |
| 1594 | if (IS_ERR_OR_NULL(dmabuf)) |
| 1595 | return PTR_ERR(dmabuf); |
| 1596 | |
| 1597 | /* if this memory came from ion */ |
| 1598 | if (dmabuf->ops != &dma_buf_ops) { |
| 1599 | pr_err("%s: can not sync dmabuf from another exporter\n", |
| 1600 | __func__); |
| 1601 | dma_buf_put(dmabuf); |
| 1602 | return -EINVAL; |
| 1603 | } |
| 1604 | buffer = dmabuf->priv; |
Rebecca Schultz Zavin | 3edb900 | 2012-09-19 23:31:05 -0700 | [diff] [blame] | 1605 | |
| 1606 | dma_sync_sg_for_device(NULL, buffer->sg_table->sgl, |
| 1607 | buffer->sg_table->nents, DMA_BIDIRECTIONAL); |
Rebecca Schultz Zavin | f441922 | 2012-06-26 13:17:34 -0700 | [diff] [blame] | 1608 | dma_buf_put(dmabuf); |
| 1609 | return 0; |
| 1610 | } |
| 1611 | |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1612 | static long ion_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
| 1613 | { |
| 1614 | struct ion_client *client = filp->private_data; |
| 1615 | |
| 1616 | switch (cmd) { |
| 1617 | case ION_IOC_ALLOC: |
| 1618 | { |
| 1619 | struct ion_allocation_data data; |
| 1620 | |
| 1621 | if (copy_from_user(&data, (void __user *)arg, sizeof(data))) |
| 1622 | return -EFAULT; |
| 1623 | data.handle = ion_alloc(client, data.len, data.align, |
Hanumant Singh | 7d72bad | 2012-08-29 18:39:44 -0700 | [diff] [blame] | 1624 | data.heap_mask, data.flags); |
KyongHo Cho | 9ae7e01 | 2011-09-07 11:27:07 +0900 | [diff] [blame] | 1625 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 1626 | if (IS_ERR(data.handle)) |
| 1627 | return PTR_ERR(data.handle); |
KyongHo Cho | 9ae7e01 | 2011-09-07 11:27:07 +0900 | [diff] [blame] | 1628 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 1629 | if (copy_to_user((void __user *)arg, &data, sizeof(data))) { |
| 1630 | ion_free(client, data.handle); |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1631 | return -EFAULT; |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 1632 | } |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1633 | break; |
| 1634 | } |
| 1635 | case ION_IOC_FREE: |
| 1636 | { |
| 1637 | struct ion_handle_data data; |
| 1638 | bool valid; |
| 1639 | |
| 1640 | if (copy_from_user(&data, (void __user *)arg, |
| 1641 | sizeof(struct ion_handle_data))) |
| 1642 | return -EFAULT; |
| 1643 | mutex_lock(&client->lock); |
| 1644 | valid = ion_handle_validate(client, data.handle); |
| 1645 | mutex_unlock(&client->lock); |
| 1646 | if (!valid) |
| 1647 | return -EINVAL; |
| 1648 | ion_free(client, data.handle); |
| 1649 | break; |
| 1650 | } |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1651 | case ION_IOC_MAP: |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1652 | case ION_IOC_SHARE: |
| 1653 | { |
| 1654 | struct ion_fd_data data; |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1655 | if (copy_from_user(&data, (void __user *)arg, sizeof(data))) |
| 1656 | return -EFAULT; |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 1657 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 1658 | data.fd = ion_share_dma_buf(client, data.handle); |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1659 | if (copy_to_user((void __user *)arg, &data, sizeof(data))) |
| 1660 | return -EFAULT; |
Olav Haugan | c2d2cf5 | 2012-05-15 14:40:11 -0700 | [diff] [blame] | 1661 | if (data.fd < 0) |
| 1662 | return data.fd; |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1663 | break; |
| 1664 | } |
| 1665 | case ION_IOC_IMPORT: |
| 1666 | { |
| 1667 | struct ion_fd_data data; |
Olav Haugan | c2d2cf5 | 2012-05-15 14:40:11 -0700 | [diff] [blame] | 1668 | int ret = 0; |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1669 | if (copy_from_user(&data, (void __user *)arg, |
| 1670 | sizeof(struct ion_fd_data))) |
| 1671 | return -EFAULT; |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 1672 | data.handle = ion_import_dma_buf(client, data.fd); |
Olav Haugan | 865e97f | 2012-05-15 14:40:11 -0700 | [diff] [blame] | 1673 | if (IS_ERR(data.handle)) { |
| 1674 | ret = PTR_ERR(data.handle); |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1675 | data.handle = NULL; |
Olav Haugan | 865e97f | 2012-05-15 14:40:11 -0700 | [diff] [blame] | 1676 | } |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1677 | if (copy_to_user((void __user *)arg, &data, |
| 1678 | sizeof(struct ion_fd_data))) |
| 1679 | return -EFAULT; |
Olav Haugan | c2d2cf5 | 2012-05-15 14:40:11 -0700 | [diff] [blame] | 1680 | if (ret < 0) |
| 1681 | return ret; |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1682 | break; |
| 1683 | } |
Rebecca Schultz Zavin | f441922 | 2012-06-26 13:17:34 -0700 | [diff] [blame] | 1684 | case ION_IOC_SYNC: |
| 1685 | { |
| 1686 | struct ion_fd_data data; |
| 1687 | if (copy_from_user(&data, (void __user *)arg, |
| 1688 | sizeof(struct ion_fd_data))) |
| 1689 | return -EFAULT; |
| 1690 | ion_sync_for_device(client, data.fd); |
| 1691 | break; |
| 1692 | } |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1693 | case ION_IOC_CUSTOM: |
| 1694 | { |
| 1695 | struct ion_device *dev = client->dev; |
| 1696 | struct ion_custom_data data; |
| 1697 | |
| 1698 | if (!dev->custom_ioctl) |
| 1699 | return -ENOTTY; |
| 1700 | if (copy_from_user(&data, (void __user *)arg, |
| 1701 | sizeof(struct ion_custom_data))) |
| 1702 | return -EFAULT; |
| 1703 | return dev->custom_ioctl(client, data.cmd, data.arg); |
| 1704 | } |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 1705 | case ION_IOC_CLEAN_CACHES: |
Mitchel Humpherys | d88b8eb | 2012-09-04 17:00:29 -0700 | [diff] [blame] | 1706 | return client->dev->custom_ioctl(client, |
| 1707 | ION_IOC_CLEAN_CACHES, arg); |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 1708 | case ION_IOC_INV_CACHES: |
Mitchel Humpherys | d88b8eb | 2012-09-04 17:00:29 -0700 | [diff] [blame] | 1709 | return client->dev->custom_ioctl(client, |
| 1710 | ION_IOC_INV_CACHES, arg); |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 1711 | case ION_IOC_CLEAN_INV_CACHES: |
Mitchel Humpherys | d88b8eb | 2012-09-04 17:00:29 -0700 | [diff] [blame] | 1712 | return client->dev->custom_ioctl(client, |
| 1713 | ION_IOC_CLEAN_INV_CACHES, arg); |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1714 | default: |
| 1715 | return -ENOTTY; |
| 1716 | } |
| 1717 | return 0; |
| 1718 | } |
| 1719 | |
| 1720 | static int ion_release(struct inode *inode, struct file *file) |
| 1721 | { |
| 1722 | struct ion_client *client = file->private_data; |
| 1723 | |
| 1724 | pr_debug("%s: %d\n", __func__, __LINE__); |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 1725 | ion_client_destroy(client); |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1726 | return 0; |
| 1727 | } |
| 1728 | |
| 1729 | static int ion_open(struct inode *inode, struct file *file) |
| 1730 | { |
| 1731 | struct miscdevice *miscdev = file->private_data; |
| 1732 | struct ion_device *dev = container_of(miscdev, struct ion_device, dev); |
| 1733 | struct ion_client *client; |
Laura Abbott | eed8603 | 2011-12-05 15:32:36 -0800 | [diff] [blame] | 1734 | char debug_name[64]; |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1735 | |
| 1736 | pr_debug("%s: %d\n", __func__, __LINE__); |
Laura Abbott | eed8603 | 2011-12-05 15:32:36 -0800 | [diff] [blame] | 1737 | snprintf(debug_name, 64, "%u", task_pid_nr(current->group_leader)); |
| 1738 | client = ion_client_create(dev, -1, debug_name); |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1739 | if (IS_ERR_OR_NULL(client)) |
| 1740 | return PTR_ERR(client); |
| 1741 | file->private_data = client; |
| 1742 | |
| 1743 | return 0; |
| 1744 | } |
| 1745 | |
| 1746 | static const struct file_operations ion_fops = { |
| 1747 | .owner = THIS_MODULE, |
| 1748 | .open = ion_open, |
| 1749 | .release = ion_release, |
| 1750 | .unlocked_ioctl = ion_ioctl, |
| 1751 | }; |
| 1752 | |
| 1753 | static size_t ion_debug_heap_total(struct ion_client *client, |
Laura Abbott | 3647ac3 | 2011-10-31 14:09:53 -0700 | [diff] [blame] | 1754 | enum ion_heap_ids id) |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1755 | { |
| 1756 | size_t size = 0; |
| 1757 | struct rb_node *n; |
| 1758 | |
| 1759 | mutex_lock(&client->lock); |
| 1760 | for (n = rb_first(&client->handles); n; n = rb_next(n)) { |
| 1761 | struct ion_handle *handle = rb_entry(n, |
| 1762 | struct ion_handle, |
| 1763 | node); |
Laura Abbott | 3647ac3 | 2011-10-31 14:09:53 -0700 | [diff] [blame] | 1764 | if (handle->buffer->heap->id == id) |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1765 | size += handle->buffer->size; |
| 1766 | } |
| 1767 | mutex_unlock(&client->lock); |
| 1768 | return size; |
| 1769 | } |
| 1770 | |
Olav Haugan | 0671b9a | 2012-05-25 11:58:56 -0700 | [diff] [blame] | 1771 | /** |
| 1772 | * Searches through a clients handles to find if the buffer is owned |
| 1773 | * by this client. Used for debug output. |
| 1774 | * @param client pointer to candidate owner of buffer |
| 1775 | * @param buf pointer to buffer that we are trying to find the owner of |
| 1776 | * @return 1 if found, 0 otherwise |
| 1777 | */ |
| 1778 | static int ion_debug_find_buffer_owner(const struct ion_client *client, |
| 1779 | const struct ion_buffer *buf) |
| 1780 | { |
| 1781 | struct rb_node *n; |
| 1782 | |
| 1783 | for (n = rb_first(&client->handles); n; n = rb_next(n)) { |
| 1784 | const struct ion_handle *handle = rb_entry(n, |
| 1785 | const struct ion_handle, |
| 1786 | node); |
| 1787 | if (handle->buffer == buf) |
| 1788 | return 1; |
| 1789 | } |
| 1790 | return 0; |
| 1791 | } |
| 1792 | |
| 1793 | /** |
| 1794 | * Adds mem_map_data pointer to the tree of mem_map |
| 1795 | * Used for debug output. |
| 1796 | * @param mem_map The mem_map tree |
| 1797 | * @param data The new data to add to the tree |
| 1798 | */ |
| 1799 | static void ion_debug_mem_map_add(struct rb_root *mem_map, |
| 1800 | struct mem_map_data *data) |
| 1801 | { |
| 1802 | struct rb_node **p = &mem_map->rb_node; |
| 1803 | struct rb_node *parent = NULL; |
| 1804 | struct mem_map_data *entry; |
| 1805 | |
| 1806 | while (*p) { |
| 1807 | parent = *p; |
| 1808 | entry = rb_entry(parent, struct mem_map_data, node); |
| 1809 | |
| 1810 | if (data->addr < entry->addr) { |
| 1811 | p = &(*p)->rb_left; |
| 1812 | } else if (data->addr > entry->addr) { |
| 1813 | p = &(*p)->rb_right; |
| 1814 | } else { |
| 1815 | pr_err("%s: mem_map_data already found.", __func__); |
| 1816 | BUG(); |
| 1817 | } |
| 1818 | } |
| 1819 | rb_link_node(&data->node, parent, p); |
| 1820 | rb_insert_color(&data->node, mem_map); |
| 1821 | } |
| 1822 | |
| 1823 | /** |
| 1824 | * Search for an owner of a buffer by iterating over all ION clients. |
| 1825 | * @param dev ion device containing pointers to all the clients. |
| 1826 | * @param buffer pointer to buffer we are trying to find the owner of. |
| 1827 | * @return name of owner. |
| 1828 | */ |
| 1829 | const char *ion_debug_locate_owner(const struct ion_device *dev, |
| 1830 | const struct ion_buffer *buffer) |
| 1831 | { |
| 1832 | struct rb_node *j; |
| 1833 | const char *client_name = NULL; |
| 1834 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 1835 | for (j = rb_first(&dev->clients); j && !client_name; |
Olav Haugan | 0671b9a | 2012-05-25 11:58:56 -0700 | [diff] [blame] | 1836 | j = rb_next(j)) { |
| 1837 | struct ion_client *client = rb_entry(j, struct ion_client, |
| 1838 | node); |
| 1839 | if (ion_debug_find_buffer_owner(client, buffer)) |
| 1840 | client_name = client->name; |
| 1841 | } |
| 1842 | return client_name; |
| 1843 | } |
| 1844 | |
| 1845 | /** |
| 1846 | * Create a mem_map of the heap. |
| 1847 | * @param s seq_file to log error message to. |
| 1848 | * @param heap The heap to create mem_map for. |
| 1849 | * @param mem_map The mem map to be created. |
| 1850 | */ |
| 1851 | void ion_debug_mem_map_create(struct seq_file *s, struct ion_heap *heap, |
| 1852 | struct rb_root *mem_map) |
| 1853 | { |
| 1854 | struct ion_device *dev = heap->dev; |
| 1855 | struct rb_node *n; |
Chintan Pandya | daf7562 | 2013-01-29 19:40:01 +0530 | [diff] [blame] | 1856 | size_t size; |
| 1857 | |
| 1858 | if (!heap->ops->phys) |
| 1859 | return; |
Olav Haugan | 0671b9a | 2012-05-25 11:58:56 -0700 | [diff] [blame] | 1860 | |
| 1861 | for (n = rb_first(&dev->buffers); n; n = rb_next(n)) { |
| 1862 | struct ion_buffer *buffer = |
| 1863 | rb_entry(n, struct ion_buffer, node); |
| 1864 | if (buffer->heap->id == heap->id) { |
| 1865 | struct mem_map_data *data = |
| 1866 | kzalloc(sizeof(*data), GFP_KERNEL); |
| 1867 | if (!data) { |
| 1868 | seq_printf(s, "ERROR: out of memory. " |
| 1869 | "Part of memory map will not be logged\n"); |
| 1870 | break; |
| 1871 | } |
Chintan Pandya | daf7562 | 2013-01-29 19:40:01 +0530 | [diff] [blame] | 1872 | |
| 1873 | buffer->heap->ops->phys(buffer->heap, buffer, |
| 1874 | &(data->addr), &size); |
| 1875 | data->size = (unsigned long) size; |
| 1876 | data->addr_end = data->addr + data->size - 1; |
Olav Haugan | 0671b9a | 2012-05-25 11:58:56 -0700 | [diff] [blame] | 1877 | data->client_name = ion_debug_locate_owner(dev, buffer); |
| 1878 | ion_debug_mem_map_add(mem_map, data); |
| 1879 | } |
| 1880 | } |
| 1881 | } |
| 1882 | |
| 1883 | /** |
| 1884 | * Free the memory allocated by ion_debug_mem_map_create |
| 1885 | * @param mem_map The mem map to free. |
| 1886 | */ |
| 1887 | static void ion_debug_mem_map_destroy(struct rb_root *mem_map) |
| 1888 | { |
| 1889 | if (mem_map) { |
| 1890 | struct rb_node *n; |
| 1891 | while ((n = rb_first(mem_map)) != 0) { |
| 1892 | struct mem_map_data *data = |
| 1893 | rb_entry(n, struct mem_map_data, node); |
| 1894 | rb_erase(&data->node, mem_map); |
| 1895 | kfree(data); |
| 1896 | } |
| 1897 | } |
| 1898 | } |
| 1899 | |
| 1900 | /** |
| 1901 | * Print heap debug information. |
| 1902 | * @param s seq_file to log message to. |
| 1903 | * @param heap pointer to heap that we will print debug information for. |
| 1904 | */ |
| 1905 | static void ion_heap_print_debug(struct seq_file *s, struct ion_heap *heap) |
| 1906 | { |
| 1907 | if (heap->ops->print_debug) { |
| 1908 | struct rb_root mem_map = RB_ROOT; |
| 1909 | ion_debug_mem_map_create(s, heap, &mem_map); |
| 1910 | heap->ops->print_debug(heap, s, &mem_map); |
| 1911 | ion_debug_mem_map_destroy(&mem_map); |
| 1912 | } |
| 1913 | } |
| 1914 | |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1915 | static int ion_debug_heap_show(struct seq_file *s, void *unused) |
| 1916 | { |
| 1917 | struct ion_heap *heap = s->private; |
| 1918 | struct ion_device *dev = heap->dev; |
| 1919 | struct rb_node *n; |
| 1920 | |
Olav Haugan | e4900b5 | 2012-05-25 11:58:03 -0700 | [diff] [blame] | 1921 | mutex_lock(&dev->lock); |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1922 | seq_printf(s, "%16.s %16.s %16.s\n", "client", "pid", "size"); |
Rebecca Schultz Zavin | 043a614 | 2012-02-01 11:09:46 -0800 | [diff] [blame] | 1923 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 1924 | for (n = rb_first(&dev->clients); n; n = rb_next(n)) { |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1925 | struct ion_client *client = rb_entry(n, struct ion_client, |
| 1926 | node); |
Laura Abbott | 3647ac3 | 2011-10-31 14:09:53 -0700 | [diff] [blame] | 1927 | size_t size = ion_debug_heap_total(client, heap->id); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1928 | if (!size) |
| 1929 | continue; |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 1930 | if (client->task) { |
| 1931 | char task_comm[TASK_COMM_LEN]; |
| 1932 | |
| 1933 | get_task_comm(task_comm, client->task); |
| 1934 | seq_printf(s, "%16.s %16u %16u\n", task_comm, |
| 1935 | client->pid, size); |
| 1936 | } else { |
| 1937 | seq_printf(s, "%16.s %16u %16u\n", client->name, |
| 1938 | client->pid, size); |
| 1939 | } |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1940 | } |
Olav Haugan | 0671b9a | 2012-05-25 11:58:56 -0700 | [diff] [blame] | 1941 | ion_heap_print_debug(s, heap); |
Olav Haugan | e4900b5 | 2012-05-25 11:58:03 -0700 | [diff] [blame] | 1942 | mutex_unlock(&dev->lock); |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1943 | return 0; |
| 1944 | } |
| 1945 | |
| 1946 | static int ion_debug_heap_open(struct inode *inode, struct file *file) |
| 1947 | { |
| 1948 | return single_open(file, ion_debug_heap_show, inode->i_private); |
| 1949 | } |
| 1950 | |
| 1951 | static const struct file_operations debug_heap_fops = { |
| 1952 | .open = ion_debug_heap_open, |
| 1953 | .read = seq_read, |
| 1954 | .llseek = seq_lseek, |
| 1955 | .release = single_release, |
| 1956 | }; |
| 1957 | |
| 1958 | void ion_device_add_heap(struct ion_device *dev, struct ion_heap *heap) |
| 1959 | { |
| 1960 | struct rb_node **p = &dev->heaps.rb_node; |
| 1961 | struct rb_node *parent = NULL; |
| 1962 | struct ion_heap *entry; |
| 1963 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 1964 | if (!heap->ops->allocate || !heap->ops->free || !heap->ops->map_dma || |
| 1965 | !heap->ops->unmap_dma) |
| 1966 | pr_err("%s: can not add heap with invalid ops struct.\n", |
| 1967 | __func__); |
| 1968 | |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1969 | heap->dev = dev; |
| 1970 | mutex_lock(&dev->lock); |
| 1971 | while (*p) { |
| 1972 | parent = *p; |
| 1973 | entry = rb_entry(parent, struct ion_heap, node); |
| 1974 | |
| 1975 | if (heap->id < entry->id) { |
| 1976 | p = &(*p)->rb_left; |
| 1977 | } else if (heap->id > entry->id ) { |
| 1978 | p = &(*p)->rb_right; |
| 1979 | } else { |
| 1980 | pr_err("%s: can not insert multiple heaps with " |
| 1981 | "id %d\n", __func__, heap->id); |
| 1982 | goto end; |
| 1983 | } |
| 1984 | } |
| 1985 | |
| 1986 | rb_link_node(&heap->node, parent, p); |
| 1987 | rb_insert_color(&heap->node, &dev->heaps); |
| 1988 | debugfs_create_file(heap->name, 0664, dev->debug_root, heap, |
| 1989 | &debug_heap_fops); |
| 1990 | end: |
| 1991 | mutex_unlock(&dev->lock); |
| 1992 | } |
| 1993 | |
Laura Abbott | 9361930 | 2012-10-11 11:51:40 -0700 | [diff] [blame] | 1994 | int ion_secure_handle(struct ion_client *client, struct ion_handle *handle, |
| 1995 | int version, void *data, int flags) |
| 1996 | { |
| 1997 | int ret = -EINVAL; |
| 1998 | struct ion_heap *heap; |
| 1999 | struct ion_buffer *buffer; |
| 2000 | |
| 2001 | mutex_lock(&client->lock); |
| 2002 | if (!ion_handle_validate(client, handle)) { |
| 2003 | WARN(1, "%s: invalid handle passed to secure.\n", __func__); |
| 2004 | goto out_unlock; |
| 2005 | } |
| 2006 | |
| 2007 | buffer = handle->buffer; |
| 2008 | heap = buffer->heap; |
| 2009 | |
Laura Abbott | 4afbd8b | 2013-02-15 09:21:33 -0800 | [diff] [blame] | 2010 | if (!ion_heap_allow_handle_secure(heap->type)) { |
Laura Abbott | 9361930 | 2012-10-11 11:51:40 -0700 | [diff] [blame] | 2011 | pr_err("%s: cannot secure buffer from non secure heap\n", |
| 2012 | __func__); |
| 2013 | goto out_unlock; |
| 2014 | } |
| 2015 | |
| 2016 | BUG_ON(!buffer->heap->ops->secure_buffer); |
| 2017 | /* |
| 2018 | * Protect the handle via the client lock to ensure we aren't |
| 2019 | * racing with free |
| 2020 | */ |
| 2021 | ret = buffer->heap->ops->secure_buffer(buffer, version, data, flags); |
| 2022 | |
| 2023 | out_unlock: |
| 2024 | mutex_unlock(&client->lock); |
| 2025 | return ret; |
| 2026 | } |
| 2027 | |
| 2028 | int ion_unsecure_handle(struct ion_client *client, struct ion_handle *handle) |
| 2029 | { |
| 2030 | int ret = -EINVAL; |
| 2031 | struct ion_heap *heap; |
| 2032 | struct ion_buffer *buffer; |
| 2033 | |
| 2034 | mutex_lock(&client->lock); |
| 2035 | if (!ion_handle_validate(client, handle)) { |
| 2036 | WARN(1, "%s: invalid handle passed to secure.\n", __func__); |
| 2037 | goto out_unlock; |
| 2038 | } |
| 2039 | |
| 2040 | buffer = handle->buffer; |
| 2041 | heap = buffer->heap; |
| 2042 | |
Laura Abbott | 4afbd8b | 2013-02-15 09:21:33 -0800 | [diff] [blame] | 2043 | if (!ion_heap_allow_handle_secure(heap->type)) { |
Laura Abbott | 9361930 | 2012-10-11 11:51:40 -0700 | [diff] [blame] | 2044 | pr_err("%s: cannot secure buffer from non secure heap\n", |
| 2045 | __func__); |
| 2046 | goto out_unlock; |
| 2047 | } |
| 2048 | |
| 2049 | BUG_ON(!buffer->heap->ops->unsecure_buffer); |
| 2050 | /* |
| 2051 | * Protect the handle via the client lock to ensure we aren't |
| 2052 | * racing with free |
| 2053 | */ |
| 2054 | ret = buffer->heap->ops->unsecure_buffer(buffer, 0); |
| 2055 | |
| 2056 | out_unlock: |
| 2057 | mutex_unlock(&client->lock); |
| 2058 | return ret; |
| 2059 | } |
| 2060 | |
Laura Abbott | 7e44648 | 2012-06-13 15:59:39 -0700 | [diff] [blame] | 2061 | int ion_secure_heap(struct ion_device *dev, int heap_id, int version, |
| 2062 | void *data) |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 2063 | { |
| 2064 | struct rb_node *n; |
| 2065 | int ret_val = 0; |
| 2066 | |
| 2067 | /* |
| 2068 | * traverse the list of heaps available in this system |
| 2069 | * and find the heap that is specified. |
| 2070 | */ |
| 2071 | mutex_lock(&dev->lock); |
| 2072 | for (n = rb_first(&dev->heaps); n != NULL; n = rb_next(n)) { |
| 2073 | struct ion_heap *heap = rb_entry(n, struct ion_heap, node); |
Laura Abbott | 4afbd8b | 2013-02-15 09:21:33 -0800 | [diff] [blame] | 2074 | if (!ion_heap_allow_heap_secure(heap->type)) |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 2075 | continue; |
| 2076 | if (ION_HEAP(heap->id) != heap_id) |
| 2077 | continue; |
| 2078 | if (heap->ops->secure_heap) |
Laura Abbott | 7e44648 | 2012-06-13 15:59:39 -0700 | [diff] [blame] | 2079 | ret_val = heap->ops->secure_heap(heap, version, data); |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 2080 | else |
| 2081 | ret_val = -EINVAL; |
| 2082 | break; |
| 2083 | } |
| 2084 | mutex_unlock(&dev->lock); |
| 2085 | return ret_val; |
| 2086 | } |
Olav Haugan | bd453a9 | 2012-07-05 14:21:34 -0700 | [diff] [blame] | 2087 | EXPORT_SYMBOL(ion_secure_heap); |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 2088 | |
Laura Abbott | 7e44648 | 2012-06-13 15:59:39 -0700 | [diff] [blame] | 2089 | int ion_unsecure_heap(struct ion_device *dev, int heap_id, int version, |
| 2090 | void *data) |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 2091 | { |
| 2092 | struct rb_node *n; |
| 2093 | int ret_val = 0; |
| 2094 | |
| 2095 | /* |
| 2096 | * traverse the list of heaps available in this system |
| 2097 | * and find the heap that is specified. |
| 2098 | */ |
| 2099 | mutex_lock(&dev->lock); |
| 2100 | for (n = rb_first(&dev->heaps); n != NULL; n = rb_next(n)) { |
| 2101 | struct ion_heap *heap = rb_entry(n, struct ion_heap, node); |
Laura Abbott | 4afbd8b | 2013-02-15 09:21:33 -0800 | [diff] [blame] | 2102 | if (!ion_heap_allow_heap_secure(heap->type)) |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 2103 | continue; |
| 2104 | if (ION_HEAP(heap->id) != heap_id) |
| 2105 | continue; |
| 2106 | if (heap->ops->secure_heap) |
Laura Abbott | 7e44648 | 2012-06-13 15:59:39 -0700 | [diff] [blame] | 2107 | ret_val = heap->ops->unsecure_heap(heap, version, data); |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 2108 | else |
| 2109 | ret_val = -EINVAL; |
| 2110 | break; |
| 2111 | } |
| 2112 | mutex_unlock(&dev->lock); |
| 2113 | return ret_val; |
| 2114 | } |
Olav Haugan | bd453a9 | 2012-07-05 14:21:34 -0700 | [diff] [blame] | 2115 | EXPORT_SYMBOL(ion_unsecure_heap); |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 2116 | |
Laura Abbott | 404f824 | 2011-10-31 14:22:53 -0700 | [diff] [blame] | 2117 | static int ion_debug_leak_show(struct seq_file *s, void *unused) |
| 2118 | { |
| 2119 | struct ion_device *dev = s->private; |
| 2120 | struct rb_node *n; |
Laura Abbott | 404f824 | 2011-10-31 14:22:53 -0700 | [diff] [blame] | 2121 | |
Laura Abbott | 404f824 | 2011-10-31 14:22:53 -0700 | [diff] [blame] | 2122 | seq_printf(s, "%16.s %16.s %16.s %16.s\n", "buffer", "heap", "size", |
| 2123 | "ref cnt"); |
Mitchel Humpherys | a75e4eb | 2012-12-14 16:12:23 -0800 | [diff] [blame] | 2124 | |
Laura Abbott | 404f824 | 2011-10-31 14:22:53 -0700 | [diff] [blame] | 2125 | mutex_lock(&dev->lock); |
Mitchel Humpherys | a75e4eb | 2012-12-14 16:12:23 -0800 | [diff] [blame] | 2126 | ion_mark_dangling_buffers_locked(dev); |
Laura Abbott | 404f824 | 2011-10-31 14:22:53 -0700 | [diff] [blame] | 2127 | |
Mitchel Humpherys | a75e4eb | 2012-12-14 16:12:23 -0800 | [diff] [blame] | 2128 | /* Anyone still marked as a 1 means a leaked handle somewhere */ |
Laura Abbott | 404f824 | 2011-10-31 14:22:53 -0700 | [diff] [blame] | 2129 | for (n = rb_first(&dev->buffers); n; n = rb_next(n)) { |
| 2130 | struct ion_buffer *buf = rb_entry(n, struct ion_buffer, |
| 2131 | node); |
| 2132 | |
| 2133 | if (buf->marked == 1) |
| 2134 | seq_printf(s, "%16.x %16.s %16.x %16.d\n", |
| 2135 | (int)buf, buf->heap->name, buf->size, |
| 2136 | atomic_read(&buf->ref.refcount)); |
| 2137 | } |
| 2138 | mutex_unlock(&dev->lock); |
| 2139 | return 0; |
| 2140 | } |
| 2141 | |
| 2142 | static int ion_debug_leak_open(struct inode *inode, struct file *file) |
| 2143 | { |
| 2144 | return single_open(file, ion_debug_leak_show, inode->i_private); |
| 2145 | } |
| 2146 | |
| 2147 | static const struct file_operations debug_leak_fops = { |
| 2148 | .open = ion_debug_leak_open, |
| 2149 | .read = seq_read, |
| 2150 | .llseek = seq_lseek, |
| 2151 | .release = single_release, |
| 2152 | }; |
| 2153 | |
| 2154 | |
| 2155 | |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 2156 | struct ion_device *ion_device_create(long (*custom_ioctl) |
| 2157 | (struct ion_client *client, |
| 2158 | unsigned int cmd, |
| 2159 | unsigned long arg)) |
| 2160 | { |
| 2161 | struct ion_device *idev; |
| 2162 | int ret; |
| 2163 | |
| 2164 | idev = kzalloc(sizeof(struct ion_device), GFP_KERNEL); |
| 2165 | if (!idev) |
| 2166 | return ERR_PTR(-ENOMEM); |
| 2167 | |
| 2168 | idev->dev.minor = MISC_DYNAMIC_MINOR; |
| 2169 | idev->dev.name = "ion"; |
| 2170 | idev->dev.fops = &ion_fops; |
| 2171 | idev->dev.parent = NULL; |
| 2172 | ret = misc_register(&idev->dev); |
| 2173 | if (ret) { |
| 2174 | pr_err("ion: failed to register misc device.\n"); |
| 2175 | return ERR_PTR(ret); |
| 2176 | } |
| 2177 | |
| 2178 | idev->debug_root = debugfs_create_dir("ion", NULL); |
| 2179 | if (IS_ERR_OR_NULL(idev->debug_root)) |
| 2180 | pr_err("ion: failed to create debug files.\n"); |
| 2181 | |
| 2182 | idev->custom_ioctl = custom_ioctl; |
| 2183 | idev->buffers = RB_ROOT; |
| 2184 | mutex_init(&idev->lock); |
| 2185 | idev->heaps = RB_ROOT; |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 2186 | idev->clients = RB_ROOT; |
Laura Abbott | 404f824 | 2011-10-31 14:22:53 -0700 | [diff] [blame] | 2187 | debugfs_create_file("check_leaked_fds", 0664, idev->debug_root, idev, |
| 2188 | &debug_leak_fops); |
Mitchel Humpherys | a75e4eb | 2012-12-14 16:12:23 -0800 | [diff] [blame] | 2189 | |
| 2190 | setup_ion_leak_check(idev->debug_root); |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 2191 | return idev; |
| 2192 | } |
| 2193 | |
| 2194 | void ion_device_destroy(struct ion_device *dev) |
| 2195 | { |
| 2196 | misc_deregister(&dev->dev); |
| 2197 | /* XXX need to free the heaps and clients ? */ |
| 2198 | kfree(dev); |
| 2199 | } |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 2200 | |
| 2201 | void __init ion_reserve(struct ion_platform_data *data) |
| 2202 | { |
| 2203 | int i, ret; |
| 2204 | |
| 2205 | for (i = 0; i < data->nr; i++) { |
| 2206 | if (data->heaps[i].size == 0) |
| 2207 | continue; |
| 2208 | ret = memblock_reserve(data->heaps[i].base, |
| 2209 | data->heaps[i].size); |
| 2210 | if (ret) |
Laura Abbott | 1135c9e | 2013-03-13 15:33:40 -0700 | [diff] [blame] | 2211 | pr_err("memblock reserve of %x@%pa failed\n", |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 2212 | data->heaps[i].size, |
Laura Abbott | 1135c9e | 2013-03-13 15:33:40 -0700 | [diff] [blame] | 2213 | &data->heaps[i].base); |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 2214 | } |
| 2215 | } |