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