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