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: |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 635 | kfree(data); |
Olav Haugan | b367659 | 2012-03-02 15:02:25 -0800 | [diff] [blame] | 636 | return ERR_PTR(ret); |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | int ion_map_iommu(struct ion_client *client, struct ion_handle *handle, |
| 640 | int domain_num, int partition_num, unsigned long align, |
| 641 | unsigned long iova_length, unsigned long *iova, |
| 642 | unsigned long *buffer_size, |
Olav Haugan | b367659 | 2012-03-02 15:02:25 -0800 | [diff] [blame] | 643 | unsigned long flags, unsigned long iommu_flags) |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 644 | { |
| 645 | struct ion_buffer *buffer; |
| 646 | struct ion_iommu_map *iommu_map; |
| 647 | int ret = 0; |
| 648 | |
Olav Haugan | 79e9ffa | 2012-02-24 13:11:10 -0800 | [diff] [blame] | 649 | if (ION_IS_CACHED(flags)) { |
| 650 | pr_err("%s: Cannot map iommu as cached.\n", __func__); |
| 651 | return -EINVAL; |
| 652 | } |
| 653 | |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 654 | mutex_lock(&client->lock); |
| 655 | if (!ion_handle_validate(client, handle)) { |
| 656 | pr_err("%s: invalid handle passed to map_kernel.\n", |
| 657 | __func__); |
| 658 | mutex_unlock(&client->lock); |
| 659 | return -EINVAL; |
| 660 | } |
| 661 | |
| 662 | buffer = handle->buffer; |
| 663 | mutex_lock(&buffer->lock); |
| 664 | |
| 665 | if (!handle->buffer->heap->ops->map_iommu) { |
| 666 | pr_err("%s: map_iommu is not implemented by this heap.\n", |
| 667 | __func__); |
| 668 | ret = -ENODEV; |
| 669 | goto out; |
| 670 | } |
| 671 | |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 672 | /* |
| 673 | * If clients don't want a custom iova length, just use whatever |
| 674 | * the buffer size is |
| 675 | */ |
| 676 | if (!iova_length) |
| 677 | iova_length = buffer->size; |
| 678 | |
| 679 | if (buffer->size > iova_length) { |
| 680 | pr_debug("%s: iova length %lx is not at least buffer size" |
| 681 | " %x\n", __func__, iova_length, buffer->size); |
| 682 | ret = -EINVAL; |
| 683 | goto out; |
| 684 | } |
| 685 | |
| 686 | if (buffer->size & ~PAGE_MASK) { |
| 687 | pr_debug("%s: buffer size %x is not aligned to %lx", __func__, |
| 688 | buffer->size, PAGE_SIZE); |
| 689 | ret = -EINVAL; |
| 690 | goto out; |
| 691 | } |
| 692 | |
| 693 | if (iova_length & ~PAGE_MASK) { |
| 694 | pr_debug("%s: iova_length %lx is not aligned to %lx", __func__, |
| 695 | iova_length, PAGE_SIZE); |
| 696 | ret = -EINVAL; |
| 697 | goto out; |
| 698 | } |
| 699 | |
| 700 | iommu_map = ion_iommu_lookup(buffer, domain_num, partition_num); |
Olav Haugan | b367659 | 2012-03-02 15:02:25 -0800 | [diff] [blame] | 701 | _ion_map(&buffer->iommu_map_cnt, &handle->iommu_map_cnt); |
| 702 | if (!iommu_map) { |
| 703 | iommu_map = __ion_iommu_map(buffer, domain_num, partition_num, |
| 704 | align, iova_length, flags, iova); |
| 705 | if (IS_ERR_OR_NULL(iommu_map)) { |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 706 | _ion_unmap(&buffer->iommu_map_cnt, |
| 707 | &handle->iommu_map_cnt); |
Olav Haugan | b367659 | 2012-03-02 15:02:25 -0800 | [diff] [blame] | 708 | } else { |
| 709 | iommu_map->flags = iommu_flags; |
| 710 | |
| 711 | if (iommu_map->flags & ION_IOMMU_UNMAP_DELAYED) |
| 712 | kref_get(&iommu_map->ref); |
| 713 | } |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 714 | } else { |
Olav Haugan | b367659 | 2012-03-02 15:02:25 -0800 | [diff] [blame] | 715 | if (iommu_map->flags != iommu_flags) { |
| 716 | pr_err("%s: handle %p is already mapped with iommu flags %lx, trying to map with flags %lx\n", |
| 717 | __func__, handle, |
| 718 | iommu_map->flags, iommu_flags); |
| 719 | _ion_unmap(&buffer->iommu_map_cnt, |
| 720 | &handle->iommu_map_cnt); |
| 721 | ret = -EINVAL; |
| 722 | } else if (iommu_map->mapped_size != iova_length) { |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 723 | pr_err("%s: handle %p is already mapped with length" |
Olav Haugan | b367659 | 2012-03-02 15:02:25 -0800 | [diff] [blame] | 724 | " %x, trying to map with length %lx\n", |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 725 | __func__, handle, iommu_map->mapped_size, |
| 726 | iova_length); |
| 727 | _ion_unmap(&buffer->iommu_map_cnt, |
| 728 | &handle->iommu_map_cnt); |
| 729 | ret = -EINVAL; |
| 730 | } else { |
| 731 | kref_get(&iommu_map->ref); |
| 732 | *iova = iommu_map->iova_addr; |
| 733 | } |
| 734 | } |
| 735 | *buffer_size = buffer->size; |
| 736 | out: |
| 737 | mutex_unlock(&buffer->lock); |
| 738 | mutex_unlock(&client->lock); |
| 739 | return ret; |
| 740 | } |
| 741 | EXPORT_SYMBOL(ion_map_iommu); |
| 742 | |
| 743 | static void ion_iommu_release(struct kref *kref) |
| 744 | { |
| 745 | struct ion_iommu_map *map = container_of(kref, struct ion_iommu_map, |
| 746 | ref); |
| 747 | struct ion_buffer *buffer = map->buffer; |
| 748 | |
| 749 | rb_erase(&map->node, &buffer->iommu_maps); |
| 750 | buffer->heap->ops->unmap_iommu(map); |
| 751 | kfree(map); |
| 752 | } |
| 753 | |
| 754 | void ion_unmap_iommu(struct ion_client *client, struct ion_handle *handle, |
| 755 | int domain_num, int partition_num) |
| 756 | { |
| 757 | struct ion_iommu_map *iommu_map; |
| 758 | struct ion_buffer *buffer; |
| 759 | |
| 760 | mutex_lock(&client->lock); |
| 761 | buffer = handle->buffer; |
| 762 | |
| 763 | mutex_lock(&buffer->lock); |
| 764 | |
| 765 | iommu_map = ion_iommu_lookup(buffer, domain_num, partition_num); |
| 766 | |
| 767 | if (!iommu_map) { |
| 768 | WARN(1, "%s: (%d,%d) was never mapped for %p\n", __func__, |
| 769 | domain_num, partition_num, buffer); |
| 770 | goto out; |
| 771 | } |
| 772 | |
| 773 | _ion_unmap(&buffer->iommu_map_cnt, &handle->iommu_map_cnt); |
| 774 | kref_put(&iommu_map->ref, ion_iommu_release); |
| 775 | |
| 776 | out: |
| 777 | mutex_unlock(&buffer->lock); |
| 778 | |
| 779 | mutex_unlock(&client->lock); |
| 780 | |
| 781 | } |
| 782 | EXPORT_SYMBOL(ion_unmap_iommu); |
| 783 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 784 | struct scatterlist *ion_map_dma(struct ion_client *client, |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 785 | struct ion_handle *handle, |
| 786 | unsigned long flags) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 787 | { |
| 788 | struct ion_buffer *buffer; |
| 789 | struct scatterlist *sglist; |
| 790 | |
| 791 | mutex_lock(&client->lock); |
| 792 | if (!ion_handle_validate(client, handle)) { |
| 793 | pr_err("%s: invalid handle passed to map_dma.\n", |
| 794 | __func__); |
Rebecca Schultz Zavin | e6ee124 | 2011-06-30 12:19:55 -0700 | [diff] [blame] | 795 | mutex_unlock(&client->lock); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 796 | return ERR_PTR(-EINVAL); |
| 797 | } |
| 798 | buffer = handle->buffer; |
| 799 | mutex_lock(&buffer->lock); |
| 800 | |
| 801 | if (!handle->buffer->heap->ops->map_dma) { |
| 802 | pr_err("%s: map_kernel is not implemented by this heap.\n", |
| 803 | __func__); |
Rebecca Schultz Zavin | e6ee124 | 2011-06-30 12:19:55 -0700 | [diff] [blame] | 804 | mutex_unlock(&buffer->lock); |
| 805 | mutex_unlock(&client->lock); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 806 | return ERR_PTR(-ENODEV); |
| 807 | } |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 808 | |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 809 | if (ion_validate_buffer_flags(buffer, flags)) { |
| 810 | sglist = ERR_PTR(-EEXIST); |
| 811 | goto out; |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 812 | } |
| 813 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 814 | if (_ion_map(&buffer->dmap_cnt, &handle->dmap_cnt)) { |
| 815 | sglist = buffer->heap->ops->map_dma(buffer->heap, buffer); |
| 816 | if (IS_ERR_OR_NULL(sglist)) |
| 817 | _ion_unmap(&buffer->dmap_cnt, &handle->dmap_cnt); |
| 818 | buffer->sglist = sglist; |
| 819 | } else { |
| 820 | sglist = buffer->sglist; |
| 821 | } |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 822 | |
| 823 | out: |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 824 | mutex_unlock(&buffer->lock); |
| 825 | mutex_unlock(&client->lock); |
| 826 | return sglist; |
| 827 | } |
Olav Haugan | bd2b692 | 2012-01-25 09:28:55 -0800 | [diff] [blame] | 828 | EXPORT_SYMBOL(ion_map_dma); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 829 | |
| 830 | void ion_unmap_kernel(struct ion_client *client, struct ion_handle *handle) |
| 831 | { |
| 832 | struct ion_buffer *buffer; |
| 833 | |
| 834 | mutex_lock(&client->lock); |
| 835 | buffer = handle->buffer; |
| 836 | mutex_lock(&buffer->lock); |
| 837 | if (_ion_unmap(&buffer->kmap_cnt, &handle->kmap_cnt)) { |
| 838 | buffer->heap->ops->unmap_kernel(buffer->heap, buffer); |
| 839 | buffer->vaddr = NULL; |
| 840 | } |
| 841 | mutex_unlock(&buffer->lock); |
| 842 | mutex_unlock(&client->lock); |
| 843 | } |
Olav Haugan | bd2b692 | 2012-01-25 09:28:55 -0800 | [diff] [blame] | 844 | EXPORT_SYMBOL(ion_unmap_kernel); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 845 | |
| 846 | void ion_unmap_dma(struct ion_client *client, struct ion_handle *handle) |
| 847 | { |
| 848 | struct ion_buffer *buffer; |
| 849 | |
| 850 | mutex_lock(&client->lock); |
| 851 | buffer = handle->buffer; |
| 852 | mutex_lock(&buffer->lock); |
| 853 | if (_ion_unmap(&buffer->dmap_cnt, &handle->dmap_cnt)) { |
| 854 | buffer->heap->ops->unmap_dma(buffer->heap, buffer); |
| 855 | buffer->sglist = NULL; |
| 856 | } |
| 857 | mutex_unlock(&buffer->lock); |
| 858 | mutex_unlock(&client->lock); |
| 859 | } |
Olav Haugan | bd2b692 | 2012-01-25 09:28:55 -0800 | [diff] [blame] | 860 | EXPORT_SYMBOL(ion_unmap_dma); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 861 | |
| 862 | struct ion_buffer *ion_share(struct ion_client *client, |
| 863 | struct ion_handle *handle) |
| 864 | { |
Rebecca Schultz Zavin | c72866d | 2011-07-07 17:07:56 -0700 | [diff] [blame] | 865 | bool valid_handle; |
| 866 | |
| 867 | mutex_lock(&client->lock); |
| 868 | valid_handle = ion_handle_validate(client, handle); |
| 869 | mutex_unlock(&client->lock); |
| 870 | if (!valid_handle) { |
| 871 | WARN("%s: invalid handle passed to share.\n", __func__); |
| 872 | return ERR_PTR(-EINVAL); |
| 873 | } |
| 874 | |
Iliyan Malchev | 3fe2436 | 2011-08-09 14:42:08 -0700 | [diff] [blame] | 875 | /* 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] | 876 | * to make sure the buffer doesn't go away while it's passing it |
| 877 | * to another client -- ion_free should not be called on this handle |
| 878 | * until the buffer has been imported into the other client |
| 879 | */ |
| 880 | return handle->buffer; |
| 881 | } |
Olav Haugan | bd2b692 | 2012-01-25 09:28:55 -0800 | [diff] [blame] | 882 | EXPORT_SYMBOL(ion_share); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 883 | |
| 884 | struct ion_handle *ion_import(struct ion_client *client, |
| 885 | struct ion_buffer *buffer) |
| 886 | { |
| 887 | struct ion_handle *handle = NULL; |
| 888 | |
| 889 | mutex_lock(&client->lock); |
| 890 | /* if a handle exists for this buffer just take a reference to it */ |
| 891 | handle = ion_handle_lookup(client, buffer); |
| 892 | if (!IS_ERR_OR_NULL(handle)) { |
| 893 | ion_handle_get(handle); |
| 894 | goto end; |
| 895 | } |
| 896 | handle = ion_handle_create(client, buffer); |
| 897 | if (IS_ERR_OR_NULL(handle)) |
| 898 | goto end; |
| 899 | ion_handle_add(client, handle); |
| 900 | end: |
| 901 | mutex_unlock(&client->lock); |
| 902 | return handle; |
| 903 | } |
Olav Haugan | bd2b692 | 2012-01-25 09:28:55 -0800 | [diff] [blame] | 904 | EXPORT_SYMBOL(ion_import); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 905 | |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 906 | static int check_vaddr_bounds(unsigned long start, unsigned long end) |
| 907 | { |
| 908 | struct mm_struct *mm = current->active_mm; |
| 909 | struct vm_area_struct *vma; |
| 910 | int ret = 1; |
| 911 | |
| 912 | if (end < start) |
| 913 | goto out; |
| 914 | |
| 915 | down_read(&mm->mmap_sem); |
| 916 | vma = find_vma(mm, start); |
| 917 | if (vma && vma->vm_start < end) { |
| 918 | if (start < vma->vm_start) |
| 919 | goto out_up; |
| 920 | if (end > vma->vm_end) |
| 921 | goto out_up; |
| 922 | ret = 0; |
| 923 | } |
| 924 | |
| 925 | out_up: |
| 926 | up_read(&mm->mmap_sem); |
| 927 | out: |
| 928 | return ret; |
| 929 | } |
| 930 | |
Olav Haugan | 41f8579 | 2012-02-08 15:28:05 -0800 | [diff] [blame] | 931 | 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] | 932 | void *uaddr, unsigned long offset, unsigned long len, |
| 933 | unsigned int cmd) |
| 934 | { |
| 935 | struct ion_buffer *buffer; |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 936 | int ret = -EINVAL; |
| 937 | |
| 938 | mutex_lock(&client->lock); |
| 939 | if (!ion_handle_validate(client, handle)) { |
| 940 | pr_err("%s: invalid handle passed to do_cache_op.\n", |
| 941 | __func__); |
| 942 | mutex_unlock(&client->lock); |
| 943 | return -EINVAL; |
| 944 | } |
| 945 | buffer = handle->buffer; |
| 946 | mutex_lock(&buffer->lock); |
| 947 | |
Laura Abbott | cbaa668 | 2011-10-19 12:14:14 -0700 | [diff] [blame] | 948 | if (!ION_IS_CACHED(buffer->flags)) { |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 949 | ret = 0; |
| 950 | goto out; |
| 951 | } |
| 952 | |
| 953 | if (!handle->buffer->heap->ops->cache_op) { |
| 954 | pr_err("%s: cache_op is not implemented by this heap.\n", |
| 955 | __func__); |
| 956 | ret = -ENODEV; |
| 957 | goto out; |
| 958 | } |
| 959 | |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 960 | |
| 961 | ret = buffer->heap->ops->cache_op(buffer->heap, buffer, uaddr, |
| 962 | offset, len, cmd); |
| 963 | |
| 964 | out: |
| 965 | mutex_unlock(&buffer->lock); |
| 966 | mutex_unlock(&client->lock); |
| 967 | return ret; |
| 968 | |
| 969 | } |
| 970 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 971 | static const struct file_operations ion_share_fops; |
| 972 | |
| 973 | struct ion_handle *ion_import_fd(struct ion_client *client, int fd) |
| 974 | { |
| 975 | struct file *file = fget(fd); |
| 976 | struct ion_handle *handle; |
| 977 | |
| 978 | if (!file) { |
| 979 | pr_err("%s: imported fd not found in file table.\n", __func__); |
| 980 | return ERR_PTR(-EINVAL); |
| 981 | } |
| 982 | if (file->f_op != &ion_share_fops) { |
Laura Abbott | 084d6eb | 2011-10-24 19:09:50 -0700 | [diff] [blame] | 983 | pr_err("%s: imported file %s is not a shared ion" |
| 984 | " file.", __func__, file->f_dentry->d_name.name); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 985 | handle = ERR_PTR(-EINVAL); |
| 986 | goto end; |
| 987 | } |
| 988 | handle = ion_import(client, file->private_data); |
| 989 | end: |
| 990 | fput(file); |
| 991 | return handle; |
| 992 | } |
Olav Haugan | bd2b692 | 2012-01-25 09:28:55 -0800 | [diff] [blame] | 993 | EXPORT_SYMBOL(ion_import_fd); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 994 | |
| 995 | static int ion_debug_client_show(struct seq_file *s, void *unused) |
| 996 | { |
| 997 | struct ion_client *client = s->private; |
| 998 | struct rb_node *n; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 999 | |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 1000 | seq_printf(s, "%16.16s: %16.16s : %16.16s : %16.16s\n", "heap_name", |
| 1001 | "size_in_bytes", "handle refcount", "buffer"); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1002 | mutex_lock(&client->lock); |
| 1003 | for (n = rb_first(&client->handles); n; n = rb_next(n)) { |
| 1004 | struct ion_handle *handle = rb_entry(n, struct ion_handle, |
| 1005 | node); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1006 | |
Laura Abbott | 8747bbe | 2011-10-31 14:18:13 -0700 | [diff] [blame] | 1007 | seq_printf(s, "%16.16s: %16x : %16d : %16p\n", |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 1008 | handle->buffer->heap->name, |
| 1009 | handle->buffer->size, |
| 1010 | atomic_read(&handle->ref.refcount), |
| 1011 | handle->buffer); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1012 | } |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 1013 | |
| 1014 | seq_printf(s, "%16.16s %d\n", "client refcount:", |
| 1015 | atomic_read(&client->ref.refcount)); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1016 | mutex_unlock(&client->lock); |
| 1017 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1018 | return 0; |
| 1019 | } |
| 1020 | |
| 1021 | static int ion_debug_client_open(struct inode *inode, struct file *file) |
| 1022 | { |
| 1023 | return single_open(file, ion_debug_client_show, inode->i_private); |
| 1024 | } |
| 1025 | |
| 1026 | static const struct file_operations debug_client_fops = { |
| 1027 | .open = ion_debug_client_open, |
| 1028 | .read = seq_read, |
| 1029 | .llseek = seq_lseek, |
| 1030 | .release = single_release, |
| 1031 | }; |
| 1032 | |
Rebecca Schultz Zavin | 83e3dab | 2011-07-01 20:41:25 -0700 | [diff] [blame] | 1033 | static struct ion_client *ion_client_lookup(struct ion_device *dev, |
| 1034 | struct task_struct *task) |
| 1035 | { |
| 1036 | struct rb_node *n = dev->user_clients.rb_node; |
| 1037 | struct ion_client *client; |
| 1038 | |
| 1039 | mutex_lock(&dev->lock); |
| 1040 | while (n) { |
| 1041 | client = rb_entry(n, struct ion_client, node); |
| 1042 | if (task == client->task) { |
| 1043 | ion_client_get(client); |
| 1044 | mutex_unlock(&dev->lock); |
| 1045 | return client; |
| 1046 | } else if (task < client->task) { |
| 1047 | n = n->rb_left; |
| 1048 | } else if (task > client->task) { |
| 1049 | n = n->rb_right; |
| 1050 | } |
| 1051 | } |
| 1052 | mutex_unlock(&dev->lock); |
| 1053 | return NULL; |
| 1054 | } |
| 1055 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1056 | struct ion_client *ion_client_create(struct ion_device *dev, |
| 1057 | unsigned int heap_mask, |
| 1058 | const char *name) |
| 1059 | { |
| 1060 | struct ion_client *client; |
| 1061 | struct task_struct *task; |
| 1062 | struct rb_node **p; |
| 1063 | struct rb_node *parent = NULL; |
| 1064 | struct ion_client *entry; |
Rebecca Schultz Zavin | 83e3dab | 2011-07-01 20:41:25 -0700 | [diff] [blame] | 1065 | pid_t pid; |
Olav Haugan | 63e5f3b | 2012-01-11 16:42:37 -0800 | [diff] [blame] | 1066 | unsigned int name_len = strnlen(name, 64); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1067 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1068 | get_task_struct(current->group_leader); |
| 1069 | task_lock(current->group_leader); |
Rebecca Schultz Zavin | 83e3dab | 2011-07-01 20:41:25 -0700 | [diff] [blame] | 1070 | pid = task_pid_nr(current->group_leader); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1071 | /* don't bother to store task struct for kernel threads, |
| 1072 | they can't be killed anyway */ |
| 1073 | if (current->group_leader->flags & PF_KTHREAD) { |
| 1074 | put_task_struct(current->group_leader); |
| 1075 | task = NULL; |
| 1076 | } else { |
| 1077 | task = current->group_leader; |
| 1078 | } |
| 1079 | task_unlock(current->group_leader); |
Rebecca Schultz Zavin | 83e3dab | 2011-07-01 20:41:25 -0700 | [diff] [blame] | 1080 | |
| 1081 | /* if this isn't a kernel thread, see if a client already |
| 1082 | exists */ |
| 1083 | if (task) { |
| 1084 | client = ion_client_lookup(dev, task); |
| 1085 | if (!IS_ERR_OR_NULL(client)) { |
| 1086 | put_task_struct(current->group_leader); |
| 1087 | return client; |
| 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | client = kzalloc(sizeof(struct ion_client), GFP_KERNEL); |
| 1092 | if (!client) { |
| 1093 | put_task_struct(current->group_leader); |
| 1094 | return ERR_PTR(-ENOMEM); |
| 1095 | } |
| 1096 | |
| 1097 | client->dev = dev; |
| 1098 | client->handles = RB_ROOT; |
| 1099 | mutex_init(&client->lock); |
Olav Haugan | 63e5f3b | 2012-01-11 16:42:37 -0800 | [diff] [blame] | 1100 | |
Olav Haugan | 6625c7d1 | 2012-01-24 13:50:43 -0800 | [diff] [blame] | 1101 | client->name = kzalloc(name_len+1, GFP_KERNEL); |
Olav Haugan | 63e5f3b | 2012-01-11 16:42:37 -0800 | [diff] [blame] | 1102 | if (!client->name) { |
| 1103 | put_task_struct(current->group_leader); |
| 1104 | kfree(client); |
| 1105 | return ERR_PTR(-ENOMEM); |
| 1106 | } else { |
Olav Haugan | 6625c7d1 | 2012-01-24 13:50:43 -0800 | [diff] [blame] | 1107 | strlcpy(client->name, name, name_len+1); |
Olav Haugan | 63e5f3b | 2012-01-11 16:42:37 -0800 | [diff] [blame] | 1108 | } |
| 1109 | |
Rebecca Schultz Zavin | 83e3dab | 2011-07-01 20:41:25 -0700 | [diff] [blame] | 1110 | client->heap_mask = heap_mask; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1111 | client->task = task; |
Rebecca Schultz Zavin | 83e3dab | 2011-07-01 20:41:25 -0700 | [diff] [blame] | 1112 | client->pid = pid; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1113 | kref_init(&client->ref); |
| 1114 | |
| 1115 | mutex_lock(&dev->lock); |
| 1116 | if (task) { |
| 1117 | p = &dev->user_clients.rb_node; |
| 1118 | while (*p) { |
| 1119 | parent = *p; |
| 1120 | entry = rb_entry(parent, struct ion_client, node); |
| 1121 | |
| 1122 | if (task < entry->task) |
| 1123 | p = &(*p)->rb_left; |
| 1124 | else if (task > entry->task) |
| 1125 | p = &(*p)->rb_right; |
| 1126 | } |
| 1127 | rb_link_node(&client->node, parent, p); |
| 1128 | rb_insert_color(&client->node, &dev->user_clients); |
| 1129 | } else { |
| 1130 | p = &dev->kernel_clients.rb_node; |
| 1131 | while (*p) { |
| 1132 | parent = *p; |
| 1133 | entry = rb_entry(parent, struct ion_client, node); |
| 1134 | |
| 1135 | if (client < entry) |
| 1136 | p = &(*p)->rb_left; |
| 1137 | else if (client > entry) |
| 1138 | p = &(*p)->rb_right; |
| 1139 | } |
| 1140 | rb_link_node(&client->node, parent, p); |
| 1141 | rb_insert_color(&client->node, &dev->kernel_clients); |
| 1142 | } |
| 1143 | |
Laura Abbott | eed8603 | 2011-12-05 15:32:36 -0800 | [diff] [blame] | 1144 | |
| 1145 | client->debug_root = debugfs_create_file(name, 0664, |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1146 | dev->debug_root, client, |
| 1147 | &debug_client_fops); |
| 1148 | mutex_unlock(&dev->lock); |
| 1149 | |
| 1150 | return client; |
| 1151 | } |
| 1152 | |
Rebecca Schultz Zavin | 0b7e8ae | 2011-07-06 18:07:01 -0700 | [diff] [blame] | 1153 | static void _ion_client_destroy(struct kref *kref) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1154 | { |
Rebecca Schultz Zavin | 0b7e8ae | 2011-07-06 18:07:01 -0700 | [diff] [blame] | 1155 | struct ion_client *client = container_of(kref, struct ion_client, ref); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1156 | struct ion_device *dev = client->dev; |
| 1157 | struct rb_node *n; |
| 1158 | |
| 1159 | pr_debug("%s: %d\n", __func__, __LINE__); |
| 1160 | while ((n = rb_first(&client->handles))) { |
| 1161 | struct ion_handle *handle = rb_entry(n, struct ion_handle, |
| 1162 | node); |
| 1163 | ion_handle_destroy(&handle->ref); |
| 1164 | } |
| 1165 | mutex_lock(&dev->lock); |
| 1166 | if (client->task) { |
| 1167 | rb_erase(&client->node, &dev->user_clients); |
| 1168 | put_task_struct(client->task); |
| 1169 | } else { |
| 1170 | rb_erase(&client->node, &dev->kernel_clients); |
| 1171 | } |
| 1172 | debugfs_remove_recursive(client->debug_root); |
| 1173 | mutex_unlock(&dev->lock); |
| 1174 | |
Olav Haugan | 63e5f3b | 2012-01-11 16:42:37 -0800 | [diff] [blame] | 1175 | kfree(client->name); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1176 | kfree(client); |
| 1177 | } |
| 1178 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1179 | static void ion_client_get(struct ion_client *client) |
| 1180 | { |
| 1181 | kref_get(&client->ref); |
| 1182 | } |
| 1183 | |
| 1184 | static int ion_client_put(struct ion_client *client) |
| 1185 | { |
| 1186 | return kref_put(&client->ref, _ion_client_destroy); |
| 1187 | } |
| 1188 | |
Rebecca Schultz Zavin | 0b7e8ae | 2011-07-06 18:07:01 -0700 | [diff] [blame] | 1189 | void ion_client_destroy(struct ion_client *client) |
| 1190 | { |
Jordan Crouse | a75022c | 2011-10-12 16:57:47 -0600 | [diff] [blame] | 1191 | if (client) |
| 1192 | ion_client_put(client); |
Rebecca Schultz Zavin | 0b7e8ae | 2011-07-06 18:07:01 -0700 | [diff] [blame] | 1193 | } |
Olav Haugan | bd2b692 | 2012-01-25 09:28:55 -0800 | [diff] [blame] | 1194 | EXPORT_SYMBOL(ion_client_destroy); |
Rebecca Schultz Zavin | 0b7e8ae | 2011-07-06 18:07:01 -0700 | [diff] [blame] | 1195 | |
Laura Abbott | 273dd8e | 2011-10-12 14:26:33 -0700 | [diff] [blame] | 1196 | int ion_handle_get_flags(struct ion_client *client, struct ion_handle *handle, |
| 1197 | unsigned long *flags) |
| 1198 | { |
| 1199 | struct ion_buffer *buffer; |
| 1200 | |
| 1201 | mutex_lock(&client->lock); |
| 1202 | if (!ion_handle_validate(client, handle)) { |
| 1203 | pr_err("%s: invalid handle passed to %s.\n", |
| 1204 | __func__, __func__); |
| 1205 | mutex_unlock(&client->lock); |
| 1206 | return -EINVAL; |
| 1207 | } |
| 1208 | buffer = handle->buffer; |
| 1209 | mutex_lock(&buffer->lock); |
| 1210 | *flags = buffer->flags; |
| 1211 | mutex_unlock(&buffer->lock); |
| 1212 | mutex_unlock(&client->lock); |
| 1213 | |
| 1214 | return 0; |
| 1215 | } |
| 1216 | EXPORT_SYMBOL(ion_handle_get_flags); |
| 1217 | |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 1218 | int ion_handle_get_size(struct ion_client *client, struct ion_handle *handle, |
| 1219 | unsigned long *size) |
| 1220 | { |
| 1221 | struct ion_buffer *buffer; |
| 1222 | |
| 1223 | mutex_lock(&client->lock); |
| 1224 | if (!ion_handle_validate(client, handle)) { |
| 1225 | pr_err("%s: invalid handle passed to %s.\n", |
| 1226 | __func__, __func__); |
| 1227 | mutex_unlock(&client->lock); |
| 1228 | return -EINVAL; |
| 1229 | } |
| 1230 | buffer = handle->buffer; |
| 1231 | mutex_lock(&buffer->lock); |
| 1232 | *size = buffer->size; |
| 1233 | mutex_unlock(&buffer->lock); |
| 1234 | mutex_unlock(&client->lock); |
| 1235 | |
| 1236 | return 0; |
| 1237 | } |
| 1238 | EXPORT_SYMBOL(ion_handle_get_size); |
| 1239 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1240 | static int ion_share_release(struct inode *inode, struct file* file) |
| 1241 | { |
| 1242 | struct ion_buffer *buffer = file->private_data; |
| 1243 | |
| 1244 | pr_debug("%s: %d\n", __func__, __LINE__); |
| 1245 | /* drop the reference to the buffer -- this prevents the |
| 1246 | buffer from going away because the client holding it exited |
| 1247 | while it was being passed */ |
| 1248 | ion_buffer_put(buffer); |
| 1249 | return 0; |
| 1250 | } |
| 1251 | |
| 1252 | static void ion_vma_open(struct vm_area_struct *vma) |
| 1253 | { |
| 1254 | |
| 1255 | struct ion_buffer *buffer = vma->vm_file->private_data; |
| 1256 | struct ion_handle *handle = vma->vm_private_data; |
| 1257 | struct ion_client *client; |
| 1258 | |
| 1259 | pr_debug("%s: %d\n", __func__, __LINE__); |
| 1260 | /* check that the client still exists and take a reference so |
| 1261 | it can't go away until this vma is closed */ |
| 1262 | client = ion_client_lookup(buffer->dev, current->group_leader); |
| 1263 | if (IS_ERR_OR_NULL(client)) { |
| 1264 | vma->vm_private_data = NULL; |
| 1265 | return; |
| 1266 | } |
Laura Abbott | 0f2175b | 2011-12-09 14:26:07 -0800 | [diff] [blame] | 1267 | ion_handle_get(handle); |
Laura Abbott | 7716850 | 2011-12-05 11:06:24 -0800 | [diff] [blame] | 1268 | mutex_lock(&buffer->lock); |
| 1269 | buffer->umap_cnt++; |
| 1270 | mutex_unlock(&buffer->lock); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1271 | pr_debug("%s: %d client_cnt %d handle_cnt %d alloc_cnt %d\n", |
| 1272 | __func__, __LINE__, |
| 1273 | atomic_read(&client->ref.refcount), |
| 1274 | atomic_read(&handle->ref.refcount), |
| 1275 | atomic_read(&buffer->ref.refcount)); |
| 1276 | } |
| 1277 | |
| 1278 | static void ion_vma_close(struct vm_area_struct *vma) |
| 1279 | { |
| 1280 | struct ion_handle *handle = vma->vm_private_data; |
| 1281 | struct ion_buffer *buffer = vma->vm_file->private_data; |
| 1282 | struct ion_client *client; |
| 1283 | |
| 1284 | pr_debug("%s: %d\n", __func__, __LINE__); |
| 1285 | /* this indicates the client is gone, nothing to do here */ |
| 1286 | if (!handle) |
| 1287 | return; |
| 1288 | client = handle->client; |
Laura Abbott | 7716850 | 2011-12-05 11:06:24 -0800 | [diff] [blame] | 1289 | mutex_lock(&buffer->lock); |
| 1290 | buffer->umap_cnt--; |
| 1291 | mutex_unlock(&buffer->lock); |
Laura Abbott | a683509 | 2011-11-14 15:27:02 -0800 | [diff] [blame] | 1292 | |
| 1293 | if (buffer->heap->ops->unmap_user) |
| 1294 | buffer->heap->ops->unmap_user(buffer->heap, buffer); |
| 1295 | |
| 1296 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1297 | pr_debug("%s: %d client_cnt %d handle_cnt %d alloc_cnt %d\n", |
| 1298 | __func__, __LINE__, |
| 1299 | atomic_read(&client->ref.refcount), |
| 1300 | atomic_read(&handle->ref.refcount), |
| 1301 | atomic_read(&buffer->ref.refcount)); |
Laura Abbott | ec149ff | 2012-01-26 13:33:11 -0800 | [diff] [blame] | 1302 | mutex_lock(&client->lock); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1303 | ion_handle_put(handle); |
Laura Abbott | ec149ff | 2012-01-26 13:33:11 -0800 | [diff] [blame] | 1304 | mutex_unlock(&client->lock); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1305 | ion_client_put(client); |
| 1306 | pr_debug("%s: %d client_cnt %d handle_cnt %d alloc_cnt %d\n", |
| 1307 | __func__, __LINE__, |
| 1308 | atomic_read(&client->ref.refcount), |
| 1309 | atomic_read(&handle->ref.refcount), |
| 1310 | atomic_read(&buffer->ref.refcount)); |
| 1311 | } |
| 1312 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1313 | static struct vm_operations_struct ion_vm_ops = { |
| 1314 | .open = ion_vma_open, |
| 1315 | .close = ion_vma_close, |
| 1316 | }; |
| 1317 | |
| 1318 | static int ion_share_mmap(struct file *file, struct vm_area_struct *vma) |
| 1319 | { |
| 1320 | struct ion_buffer *buffer = file->private_data; |
| 1321 | unsigned long size = vma->vm_end - vma->vm_start; |
| 1322 | struct ion_client *client; |
| 1323 | struct ion_handle *handle; |
| 1324 | int ret; |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 1325 | unsigned long flags = file->f_flags & O_DSYNC ? |
| 1326 | ION_SET_CACHE(UNCACHED) : |
| 1327 | ION_SET_CACHE(CACHED); |
| 1328 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1329 | |
| 1330 | pr_debug("%s: %d\n", __func__, __LINE__); |
| 1331 | /* make sure the client still exists, it's possible for the client to |
| 1332 | have gone away but the map/share fd still to be around, take |
| 1333 | a reference to it so it can't go away while this mapping exists */ |
| 1334 | client = ion_client_lookup(buffer->dev, current->group_leader); |
| 1335 | if (IS_ERR_OR_NULL(client)) { |
| 1336 | pr_err("%s: trying to mmap an ion handle in a process with no " |
| 1337 | "ion client\n", __func__); |
| 1338 | return -EINVAL; |
| 1339 | } |
| 1340 | |
| 1341 | if ((size > buffer->size) || (size + (vma->vm_pgoff << PAGE_SHIFT) > |
| 1342 | buffer->size)) { |
| 1343 | pr_err("%s: trying to map larger area than handle has available" |
| 1344 | "\n", __func__); |
| 1345 | ret = -EINVAL; |
| 1346 | goto err; |
| 1347 | } |
| 1348 | |
| 1349 | /* find the handle and take a reference to it */ |
| 1350 | handle = ion_import(client, buffer); |
| 1351 | if (IS_ERR_OR_NULL(handle)) { |
| 1352 | ret = -EINVAL; |
| 1353 | goto err; |
| 1354 | } |
| 1355 | |
| 1356 | if (!handle->buffer->heap->ops->map_user) { |
| 1357 | pr_err("%s: this heap does not define a method for mapping " |
| 1358 | "to userspace\n", __func__); |
| 1359 | ret = -EINVAL; |
| 1360 | goto err1; |
| 1361 | } |
| 1362 | |
| 1363 | mutex_lock(&buffer->lock); |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 1364 | |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 1365 | if (ion_validate_buffer_flags(buffer, flags)) { |
| 1366 | ret = -EEXIST; |
| 1367 | mutex_unlock(&buffer->lock); |
| 1368 | goto err1; |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 1369 | } |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 1370 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1371 | /* now map it to userspace */ |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 1372 | ret = buffer->heap->ops->map_user(buffer->heap, buffer, vma, |
| 1373 | flags); |
Laura Abbott | e8bc7aa | 2011-12-09 14:49:33 -0800 | [diff] [blame] | 1374 | |
| 1375 | buffer->umap_cnt++; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1376 | if (ret) { |
| 1377 | pr_err("%s: failure mapping buffer to userspace\n", |
| 1378 | __func__); |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 1379 | goto err2; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1380 | } |
Laura Abbott | e8bc7aa | 2011-12-09 14:49:33 -0800 | [diff] [blame] | 1381 | mutex_unlock(&buffer->lock); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1382 | |
| 1383 | vma->vm_ops = &ion_vm_ops; |
| 1384 | /* move the handle into the vm_private_data so we can access it from |
| 1385 | vma_open/close */ |
| 1386 | vma->vm_private_data = handle; |
| 1387 | pr_debug("%s: %d client_cnt %d handle_cnt %d alloc_cnt %d\n", |
| 1388 | __func__, __LINE__, |
| 1389 | atomic_read(&client->ref.refcount), |
| 1390 | atomic_read(&handle->ref.refcount), |
| 1391 | atomic_read(&buffer->ref.refcount)); |
| 1392 | return 0; |
| 1393 | |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 1394 | err2: |
| 1395 | buffer->umap_cnt--; |
Laura Abbott | e8bc7aa | 2011-12-09 14:49:33 -0800 | [diff] [blame] | 1396 | mutex_unlock(&buffer->lock); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1397 | /* drop the reference to the handle */ |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 1398 | err1: |
Laura Abbott | ec149ff | 2012-01-26 13:33:11 -0800 | [diff] [blame] | 1399 | mutex_lock(&client->lock); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1400 | ion_handle_put(handle); |
Laura Abbott | ec149ff | 2012-01-26 13:33:11 -0800 | [diff] [blame] | 1401 | mutex_unlock(&client->lock); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1402 | err: |
Iliyan Malchev | 3fe2436 | 2011-08-09 14:42:08 -0700 | [diff] [blame] | 1403 | /* drop the reference to the client */ |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1404 | ion_client_put(client); |
| 1405 | return ret; |
| 1406 | } |
| 1407 | |
| 1408 | static const struct file_operations ion_share_fops = { |
| 1409 | .owner = THIS_MODULE, |
| 1410 | .release = ion_share_release, |
| 1411 | .mmap = ion_share_mmap, |
| 1412 | }; |
| 1413 | |
| 1414 | static int ion_ioctl_share(struct file *parent, struct ion_client *client, |
| 1415 | struct ion_handle *handle) |
| 1416 | { |
| 1417 | int fd = get_unused_fd(); |
| 1418 | struct file *file; |
| 1419 | |
| 1420 | if (fd < 0) |
| 1421 | return -ENFILE; |
| 1422 | |
| 1423 | file = anon_inode_getfile("ion_share_fd", &ion_share_fops, |
| 1424 | handle->buffer, O_RDWR); |
| 1425 | if (IS_ERR_OR_NULL(file)) |
| 1426 | goto err; |
Laura Abbott | 4b5d048 | 2011-09-27 18:35:14 -0700 | [diff] [blame] | 1427 | |
| 1428 | if (parent->f_flags & O_DSYNC) |
| 1429 | file->f_flags |= O_DSYNC; |
| 1430 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1431 | ion_buffer_get(handle->buffer); |
| 1432 | fd_install(fd, file); |
| 1433 | |
| 1434 | return fd; |
| 1435 | |
| 1436 | err: |
| 1437 | put_unused_fd(fd); |
| 1438 | return -ENFILE; |
| 1439 | } |
| 1440 | |
| 1441 | static long ion_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
| 1442 | { |
| 1443 | struct ion_client *client = filp->private_data; |
| 1444 | |
| 1445 | switch (cmd) { |
| 1446 | case ION_IOC_ALLOC: |
| 1447 | { |
| 1448 | struct ion_allocation_data data; |
| 1449 | |
| 1450 | if (copy_from_user(&data, (void __user *)arg, sizeof(data))) |
| 1451 | return -EFAULT; |
| 1452 | data.handle = ion_alloc(client, data.len, data.align, |
| 1453 | data.flags); |
Laura Abbott | e1b9ce5 | 2011-11-11 18:31:39 -0800 | [diff] [blame] | 1454 | |
| 1455 | if (IS_ERR_OR_NULL(data.handle)) |
Olav Haugan | b06ee07 | 2011-12-13 15:31:41 -0800 | [diff] [blame] | 1456 | return -ENOMEM; |
Laura Abbott | e1b9ce5 | 2011-11-11 18:31:39 -0800 | [diff] [blame] | 1457 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1458 | if (copy_to_user((void __user *)arg, &data, sizeof(data))) |
| 1459 | return -EFAULT; |
| 1460 | break; |
| 1461 | } |
| 1462 | case ION_IOC_FREE: |
| 1463 | { |
| 1464 | struct ion_handle_data data; |
Rebecca Schultz Zavin | e6ee124 | 2011-06-30 12:19:55 -0700 | [diff] [blame] | 1465 | bool valid; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1466 | |
| 1467 | if (copy_from_user(&data, (void __user *)arg, |
| 1468 | sizeof(struct ion_handle_data))) |
| 1469 | return -EFAULT; |
| 1470 | mutex_lock(&client->lock); |
Rebecca Schultz Zavin | e6ee124 | 2011-06-30 12:19:55 -0700 | [diff] [blame] | 1471 | valid = ion_handle_validate(client, data.handle); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1472 | mutex_unlock(&client->lock); |
Rebecca Schultz Zavin | e6ee124 | 2011-06-30 12:19:55 -0700 | [diff] [blame] | 1473 | if (!valid) |
| 1474 | return -EINVAL; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1475 | ion_free(client, data.handle); |
| 1476 | break; |
| 1477 | } |
| 1478 | case ION_IOC_MAP: |
| 1479 | case ION_IOC_SHARE: |
| 1480 | { |
| 1481 | struct ion_fd_data data; |
| 1482 | |
| 1483 | if (copy_from_user(&data, (void __user *)arg, sizeof(data))) |
| 1484 | return -EFAULT; |
| 1485 | mutex_lock(&client->lock); |
| 1486 | if (!ion_handle_validate(client, data.handle)) { |
| 1487 | pr_err("%s: invalid handle passed to share ioctl.\n", |
| 1488 | __func__); |
Rebecca Schultz Zavin | e6ee124 | 2011-06-30 12:19:55 -0700 | [diff] [blame] | 1489 | mutex_unlock(&client->lock); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1490 | return -EINVAL; |
| 1491 | } |
| 1492 | data.fd = ion_ioctl_share(filp, client, data.handle); |
| 1493 | mutex_unlock(&client->lock); |
| 1494 | if (copy_to_user((void __user *)arg, &data, sizeof(data))) |
| 1495 | return -EFAULT; |
Olav Haugan | c2d2cf5 | 2012-05-15 14:40:11 -0700 | [diff] [blame] | 1496 | if (data.fd < 0) |
| 1497 | return data.fd; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1498 | break; |
| 1499 | } |
| 1500 | case ION_IOC_IMPORT: |
| 1501 | { |
| 1502 | struct ion_fd_data data; |
Olav Haugan | c2d2cf5 | 2012-05-15 14:40:11 -0700 | [diff] [blame] | 1503 | int ret = 0; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1504 | if (copy_from_user(&data, (void __user *)arg, |
| 1505 | sizeof(struct ion_fd_data))) |
| 1506 | return -EFAULT; |
| 1507 | |
| 1508 | data.handle = ion_import_fd(client, data.fd); |
Olav Haugan | c2d2cf5 | 2012-05-15 14:40:11 -0700 | [diff] [blame] | 1509 | if (IS_ERR(data.handle)) { |
| 1510 | ret = PTR_ERR(data.handle); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1511 | data.handle = NULL; |
Olav Haugan | c2d2cf5 | 2012-05-15 14:40:11 -0700 | [diff] [blame] | 1512 | } |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1513 | if (copy_to_user((void __user *)arg, &data, |
| 1514 | sizeof(struct ion_fd_data))) |
| 1515 | return -EFAULT; |
Olav Haugan | c2d2cf5 | 2012-05-15 14:40:11 -0700 | [diff] [blame] | 1516 | if (ret < 0) |
| 1517 | return ret; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1518 | break; |
| 1519 | } |
| 1520 | case ION_IOC_CUSTOM: |
| 1521 | { |
| 1522 | struct ion_device *dev = client->dev; |
| 1523 | struct ion_custom_data data; |
| 1524 | |
| 1525 | if (!dev->custom_ioctl) |
| 1526 | return -ENOTTY; |
| 1527 | if (copy_from_user(&data, (void __user *)arg, |
| 1528 | sizeof(struct ion_custom_data))) |
| 1529 | return -EFAULT; |
| 1530 | return dev->custom_ioctl(client, data.cmd, data.arg); |
| 1531 | } |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 1532 | case ION_IOC_CLEAN_CACHES: |
| 1533 | case ION_IOC_INV_CACHES: |
| 1534 | case ION_IOC_CLEAN_INV_CACHES: |
| 1535 | { |
| 1536 | struct ion_flush_data data; |
Laura Abbott | 9fa29e8 | 2011-11-14 09:42:53 -0800 | [diff] [blame] | 1537 | unsigned long start, end; |
Laura Abbott | e80ea01 | 2011-11-18 18:36:47 -0800 | [diff] [blame] | 1538 | struct ion_handle *handle = NULL; |
| 1539 | int ret; |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 1540 | |
| 1541 | if (copy_from_user(&data, (void __user *)arg, |
| 1542 | sizeof(struct ion_flush_data))) |
| 1543 | return -EFAULT; |
| 1544 | |
Laura Abbott | 9fa29e8 | 2011-11-14 09:42:53 -0800 | [diff] [blame] | 1545 | start = (unsigned long) data.vaddr; |
| 1546 | end = (unsigned long) data.vaddr + data.length; |
| 1547 | |
| 1548 | if (check_vaddr_bounds(start, end)) { |
| 1549 | pr_err("%s: virtual address %p is out of bounds\n", |
| 1550 | __func__, data.vaddr); |
| 1551 | return -EINVAL; |
| 1552 | } |
| 1553 | |
Laura Abbott | e80ea01 | 2011-11-18 18:36:47 -0800 | [diff] [blame] | 1554 | if (!data.handle) { |
| 1555 | handle = ion_import_fd(client, data.fd); |
| 1556 | if (IS_ERR_OR_NULL(handle)) { |
| 1557 | pr_info("%s: Could not import handle: %d\n", |
| 1558 | __func__, (int)handle); |
| 1559 | return -EINVAL; |
| 1560 | } |
| 1561 | } |
| 1562 | |
| 1563 | ret = ion_do_cache_op(client, |
| 1564 | data.handle ? data.handle : handle, |
| 1565 | data.vaddr, data.offset, data.length, |
| 1566 | cmd); |
| 1567 | |
| 1568 | if (!data.handle) |
| 1569 | ion_free(client, handle); |
| 1570 | |
Olav Haugan | d7baec0 | 2012-05-15 14:38:09 -0700 | [diff] [blame] | 1571 | if (ret < 0) |
| 1572 | return ret; |
Laura Abbott | e80ea01 | 2011-11-18 18:36:47 -0800 | [diff] [blame] | 1573 | break; |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 1574 | |
| 1575 | } |
Laura Abbott | 273dd8e | 2011-10-12 14:26:33 -0700 | [diff] [blame] | 1576 | case ION_IOC_GET_FLAGS: |
| 1577 | { |
| 1578 | struct ion_flag_data data; |
| 1579 | int ret; |
| 1580 | if (copy_from_user(&data, (void __user *)arg, |
| 1581 | sizeof(struct ion_flag_data))) |
| 1582 | return -EFAULT; |
| 1583 | |
| 1584 | ret = ion_handle_get_flags(client, data.handle, &data.flags); |
| 1585 | if (ret < 0) |
| 1586 | return ret; |
| 1587 | if (copy_to_user((void __user *)arg, &data, |
| 1588 | sizeof(struct ion_flag_data))) |
| 1589 | return -EFAULT; |
| 1590 | break; |
| 1591 | } |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1592 | default: |
| 1593 | return -ENOTTY; |
| 1594 | } |
| 1595 | return 0; |
| 1596 | } |
| 1597 | |
| 1598 | static int ion_release(struct inode *inode, struct file *file) |
| 1599 | { |
| 1600 | struct ion_client *client = file->private_data; |
| 1601 | |
| 1602 | pr_debug("%s: %d\n", __func__, __LINE__); |
| 1603 | ion_client_put(client); |
| 1604 | return 0; |
| 1605 | } |
| 1606 | |
| 1607 | static int ion_open(struct inode *inode, struct file *file) |
| 1608 | { |
| 1609 | struct miscdevice *miscdev = file->private_data; |
| 1610 | struct ion_device *dev = container_of(miscdev, struct ion_device, dev); |
| 1611 | struct ion_client *client; |
Laura Abbott | eed8603 | 2011-12-05 15:32:36 -0800 | [diff] [blame] | 1612 | char debug_name[64]; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1613 | |
| 1614 | pr_debug("%s: %d\n", __func__, __LINE__); |
Laura Abbott | eed8603 | 2011-12-05 15:32:36 -0800 | [diff] [blame] | 1615 | snprintf(debug_name, 64, "%u", task_pid_nr(current->group_leader)); |
| 1616 | client = ion_client_create(dev, -1, debug_name); |
Rebecca Schultz Zavin | 6d3b958 | 2011-07-06 18:07:24 -0700 | [diff] [blame] | 1617 | if (IS_ERR_OR_NULL(client)) |
| 1618 | return PTR_ERR(client); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1619 | file->private_data = client; |
| 1620 | |
| 1621 | return 0; |
| 1622 | } |
| 1623 | |
| 1624 | static const struct file_operations ion_fops = { |
| 1625 | .owner = THIS_MODULE, |
| 1626 | .open = ion_open, |
| 1627 | .release = ion_release, |
| 1628 | .unlocked_ioctl = ion_ioctl, |
| 1629 | }; |
| 1630 | |
| 1631 | static size_t ion_debug_heap_total(struct ion_client *client, |
Laura Abbott | 3647ac3 | 2011-10-31 14:09:53 -0700 | [diff] [blame] | 1632 | enum ion_heap_ids id) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1633 | { |
| 1634 | size_t size = 0; |
| 1635 | struct rb_node *n; |
| 1636 | |
| 1637 | mutex_lock(&client->lock); |
| 1638 | for (n = rb_first(&client->handles); n; n = rb_next(n)) { |
| 1639 | struct ion_handle *handle = rb_entry(n, |
| 1640 | struct ion_handle, |
| 1641 | node); |
Laura Abbott | 3647ac3 | 2011-10-31 14:09:53 -0700 | [diff] [blame] | 1642 | if (handle->buffer->heap->id == id) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1643 | size += handle->buffer->size; |
| 1644 | } |
| 1645 | mutex_unlock(&client->lock); |
| 1646 | return size; |
| 1647 | } |
| 1648 | |
| 1649 | static int ion_debug_heap_show(struct seq_file *s, void *unused) |
| 1650 | { |
| 1651 | struct ion_heap *heap = s->private; |
| 1652 | struct ion_device *dev = heap->dev; |
| 1653 | struct rb_node *n; |
| 1654 | |
| 1655 | seq_printf(s, "%16.s %16.s %16.s\n", "client", "pid", "size"); |
| 1656 | for (n = rb_first(&dev->user_clients); n; n = rb_next(n)) { |
| 1657 | struct ion_client *client = rb_entry(n, struct ion_client, |
| 1658 | node); |
| 1659 | char task_comm[TASK_COMM_LEN]; |
Laura Abbott | 3647ac3 | 2011-10-31 14:09:53 -0700 | [diff] [blame] | 1660 | size_t size = ion_debug_heap_total(client, heap->id); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1661 | if (!size) |
| 1662 | continue; |
| 1663 | |
| 1664 | get_task_comm(task_comm, client->task); |
Laura Abbott | 8747bbe | 2011-10-31 14:18:13 -0700 | [diff] [blame] | 1665 | 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] | 1666 | size); |
| 1667 | } |
| 1668 | |
| 1669 | for (n = rb_first(&dev->kernel_clients); n; n = rb_next(n)) { |
| 1670 | struct ion_client *client = rb_entry(n, struct ion_client, |
| 1671 | node); |
Laura Abbott | 3647ac3 | 2011-10-31 14:09:53 -0700 | [diff] [blame] | 1672 | size_t size = ion_debug_heap_total(client, heap->id); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1673 | if (!size) |
| 1674 | continue; |
Laura Abbott | 8747bbe | 2011-10-31 14:18:13 -0700 | [diff] [blame] | 1675 | 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] | 1676 | size); |
| 1677 | } |
Olav Haugan | 3d4fe1a | 2012-01-13 11:42:15 -0800 | [diff] [blame] | 1678 | if (heap->ops->print_debug) |
| 1679 | heap->ops->print_debug(heap, s); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1680 | return 0; |
| 1681 | } |
| 1682 | |
| 1683 | static int ion_debug_heap_open(struct inode *inode, struct file *file) |
| 1684 | { |
| 1685 | return single_open(file, ion_debug_heap_show, inode->i_private); |
| 1686 | } |
| 1687 | |
| 1688 | static const struct file_operations debug_heap_fops = { |
| 1689 | .open = ion_debug_heap_open, |
| 1690 | .read = seq_read, |
| 1691 | .llseek = seq_lseek, |
| 1692 | .release = single_release, |
| 1693 | }; |
| 1694 | |
| 1695 | void ion_device_add_heap(struct ion_device *dev, struct ion_heap *heap) |
| 1696 | { |
| 1697 | struct rb_node **p = &dev->heaps.rb_node; |
| 1698 | struct rb_node *parent = NULL; |
| 1699 | struct ion_heap *entry; |
| 1700 | |
| 1701 | heap->dev = dev; |
| 1702 | mutex_lock(&dev->lock); |
| 1703 | while (*p) { |
| 1704 | parent = *p; |
| 1705 | entry = rb_entry(parent, struct ion_heap, node); |
| 1706 | |
Rebecca Schultz Zavin | e6ee124 | 2011-06-30 12:19:55 -0700 | [diff] [blame] | 1707 | if (heap->id < entry->id) { |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1708 | p = &(*p)->rb_left; |
Rebecca Schultz Zavin | e6ee124 | 2011-06-30 12:19:55 -0700 | [diff] [blame] | 1709 | } else if (heap->id > entry->id ) { |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1710 | p = &(*p)->rb_right; |
| 1711 | } else { |
| 1712 | pr_err("%s: can not insert multiple heaps with " |
Rebecca Schultz Zavin | e6ee124 | 2011-06-30 12:19:55 -0700 | [diff] [blame] | 1713 | "id %d\n", __func__, heap->id); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1714 | goto end; |
| 1715 | } |
| 1716 | } |
| 1717 | |
| 1718 | rb_link_node(&heap->node, parent, p); |
| 1719 | rb_insert_color(&heap->node, &dev->heaps); |
| 1720 | debugfs_create_file(heap->name, 0664, dev->debug_root, heap, |
| 1721 | &debug_heap_fops); |
| 1722 | end: |
| 1723 | mutex_unlock(&dev->lock); |
| 1724 | } |
| 1725 | |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 1726 | int ion_secure_heap(struct ion_device *dev, int heap_id) |
| 1727 | { |
| 1728 | struct rb_node *n; |
| 1729 | int ret_val = 0; |
| 1730 | |
| 1731 | /* |
| 1732 | * traverse the list of heaps available in this system |
| 1733 | * and find the heap that is specified. |
| 1734 | */ |
| 1735 | mutex_lock(&dev->lock); |
| 1736 | for (n = rb_first(&dev->heaps); n != NULL; n = rb_next(n)) { |
| 1737 | struct ion_heap *heap = rb_entry(n, struct ion_heap, node); |
| 1738 | if (heap->type != ION_HEAP_TYPE_CP) |
| 1739 | continue; |
| 1740 | if (ION_HEAP(heap->id) != heap_id) |
| 1741 | continue; |
| 1742 | if (heap->ops->secure_heap) |
| 1743 | ret_val = heap->ops->secure_heap(heap); |
| 1744 | else |
| 1745 | ret_val = -EINVAL; |
| 1746 | break; |
| 1747 | } |
| 1748 | mutex_unlock(&dev->lock); |
| 1749 | return ret_val; |
| 1750 | } |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 1751 | |
| 1752 | int ion_unsecure_heap(struct ion_device *dev, int heap_id) |
| 1753 | { |
| 1754 | struct rb_node *n; |
| 1755 | int ret_val = 0; |
| 1756 | |
| 1757 | /* |
| 1758 | * traverse the list of heaps available in this system |
| 1759 | * and find the heap that is specified. |
| 1760 | */ |
| 1761 | mutex_lock(&dev->lock); |
| 1762 | for (n = rb_first(&dev->heaps); n != NULL; n = rb_next(n)) { |
| 1763 | struct ion_heap *heap = rb_entry(n, struct ion_heap, node); |
| 1764 | if (heap->type != ION_HEAP_TYPE_CP) |
| 1765 | continue; |
| 1766 | if (ION_HEAP(heap->id) != heap_id) |
| 1767 | continue; |
| 1768 | if (heap->ops->secure_heap) |
| 1769 | ret_val = heap->ops->unsecure_heap(heap); |
| 1770 | else |
| 1771 | ret_val = -EINVAL; |
| 1772 | break; |
| 1773 | } |
| 1774 | mutex_unlock(&dev->lock); |
| 1775 | return ret_val; |
| 1776 | } |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 1777 | |
Laura Abbott | 404f824 | 2011-10-31 14:22:53 -0700 | [diff] [blame] | 1778 | static int ion_debug_leak_show(struct seq_file *s, void *unused) |
| 1779 | { |
| 1780 | struct ion_device *dev = s->private; |
| 1781 | struct rb_node *n; |
| 1782 | struct rb_node *n2; |
| 1783 | |
| 1784 | /* mark all buffers as 1 */ |
| 1785 | seq_printf(s, "%16.s %16.s %16.s %16.s\n", "buffer", "heap", "size", |
| 1786 | "ref cnt"); |
| 1787 | mutex_lock(&dev->lock); |
| 1788 | for (n = rb_first(&dev->buffers); n; n = rb_next(n)) { |
| 1789 | struct ion_buffer *buf = rb_entry(n, struct ion_buffer, |
| 1790 | node); |
| 1791 | |
| 1792 | buf->marked = 1; |
| 1793 | } |
| 1794 | |
| 1795 | /* now see which buffers we can access */ |
| 1796 | for (n = rb_first(&dev->kernel_clients); n; n = rb_next(n)) { |
| 1797 | struct ion_client *client = rb_entry(n, struct ion_client, |
| 1798 | node); |
| 1799 | |
| 1800 | mutex_lock(&client->lock); |
| 1801 | for (n2 = rb_first(&client->handles); n2; n2 = rb_next(n2)) { |
| 1802 | struct ion_handle *handle = rb_entry(n2, |
| 1803 | struct ion_handle, node); |
| 1804 | |
| 1805 | handle->buffer->marked = 0; |
| 1806 | |
| 1807 | } |
| 1808 | mutex_unlock(&client->lock); |
| 1809 | |
| 1810 | } |
| 1811 | |
| 1812 | for (n = rb_first(&dev->user_clients); n; n = rb_next(n)) { |
| 1813 | struct ion_client *client = rb_entry(n, struct ion_client, |
| 1814 | node); |
| 1815 | |
| 1816 | mutex_lock(&client->lock); |
| 1817 | for (n2 = rb_first(&client->handles); n2; n2 = rb_next(n2)) { |
| 1818 | struct ion_handle *handle = rb_entry(n2, |
| 1819 | struct ion_handle, node); |
| 1820 | |
| 1821 | handle->buffer->marked = 0; |
| 1822 | |
| 1823 | } |
| 1824 | mutex_unlock(&client->lock); |
| 1825 | |
| 1826 | } |
| 1827 | /* And anyone still marked as a 1 means a leaked handle somewhere */ |
| 1828 | for (n = rb_first(&dev->buffers); n; n = rb_next(n)) { |
| 1829 | struct ion_buffer *buf = rb_entry(n, struct ion_buffer, |
| 1830 | node); |
| 1831 | |
| 1832 | if (buf->marked == 1) |
| 1833 | seq_printf(s, "%16.x %16.s %16.x %16.d\n", |
| 1834 | (int)buf, buf->heap->name, buf->size, |
| 1835 | atomic_read(&buf->ref.refcount)); |
| 1836 | } |
| 1837 | mutex_unlock(&dev->lock); |
| 1838 | return 0; |
| 1839 | } |
| 1840 | |
| 1841 | static int ion_debug_leak_open(struct inode *inode, struct file *file) |
| 1842 | { |
| 1843 | return single_open(file, ion_debug_leak_show, inode->i_private); |
| 1844 | } |
| 1845 | |
| 1846 | static const struct file_operations debug_leak_fops = { |
| 1847 | .open = ion_debug_leak_open, |
| 1848 | .read = seq_read, |
| 1849 | .llseek = seq_lseek, |
| 1850 | .release = single_release, |
| 1851 | }; |
| 1852 | |
| 1853 | |
| 1854 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1855 | struct ion_device *ion_device_create(long (*custom_ioctl) |
| 1856 | (struct ion_client *client, |
| 1857 | unsigned int cmd, |
| 1858 | unsigned long arg)) |
| 1859 | { |
| 1860 | struct ion_device *idev; |
| 1861 | int ret; |
| 1862 | |
| 1863 | idev = kzalloc(sizeof(struct ion_device), GFP_KERNEL); |
| 1864 | if (!idev) |
| 1865 | return ERR_PTR(-ENOMEM); |
| 1866 | |
| 1867 | idev->dev.minor = MISC_DYNAMIC_MINOR; |
| 1868 | idev->dev.name = "ion"; |
| 1869 | idev->dev.fops = &ion_fops; |
| 1870 | idev->dev.parent = NULL; |
| 1871 | ret = misc_register(&idev->dev); |
| 1872 | if (ret) { |
| 1873 | pr_err("ion: failed to register misc device.\n"); |
| 1874 | return ERR_PTR(ret); |
| 1875 | } |
| 1876 | |
| 1877 | idev->debug_root = debugfs_create_dir("ion", NULL); |
| 1878 | if (IS_ERR_OR_NULL(idev->debug_root)) |
| 1879 | pr_err("ion: failed to create debug files.\n"); |
| 1880 | |
| 1881 | idev->custom_ioctl = custom_ioctl; |
| 1882 | idev->buffers = RB_ROOT; |
| 1883 | mutex_init(&idev->lock); |
| 1884 | idev->heaps = RB_ROOT; |
| 1885 | idev->user_clients = RB_ROOT; |
| 1886 | idev->kernel_clients = RB_ROOT; |
Laura Abbott | 404f824 | 2011-10-31 14:22:53 -0700 | [diff] [blame] | 1887 | debugfs_create_file("check_leaked_fds", 0664, idev->debug_root, idev, |
| 1888 | &debug_leak_fops); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1889 | return idev; |
| 1890 | } |
| 1891 | |
| 1892 | void ion_device_destroy(struct ion_device *dev) |
| 1893 | { |
| 1894 | misc_deregister(&dev->dev); |
| 1895 | /* XXX need to free the heaps and clients ? */ |
| 1896 | kfree(dev); |
| 1897 | } |