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