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 | e8a3197 | 2012-05-16 13:11:41 -0700 | [diff] [blame^] | 1066 | unsigned int name_len; |
| 1067 | |
| 1068 | if (!name) { |
| 1069 | pr_err("%s: Name cannot be null\n", __func__); |
| 1070 | return ERR_PTR(-EINVAL); |
| 1071 | } |
| 1072 | name_len = strnlen(name, 64); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1073 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1074 | get_task_struct(current->group_leader); |
| 1075 | task_lock(current->group_leader); |
Rebecca Schultz Zavin | 83e3dab | 2011-07-01 20:41:25 -0700 | [diff] [blame] | 1076 | pid = task_pid_nr(current->group_leader); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1077 | /* don't bother to store task struct for kernel threads, |
| 1078 | they can't be killed anyway */ |
| 1079 | if (current->group_leader->flags & PF_KTHREAD) { |
| 1080 | put_task_struct(current->group_leader); |
| 1081 | task = NULL; |
| 1082 | } else { |
| 1083 | task = current->group_leader; |
| 1084 | } |
| 1085 | task_unlock(current->group_leader); |
Rebecca Schultz Zavin | 83e3dab | 2011-07-01 20:41:25 -0700 | [diff] [blame] | 1086 | |
| 1087 | /* if this isn't a kernel thread, see if a client already |
| 1088 | exists */ |
| 1089 | if (task) { |
| 1090 | client = ion_client_lookup(dev, task); |
| 1091 | if (!IS_ERR_OR_NULL(client)) { |
| 1092 | put_task_struct(current->group_leader); |
| 1093 | return client; |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | client = kzalloc(sizeof(struct ion_client), GFP_KERNEL); |
| 1098 | if (!client) { |
| 1099 | put_task_struct(current->group_leader); |
| 1100 | return ERR_PTR(-ENOMEM); |
| 1101 | } |
| 1102 | |
| 1103 | client->dev = dev; |
| 1104 | client->handles = RB_ROOT; |
| 1105 | mutex_init(&client->lock); |
Olav Haugan | 63e5f3b | 2012-01-11 16:42:37 -0800 | [diff] [blame] | 1106 | |
Olav Haugan | 6625c7d1 | 2012-01-24 13:50:43 -0800 | [diff] [blame] | 1107 | client->name = kzalloc(name_len+1, GFP_KERNEL); |
Olav Haugan | 63e5f3b | 2012-01-11 16:42:37 -0800 | [diff] [blame] | 1108 | if (!client->name) { |
| 1109 | put_task_struct(current->group_leader); |
| 1110 | kfree(client); |
| 1111 | return ERR_PTR(-ENOMEM); |
| 1112 | } else { |
Olav Haugan | 6625c7d1 | 2012-01-24 13:50:43 -0800 | [diff] [blame] | 1113 | strlcpy(client->name, name, name_len+1); |
Olav Haugan | 63e5f3b | 2012-01-11 16:42:37 -0800 | [diff] [blame] | 1114 | } |
| 1115 | |
Rebecca Schultz Zavin | 83e3dab | 2011-07-01 20:41:25 -0700 | [diff] [blame] | 1116 | client->heap_mask = heap_mask; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1117 | client->task = task; |
Rebecca Schultz Zavin | 83e3dab | 2011-07-01 20:41:25 -0700 | [diff] [blame] | 1118 | client->pid = pid; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1119 | kref_init(&client->ref); |
| 1120 | |
| 1121 | mutex_lock(&dev->lock); |
| 1122 | if (task) { |
| 1123 | p = &dev->user_clients.rb_node; |
| 1124 | while (*p) { |
| 1125 | parent = *p; |
| 1126 | entry = rb_entry(parent, struct ion_client, node); |
| 1127 | |
| 1128 | if (task < entry->task) |
| 1129 | p = &(*p)->rb_left; |
| 1130 | else if (task > entry->task) |
| 1131 | p = &(*p)->rb_right; |
| 1132 | } |
| 1133 | rb_link_node(&client->node, parent, p); |
| 1134 | rb_insert_color(&client->node, &dev->user_clients); |
| 1135 | } else { |
| 1136 | p = &dev->kernel_clients.rb_node; |
| 1137 | while (*p) { |
| 1138 | parent = *p; |
| 1139 | entry = rb_entry(parent, struct ion_client, node); |
| 1140 | |
| 1141 | if (client < entry) |
| 1142 | p = &(*p)->rb_left; |
| 1143 | else if (client > entry) |
| 1144 | p = &(*p)->rb_right; |
| 1145 | } |
| 1146 | rb_link_node(&client->node, parent, p); |
| 1147 | rb_insert_color(&client->node, &dev->kernel_clients); |
| 1148 | } |
| 1149 | |
Laura Abbott | eed8603 | 2011-12-05 15:32:36 -0800 | [diff] [blame] | 1150 | |
| 1151 | client->debug_root = debugfs_create_file(name, 0664, |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1152 | dev->debug_root, client, |
| 1153 | &debug_client_fops); |
| 1154 | mutex_unlock(&dev->lock); |
| 1155 | |
| 1156 | return client; |
| 1157 | } |
| 1158 | |
Rebecca Schultz Zavin | 0b7e8ae | 2011-07-06 18:07:01 -0700 | [diff] [blame] | 1159 | static void _ion_client_destroy(struct kref *kref) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1160 | { |
Rebecca Schultz Zavin | 0b7e8ae | 2011-07-06 18:07:01 -0700 | [diff] [blame] | 1161 | struct ion_client *client = container_of(kref, struct ion_client, ref); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1162 | struct ion_device *dev = client->dev; |
| 1163 | struct rb_node *n; |
| 1164 | |
| 1165 | pr_debug("%s: %d\n", __func__, __LINE__); |
| 1166 | while ((n = rb_first(&client->handles))) { |
| 1167 | struct ion_handle *handle = rb_entry(n, struct ion_handle, |
| 1168 | node); |
| 1169 | ion_handle_destroy(&handle->ref); |
| 1170 | } |
| 1171 | mutex_lock(&dev->lock); |
| 1172 | if (client->task) { |
| 1173 | rb_erase(&client->node, &dev->user_clients); |
| 1174 | put_task_struct(client->task); |
| 1175 | } else { |
| 1176 | rb_erase(&client->node, &dev->kernel_clients); |
| 1177 | } |
| 1178 | debugfs_remove_recursive(client->debug_root); |
| 1179 | mutex_unlock(&dev->lock); |
| 1180 | |
Olav Haugan | 63e5f3b | 2012-01-11 16:42:37 -0800 | [diff] [blame] | 1181 | kfree(client->name); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1182 | kfree(client); |
| 1183 | } |
| 1184 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1185 | static void ion_client_get(struct ion_client *client) |
| 1186 | { |
| 1187 | kref_get(&client->ref); |
| 1188 | } |
| 1189 | |
| 1190 | static int ion_client_put(struct ion_client *client) |
| 1191 | { |
| 1192 | return kref_put(&client->ref, _ion_client_destroy); |
| 1193 | } |
| 1194 | |
Rebecca Schultz Zavin | 0b7e8ae | 2011-07-06 18:07:01 -0700 | [diff] [blame] | 1195 | void ion_client_destroy(struct ion_client *client) |
| 1196 | { |
Jordan Crouse | a75022c | 2011-10-12 16:57:47 -0600 | [diff] [blame] | 1197 | if (client) |
| 1198 | ion_client_put(client); |
Rebecca Schultz Zavin | 0b7e8ae | 2011-07-06 18:07:01 -0700 | [diff] [blame] | 1199 | } |
Olav Haugan | bd2b692 | 2012-01-25 09:28:55 -0800 | [diff] [blame] | 1200 | EXPORT_SYMBOL(ion_client_destroy); |
Rebecca Schultz Zavin | 0b7e8ae | 2011-07-06 18:07:01 -0700 | [diff] [blame] | 1201 | |
Laura Abbott | 273dd8e | 2011-10-12 14:26:33 -0700 | [diff] [blame] | 1202 | int ion_handle_get_flags(struct ion_client *client, struct ion_handle *handle, |
| 1203 | unsigned long *flags) |
| 1204 | { |
| 1205 | struct ion_buffer *buffer; |
| 1206 | |
| 1207 | mutex_lock(&client->lock); |
| 1208 | if (!ion_handle_validate(client, handle)) { |
| 1209 | pr_err("%s: invalid handle passed to %s.\n", |
| 1210 | __func__, __func__); |
| 1211 | mutex_unlock(&client->lock); |
| 1212 | return -EINVAL; |
| 1213 | } |
| 1214 | buffer = handle->buffer; |
| 1215 | mutex_lock(&buffer->lock); |
| 1216 | *flags = buffer->flags; |
| 1217 | mutex_unlock(&buffer->lock); |
| 1218 | mutex_unlock(&client->lock); |
| 1219 | |
| 1220 | return 0; |
| 1221 | } |
| 1222 | EXPORT_SYMBOL(ion_handle_get_flags); |
| 1223 | |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 1224 | int ion_handle_get_size(struct ion_client *client, struct ion_handle *handle, |
| 1225 | unsigned long *size) |
| 1226 | { |
| 1227 | struct ion_buffer *buffer; |
| 1228 | |
| 1229 | mutex_lock(&client->lock); |
| 1230 | if (!ion_handle_validate(client, handle)) { |
| 1231 | pr_err("%s: invalid handle passed to %s.\n", |
| 1232 | __func__, __func__); |
| 1233 | mutex_unlock(&client->lock); |
| 1234 | return -EINVAL; |
| 1235 | } |
| 1236 | buffer = handle->buffer; |
| 1237 | mutex_lock(&buffer->lock); |
| 1238 | *size = buffer->size; |
| 1239 | mutex_unlock(&buffer->lock); |
| 1240 | mutex_unlock(&client->lock); |
| 1241 | |
| 1242 | return 0; |
| 1243 | } |
| 1244 | EXPORT_SYMBOL(ion_handle_get_size); |
| 1245 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1246 | static int ion_share_release(struct inode *inode, struct file* file) |
| 1247 | { |
| 1248 | struct ion_buffer *buffer = file->private_data; |
| 1249 | |
| 1250 | pr_debug("%s: %d\n", __func__, __LINE__); |
| 1251 | /* drop the reference to the buffer -- this prevents the |
| 1252 | buffer from going away because the client holding it exited |
| 1253 | while it was being passed */ |
| 1254 | ion_buffer_put(buffer); |
| 1255 | return 0; |
| 1256 | } |
| 1257 | |
| 1258 | static void ion_vma_open(struct vm_area_struct *vma) |
| 1259 | { |
| 1260 | |
| 1261 | struct ion_buffer *buffer = vma->vm_file->private_data; |
| 1262 | struct ion_handle *handle = vma->vm_private_data; |
| 1263 | struct ion_client *client; |
| 1264 | |
| 1265 | pr_debug("%s: %d\n", __func__, __LINE__); |
| 1266 | /* check that the client still exists and take a reference so |
| 1267 | it can't go away until this vma is closed */ |
| 1268 | client = ion_client_lookup(buffer->dev, current->group_leader); |
| 1269 | if (IS_ERR_OR_NULL(client)) { |
| 1270 | vma->vm_private_data = NULL; |
| 1271 | return; |
| 1272 | } |
Laura Abbott | 0f2175b | 2011-12-09 14:26:07 -0800 | [diff] [blame] | 1273 | ion_handle_get(handle); |
Laura Abbott | 7716850 | 2011-12-05 11:06:24 -0800 | [diff] [blame] | 1274 | mutex_lock(&buffer->lock); |
| 1275 | buffer->umap_cnt++; |
| 1276 | mutex_unlock(&buffer->lock); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1277 | pr_debug("%s: %d client_cnt %d handle_cnt %d alloc_cnt %d\n", |
| 1278 | __func__, __LINE__, |
| 1279 | atomic_read(&client->ref.refcount), |
| 1280 | atomic_read(&handle->ref.refcount), |
| 1281 | atomic_read(&buffer->ref.refcount)); |
| 1282 | } |
| 1283 | |
| 1284 | static void ion_vma_close(struct vm_area_struct *vma) |
| 1285 | { |
| 1286 | struct ion_handle *handle = vma->vm_private_data; |
| 1287 | struct ion_buffer *buffer = vma->vm_file->private_data; |
| 1288 | struct ion_client *client; |
| 1289 | |
| 1290 | pr_debug("%s: %d\n", __func__, __LINE__); |
| 1291 | /* this indicates the client is gone, nothing to do here */ |
| 1292 | if (!handle) |
| 1293 | return; |
| 1294 | client = handle->client; |
Laura Abbott | 7716850 | 2011-12-05 11:06:24 -0800 | [diff] [blame] | 1295 | mutex_lock(&buffer->lock); |
| 1296 | buffer->umap_cnt--; |
| 1297 | mutex_unlock(&buffer->lock); |
Laura Abbott | a683509 | 2011-11-14 15:27:02 -0800 | [diff] [blame] | 1298 | |
| 1299 | if (buffer->heap->ops->unmap_user) |
| 1300 | buffer->heap->ops->unmap_user(buffer->heap, buffer); |
| 1301 | |
| 1302 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1303 | pr_debug("%s: %d client_cnt %d handle_cnt %d alloc_cnt %d\n", |
| 1304 | __func__, __LINE__, |
| 1305 | atomic_read(&client->ref.refcount), |
| 1306 | atomic_read(&handle->ref.refcount), |
| 1307 | atomic_read(&buffer->ref.refcount)); |
Laura Abbott | ec149ff | 2012-01-26 13:33:11 -0800 | [diff] [blame] | 1308 | mutex_lock(&client->lock); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1309 | ion_handle_put(handle); |
Laura Abbott | ec149ff | 2012-01-26 13:33:11 -0800 | [diff] [blame] | 1310 | mutex_unlock(&client->lock); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1311 | ion_client_put(client); |
| 1312 | pr_debug("%s: %d client_cnt %d handle_cnt %d alloc_cnt %d\n", |
| 1313 | __func__, __LINE__, |
| 1314 | atomic_read(&client->ref.refcount), |
| 1315 | atomic_read(&handle->ref.refcount), |
| 1316 | atomic_read(&buffer->ref.refcount)); |
| 1317 | } |
| 1318 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1319 | static struct vm_operations_struct ion_vm_ops = { |
| 1320 | .open = ion_vma_open, |
| 1321 | .close = ion_vma_close, |
| 1322 | }; |
| 1323 | |
| 1324 | static int ion_share_mmap(struct file *file, struct vm_area_struct *vma) |
| 1325 | { |
| 1326 | struct ion_buffer *buffer = file->private_data; |
| 1327 | unsigned long size = vma->vm_end - vma->vm_start; |
| 1328 | struct ion_client *client; |
| 1329 | struct ion_handle *handle; |
| 1330 | int ret; |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 1331 | unsigned long flags = file->f_flags & O_DSYNC ? |
| 1332 | ION_SET_CACHE(UNCACHED) : |
| 1333 | ION_SET_CACHE(CACHED); |
| 1334 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1335 | |
| 1336 | pr_debug("%s: %d\n", __func__, __LINE__); |
| 1337 | /* make sure the client still exists, it's possible for the client to |
| 1338 | have gone away but the map/share fd still to be around, take |
| 1339 | a reference to it so it can't go away while this mapping exists */ |
| 1340 | client = ion_client_lookup(buffer->dev, current->group_leader); |
| 1341 | if (IS_ERR_OR_NULL(client)) { |
| 1342 | pr_err("%s: trying to mmap an ion handle in a process with no " |
| 1343 | "ion client\n", __func__); |
| 1344 | return -EINVAL; |
| 1345 | } |
| 1346 | |
| 1347 | if ((size > buffer->size) || (size + (vma->vm_pgoff << PAGE_SHIFT) > |
| 1348 | buffer->size)) { |
| 1349 | pr_err("%s: trying to map larger area than handle has available" |
| 1350 | "\n", __func__); |
| 1351 | ret = -EINVAL; |
| 1352 | goto err; |
| 1353 | } |
| 1354 | |
| 1355 | /* find the handle and take a reference to it */ |
| 1356 | handle = ion_import(client, buffer); |
| 1357 | if (IS_ERR_OR_NULL(handle)) { |
| 1358 | ret = -EINVAL; |
| 1359 | goto err; |
| 1360 | } |
| 1361 | |
| 1362 | if (!handle->buffer->heap->ops->map_user) { |
| 1363 | pr_err("%s: this heap does not define a method for mapping " |
| 1364 | "to userspace\n", __func__); |
| 1365 | ret = -EINVAL; |
| 1366 | goto err1; |
| 1367 | } |
| 1368 | |
| 1369 | mutex_lock(&buffer->lock); |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 1370 | |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 1371 | if (ion_validate_buffer_flags(buffer, flags)) { |
| 1372 | ret = -EEXIST; |
| 1373 | mutex_unlock(&buffer->lock); |
| 1374 | goto err1; |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 1375 | } |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 1376 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1377 | /* now map it to userspace */ |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 1378 | ret = buffer->heap->ops->map_user(buffer->heap, buffer, vma, |
| 1379 | flags); |
Laura Abbott | e8bc7aa | 2011-12-09 14:49:33 -0800 | [diff] [blame] | 1380 | |
| 1381 | buffer->umap_cnt++; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1382 | if (ret) { |
| 1383 | pr_err("%s: failure mapping buffer to userspace\n", |
| 1384 | __func__); |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 1385 | goto err2; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1386 | } |
Laura Abbott | e8bc7aa | 2011-12-09 14:49:33 -0800 | [diff] [blame] | 1387 | mutex_unlock(&buffer->lock); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1388 | |
| 1389 | vma->vm_ops = &ion_vm_ops; |
| 1390 | /* move the handle into the vm_private_data so we can access it from |
| 1391 | vma_open/close */ |
| 1392 | vma->vm_private_data = handle; |
| 1393 | pr_debug("%s: %d client_cnt %d handle_cnt %d alloc_cnt %d\n", |
| 1394 | __func__, __LINE__, |
| 1395 | atomic_read(&client->ref.refcount), |
| 1396 | atomic_read(&handle->ref.refcount), |
| 1397 | atomic_read(&buffer->ref.refcount)); |
| 1398 | return 0; |
| 1399 | |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 1400 | err2: |
| 1401 | buffer->umap_cnt--; |
Laura Abbott | e8bc7aa | 2011-12-09 14:49:33 -0800 | [diff] [blame] | 1402 | mutex_unlock(&buffer->lock); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1403 | /* drop the reference to the handle */ |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 1404 | err1: |
Laura Abbott | ec149ff | 2012-01-26 13:33:11 -0800 | [diff] [blame] | 1405 | mutex_lock(&client->lock); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1406 | ion_handle_put(handle); |
Laura Abbott | ec149ff | 2012-01-26 13:33:11 -0800 | [diff] [blame] | 1407 | mutex_unlock(&client->lock); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1408 | err: |
Iliyan Malchev | 3fe2436 | 2011-08-09 14:42:08 -0700 | [diff] [blame] | 1409 | /* drop the reference to the client */ |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1410 | ion_client_put(client); |
| 1411 | return ret; |
| 1412 | } |
| 1413 | |
| 1414 | static const struct file_operations ion_share_fops = { |
| 1415 | .owner = THIS_MODULE, |
| 1416 | .release = ion_share_release, |
| 1417 | .mmap = ion_share_mmap, |
| 1418 | }; |
| 1419 | |
| 1420 | static int ion_ioctl_share(struct file *parent, struct ion_client *client, |
| 1421 | struct ion_handle *handle) |
| 1422 | { |
| 1423 | int fd = get_unused_fd(); |
| 1424 | struct file *file; |
| 1425 | |
| 1426 | if (fd < 0) |
| 1427 | return -ENFILE; |
| 1428 | |
| 1429 | file = anon_inode_getfile("ion_share_fd", &ion_share_fops, |
| 1430 | handle->buffer, O_RDWR); |
| 1431 | if (IS_ERR_OR_NULL(file)) |
| 1432 | goto err; |
Laura Abbott | 4b5d048 | 2011-09-27 18:35:14 -0700 | [diff] [blame] | 1433 | |
| 1434 | if (parent->f_flags & O_DSYNC) |
| 1435 | file->f_flags |= O_DSYNC; |
| 1436 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1437 | ion_buffer_get(handle->buffer); |
| 1438 | fd_install(fd, file); |
| 1439 | |
| 1440 | return fd; |
| 1441 | |
| 1442 | err: |
| 1443 | put_unused_fd(fd); |
| 1444 | return -ENFILE; |
| 1445 | } |
| 1446 | |
| 1447 | static long ion_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
| 1448 | { |
| 1449 | struct ion_client *client = filp->private_data; |
| 1450 | |
| 1451 | switch (cmd) { |
| 1452 | case ION_IOC_ALLOC: |
| 1453 | { |
| 1454 | struct ion_allocation_data data; |
| 1455 | |
| 1456 | if (copy_from_user(&data, (void __user *)arg, sizeof(data))) |
| 1457 | return -EFAULT; |
| 1458 | data.handle = ion_alloc(client, data.len, data.align, |
| 1459 | data.flags); |
Laura Abbott | e1b9ce5 | 2011-11-11 18:31:39 -0800 | [diff] [blame] | 1460 | |
| 1461 | if (IS_ERR_OR_NULL(data.handle)) |
Olav Haugan | b06ee07 | 2011-12-13 15:31:41 -0800 | [diff] [blame] | 1462 | return -ENOMEM; |
Laura Abbott | e1b9ce5 | 2011-11-11 18:31:39 -0800 | [diff] [blame] | 1463 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1464 | if (copy_to_user((void __user *)arg, &data, sizeof(data))) |
| 1465 | return -EFAULT; |
| 1466 | break; |
| 1467 | } |
| 1468 | case ION_IOC_FREE: |
| 1469 | { |
| 1470 | struct ion_handle_data data; |
Rebecca Schultz Zavin | e6ee124 | 2011-06-30 12:19:55 -0700 | [diff] [blame] | 1471 | bool valid; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1472 | |
| 1473 | if (copy_from_user(&data, (void __user *)arg, |
| 1474 | sizeof(struct ion_handle_data))) |
| 1475 | return -EFAULT; |
| 1476 | mutex_lock(&client->lock); |
Rebecca Schultz Zavin | e6ee124 | 2011-06-30 12:19:55 -0700 | [diff] [blame] | 1477 | valid = ion_handle_validate(client, data.handle); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1478 | mutex_unlock(&client->lock); |
Rebecca Schultz Zavin | e6ee124 | 2011-06-30 12:19:55 -0700 | [diff] [blame] | 1479 | if (!valid) |
| 1480 | return -EINVAL; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1481 | ion_free(client, data.handle); |
| 1482 | break; |
| 1483 | } |
| 1484 | case ION_IOC_MAP: |
| 1485 | case ION_IOC_SHARE: |
| 1486 | { |
| 1487 | struct ion_fd_data data; |
| 1488 | |
| 1489 | if (copy_from_user(&data, (void __user *)arg, sizeof(data))) |
| 1490 | return -EFAULT; |
| 1491 | mutex_lock(&client->lock); |
| 1492 | if (!ion_handle_validate(client, data.handle)) { |
| 1493 | pr_err("%s: invalid handle passed to share ioctl.\n", |
| 1494 | __func__); |
Rebecca Schultz Zavin | e6ee124 | 2011-06-30 12:19:55 -0700 | [diff] [blame] | 1495 | mutex_unlock(&client->lock); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1496 | return -EINVAL; |
| 1497 | } |
| 1498 | data.fd = ion_ioctl_share(filp, client, data.handle); |
| 1499 | mutex_unlock(&client->lock); |
| 1500 | if (copy_to_user((void __user *)arg, &data, sizeof(data))) |
| 1501 | return -EFAULT; |
Olav Haugan | c2d2cf5 | 2012-05-15 14:40:11 -0700 | [diff] [blame] | 1502 | if (data.fd < 0) |
| 1503 | return data.fd; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1504 | break; |
| 1505 | } |
| 1506 | case ION_IOC_IMPORT: |
| 1507 | { |
| 1508 | struct ion_fd_data data; |
Olav Haugan | c2d2cf5 | 2012-05-15 14:40:11 -0700 | [diff] [blame] | 1509 | int ret = 0; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1510 | if (copy_from_user(&data, (void __user *)arg, |
| 1511 | sizeof(struct ion_fd_data))) |
| 1512 | return -EFAULT; |
| 1513 | |
| 1514 | data.handle = ion_import_fd(client, data.fd); |
Olav Haugan | c2d2cf5 | 2012-05-15 14:40:11 -0700 | [diff] [blame] | 1515 | if (IS_ERR(data.handle)) { |
| 1516 | ret = PTR_ERR(data.handle); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1517 | data.handle = NULL; |
Olav Haugan | c2d2cf5 | 2012-05-15 14:40:11 -0700 | [diff] [blame] | 1518 | } |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1519 | if (copy_to_user((void __user *)arg, &data, |
| 1520 | sizeof(struct ion_fd_data))) |
| 1521 | return -EFAULT; |
Olav Haugan | c2d2cf5 | 2012-05-15 14:40:11 -0700 | [diff] [blame] | 1522 | if (ret < 0) |
| 1523 | return ret; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1524 | break; |
| 1525 | } |
| 1526 | case ION_IOC_CUSTOM: |
| 1527 | { |
| 1528 | struct ion_device *dev = client->dev; |
| 1529 | struct ion_custom_data data; |
| 1530 | |
| 1531 | if (!dev->custom_ioctl) |
| 1532 | return -ENOTTY; |
| 1533 | if (copy_from_user(&data, (void __user *)arg, |
| 1534 | sizeof(struct ion_custom_data))) |
| 1535 | return -EFAULT; |
| 1536 | return dev->custom_ioctl(client, data.cmd, data.arg); |
| 1537 | } |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 1538 | case ION_IOC_CLEAN_CACHES: |
| 1539 | case ION_IOC_INV_CACHES: |
| 1540 | case ION_IOC_CLEAN_INV_CACHES: |
| 1541 | { |
| 1542 | struct ion_flush_data data; |
Laura Abbott | 9fa29e8 | 2011-11-14 09:42:53 -0800 | [diff] [blame] | 1543 | unsigned long start, end; |
Laura Abbott | e80ea01 | 2011-11-18 18:36:47 -0800 | [diff] [blame] | 1544 | struct ion_handle *handle = NULL; |
| 1545 | int ret; |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 1546 | |
| 1547 | if (copy_from_user(&data, (void __user *)arg, |
| 1548 | sizeof(struct ion_flush_data))) |
| 1549 | return -EFAULT; |
| 1550 | |
Laura Abbott | 9fa29e8 | 2011-11-14 09:42:53 -0800 | [diff] [blame] | 1551 | start = (unsigned long) data.vaddr; |
| 1552 | end = (unsigned long) data.vaddr + data.length; |
| 1553 | |
| 1554 | if (check_vaddr_bounds(start, end)) { |
| 1555 | pr_err("%s: virtual address %p is out of bounds\n", |
| 1556 | __func__, data.vaddr); |
| 1557 | return -EINVAL; |
| 1558 | } |
| 1559 | |
Laura Abbott | e80ea01 | 2011-11-18 18:36:47 -0800 | [diff] [blame] | 1560 | if (!data.handle) { |
| 1561 | handle = ion_import_fd(client, data.fd); |
| 1562 | if (IS_ERR_OR_NULL(handle)) { |
| 1563 | pr_info("%s: Could not import handle: %d\n", |
| 1564 | __func__, (int)handle); |
| 1565 | return -EINVAL; |
| 1566 | } |
| 1567 | } |
| 1568 | |
| 1569 | ret = ion_do_cache_op(client, |
| 1570 | data.handle ? data.handle : handle, |
| 1571 | data.vaddr, data.offset, data.length, |
| 1572 | cmd); |
| 1573 | |
| 1574 | if (!data.handle) |
| 1575 | ion_free(client, handle); |
| 1576 | |
Olav Haugan | d7baec0 | 2012-05-15 14:38:09 -0700 | [diff] [blame] | 1577 | if (ret < 0) |
| 1578 | return ret; |
Laura Abbott | e80ea01 | 2011-11-18 18:36:47 -0800 | [diff] [blame] | 1579 | break; |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 1580 | |
| 1581 | } |
Laura Abbott | 273dd8e | 2011-10-12 14:26:33 -0700 | [diff] [blame] | 1582 | case ION_IOC_GET_FLAGS: |
| 1583 | { |
| 1584 | struct ion_flag_data data; |
| 1585 | int ret; |
| 1586 | if (copy_from_user(&data, (void __user *)arg, |
| 1587 | sizeof(struct ion_flag_data))) |
| 1588 | return -EFAULT; |
| 1589 | |
| 1590 | ret = ion_handle_get_flags(client, data.handle, &data.flags); |
| 1591 | if (ret < 0) |
| 1592 | return ret; |
| 1593 | if (copy_to_user((void __user *)arg, &data, |
| 1594 | sizeof(struct ion_flag_data))) |
| 1595 | return -EFAULT; |
| 1596 | break; |
| 1597 | } |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1598 | default: |
| 1599 | return -ENOTTY; |
| 1600 | } |
| 1601 | return 0; |
| 1602 | } |
| 1603 | |
| 1604 | static int ion_release(struct inode *inode, struct file *file) |
| 1605 | { |
| 1606 | struct ion_client *client = file->private_data; |
| 1607 | |
| 1608 | pr_debug("%s: %d\n", __func__, __LINE__); |
| 1609 | ion_client_put(client); |
| 1610 | return 0; |
| 1611 | } |
| 1612 | |
| 1613 | static int ion_open(struct inode *inode, struct file *file) |
| 1614 | { |
| 1615 | struct miscdevice *miscdev = file->private_data; |
| 1616 | struct ion_device *dev = container_of(miscdev, struct ion_device, dev); |
| 1617 | struct ion_client *client; |
Laura Abbott | eed8603 | 2011-12-05 15:32:36 -0800 | [diff] [blame] | 1618 | char debug_name[64]; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1619 | |
| 1620 | pr_debug("%s: %d\n", __func__, __LINE__); |
Laura Abbott | eed8603 | 2011-12-05 15:32:36 -0800 | [diff] [blame] | 1621 | snprintf(debug_name, 64, "%u", task_pid_nr(current->group_leader)); |
| 1622 | client = ion_client_create(dev, -1, debug_name); |
Rebecca Schultz Zavin | 6d3b958 | 2011-07-06 18:07:24 -0700 | [diff] [blame] | 1623 | if (IS_ERR_OR_NULL(client)) |
| 1624 | return PTR_ERR(client); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1625 | file->private_data = client; |
| 1626 | |
| 1627 | return 0; |
| 1628 | } |
| 1629 | |
| 1630 | static const struct file_operations ion_fops = { |
| 1631 | .owner = THIS_MODULE, |
| 1632 | .open = ion_open, |
| 1633 | .release = ion_release, |
| 1634 | .unlocked_ioctl = ion_ioctl, |
| 1635 | }; |
| 1636 | |
| 1637 | static size_t ion_debug_heap_total(struct ion_client *client, |
Laura Abbott | 3647ac3 | 2011-10-31 14:09:53 -0700 | [diff] [blame] | 1638 | enum ion_heap_ids id) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1639 | { |
| 1640 | size_t size = 0; |
| 1641 | struct rb_node *n; |
| 1642 | |
| 1643 | mutex_lock(&client->lock); |
| 1644 | for (n = rb_first(&client->handles); n; n = rb_next(n)) { |
| 1645 | struct ion_handle *handle = rb_entry(n, |
| 1646 | struct ion_handle, |
| 1647 | node); |
Laura Abbott | 3647ac3 | 2011-10-31 14:09:53 -0700 | [diff] [blame] | 1648 | if (handle->buffer->heap->id == id) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1649 | size += handle->buffer->size; |
| 1650 | } |
| 1651 | mutex_unlock(&client->lock); |
| 1652 | return size; |
| 1653 | } |
| 1654 | |
| 1655 | static int ion_debug_heap_show(struct seq_file *s, void *unused) |
| 1656 | { |
| 1657 | struct ion_heap *heap = s->private; |
| 1658 | struct ion_device *dev = heap->dev; |
| 1659 | struct rb_node *n; |
| 1660 | |
| 1661 | seq_printf(s, "%16.s %16.s %16.s\n", "client", "pid", "size"); |
| 1662 | for (n = rb_first(&dev->user_clients); n; n = rb_next(n)) { |
| 1663 | struct ion_client *client = rb_entry(n, struct ion_client, |
| 1664 | node); |
| 1665 | char task_comm[TASK_COMM_LEN]; |
Laura Abbott | 3647ac3 | 2011-10-31 14:09:53 -0700 | [diff] [blame] | 1666 | size_t size = ion_debug_heap_total(client, heap->id); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1667 | if (!size) |
| 1668 | continue; |
| 1669 | |
| 1670 | get_task_comm(task_comm, client->task); |
Laura Abbott | 8747bbe | 2011-10-31 14:18:13 -0700 | [diff] [blame] | 1671 | 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] | 1672 | size); |
| 1673 | } |
| 1674 | |
| 1675 | for (n = rb_first(&dev->kernel_clients); n; n = rb_next(n)) { |
| 1676 | struct ion_client *client = rb_entry(n, struct ion_client, |
| 1677 | node); |
Laura Abbott | 3647ac3 | 2011-10-31 14:09:53 -0700 | [diff] [blame] | 1678 | size_t size = ion_debug_heap_total(client, heap->id); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1679 | if (!size) |
| 1680 | continue; |
Laura Abbott | 8747bbe | 2011-10-31 14:18:13 -0700 | [diff] [blame] | 1681 | 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] | 1682 | size); |
| 1683 | } |
Olav Haugan | 3d4fe1a | 2012-01-13 11:42:15 -0800 | [diff] [blame] | 1684 | if (heap->ops->print_debug) |
| 1685 | heap->ops->print_debug(heap, s); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1686 | return 0; |
| 1687 | } |
| 1688 | |
| 1689 | static int ion_debug_heap_open(struct inode *inode, struct file *file) |
| 1690 | { |
| 1691 | return single_open(file, ion_debug_heap_show, inode->i_private); |
| 1692 | } |
| 1693 | |
| 1694 | static const struct file_operations debug_heap_fops = { |
| 1695 | .open = ion_debug_heap_open, |
| 1696 | .read = seq_read, |
| 1697 | .llseek = seq_lseek, |
| 1698 | .release = single_release, |
| 1699 | }; |
| 1700 | |
| 1701 | void ion_device_add_heap(struct ion_device *dev, struct ion_heap *heap) |
| 1702 | { |
| 1703 | struct rb_node **p = &dev->heaps.rb_node; |
| 1704 | struct rb_node *parent = NULL; |
| 1705 | struct ion_heap *entry; |
| 1706 | |
| 1707 | heap->dev = dev; |
| 1708 | mutex_lock(&dev->lock); |
| 1709 | while (*p) { |
| 1710 | parent = *p; |
| 1711 | entry = rb_entry(parent, struct ion_heap, node); |
| 1712 | |
Rebecca Schultz Zavin | e6ee124 | 2011-06-30 12:19:55 -0700 | [diff] [blame] | 1713 | if (heap->id < entry->id) { |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1714 | p = &(*p)->rb_left; |
Rebecca Schultz Zavin | e6ee124 | 2011-06-30 12:19:55 -0700 | [diff] [blame] | 1715 | } else if (heap->id > entry->id ) { |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1716 | p = &(*p)->rb_right; |
| 1717 | } else { |
| 1718 | pr_err("%s: can not insert multiple heaps with " |
Rebecca Schultz Zavin | e6ee124 | 2011-06-30 12:19:55 -0700 | [diff] [blame] | 1719 | "id %d\n", __func__, heap->id); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1720 | goto end; |
| 1721 | } |
| 1722 | } |
| 1723 | |
| 1724 | rb_link_node(&heap->node, parent, p); |
| 1725 | rb_insert_color(&heap->node, &dev->heaps); |
| 1726 | debugfs_create_file(heap->name, 0664, dev->debug_root, heap, |
| 1727 | &debug_heap_fops); |
| 1728 | end: |
| 1729 | mutex_unlock(&dev->lock); |
| 1730 | } |
| 1731 | |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 1732 | int ion_secure_heap(struct ion_device *dev, int heap_id) |
| 1733 | { |
| 1734 | struct rb_node *n; |
| 1735 | int ret_val = 0; |
| 1736 | |
| 1737 | /* |
| 1738 | * traverse the list of heaps available in this system |
| 1739 | * and find the heap that is specified. |
| 1740 | */ |
| 1741 | mutex_lock(&dev->lock); |
| 1742 | for (n = rb_first(&dev->heaps); n != NULL; n = rb_next(n)) { |
| 1743 | struct ion_heap *heap = rb_entry(n, struct ion_heap, node); |
| 1744 | if (heap->type != ION_HEAP_TYPE_CP) |
| 1745 | continue; |
| 1746 | if (ION_HEAP(heap->id) != heap_id) |
| 1747 | continue; |
| 1748 | if (heap->ops->secure_heap) |
| 1749 | ret_val = heap->ops->secure_heap(heap); |
| 1750 | else |
| 1751 | ret_val = -EINVAL; |
| 1752 | break; |
| 1753 | } |
| 1754 | mutex_unlock(&dev->lock); |
| 1755 | return ret_val; |
| 1756 | } |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 1757 | |
| 1758 | int ion_unsecure_heap(struct ion_device *dev, int heap_id) |
| 1759 | { |
| 1760 | struct rb_node *n; |
| 1761 | int ret_val = 0; |
| 1762 | |
| 1763 | /* |
| 1764 | * traverse the list of heaps available in this system |
| 1765 | * and find the heap that is specified. |
| 1766 | */ |
| 1767 | mutex_lock(&dev->lock); |
| 1768 | for (n = rb_first(&dev->heaps); n != NULL; n = rb_next(n)) { |
| 1769 | struct ion_heap *heap = rb_entry(n, struct ion_heap, node); |
| 1770 | if (heap->type != ION_HEAP_TYPE_CP) |
| 1771 | continue; |
| 1772 | if (ION_HEAP(heap->id) != heap_id) |
| 1773 | continue; |
| 1774 | if (heap->ops->secure_heap) |
| 1775 | ret_val = heap->ops->unsecure_heap(heap); |
| 1776 | else |
| 1777 | ret_val = -EINVAL; |
| 1778 | break; |
| 1779 | } |
| 1780 | mutex_unlock(&dev->lock); |
| 1781 | return ret_val; |
| 1782 | } |
Olav Haugan | 0a85251 | 2012-01-09 10:20:55 -0800 | [diff] [blame] | 1783 | |
Laura Abbott | 404f824 | 2011-10-31 14:22:53 -0700 | [diff] [blame] | 1784 | static int ion_debug_leak_show(struct seq_file *s, void *unused) |
| 1785 | { |
| 1786 | struct ion_device *dev = s->private; |
| 1787 | struct rb_node *n; |
| 1788 | struct rb_node *n2; |
| 1789 | |
| 1790 | /* mark all buffers as 1 */ |
| 1791 | seq_printf(s, "%16.s %16.s %16.s %16.s\n", "buffer", "heap", "size", |
| 1792 | "ref cnt"); |
| 1793 | mutex_lock(&dev->lock); |
| 1794 | for (n = rb_first(&dev->buffers); n; n = rb_next(n)) { |
| 1795 | struct ion_buffer *buf = rb_entry(n, struct ion_buffer, |
| 1796 | node); |
| 1797 | |
| 1798 | buf->marked = 1; |
| 1799 | } |
| 1800 | |
| 1801 | /* now see which buffers we can access */ |
| 1802 | for (n = rb_first(&dev->kernel_clients); n; n = rb_next(n)) { |
| 1803 | struct ion_client *client = rb_entry(n, struct ion_client, |
| 1804 | node); |
| 1805 | |
| 1806 | mutex_lock(&client->lock); |
| 1807 | for (n2 = rb_first(&client->handles); n2; n2 = rb_next(n2)) { |
| 1808 | struct ion_handle *handle = rb_entry(n2, |
| 1809 | struct ion_handle, node); |
| 1810 | |
| 1811 | handle->buffer->marked = 0; |
| 1812 | |
| 1813 | } |
| 1814 | mutex_unlock(&client->lock); |
| 1815 | |
| 1816 | } |
| 1817 | |
| 1818 | for (n = rb_first(&dev->user_clients); n; n = rb_next(n)) { |
| 1819 | struct ion_client *client = rb_entry(n, struct ion_client, |
| 1820 | node); |
| 1821 | |
| 1822 | mutex_lock(&client->lock); |
| 1823 | for (n2 = rb_first(&client->handles); n2; n2 = rb_next(n2)) { |
| 1824 | struct ion_handle *handle = rb_entry(n2, |
| 1825 | struct ion_handle, node); |
| 1826 | |
| 1827 | handle->buffer->marked = 0; |
| 1828 | |
| 1829 | } |
| 1830 | mutex_unlock(&client->lock); |
| 1831 | |
| 1832 | } |
| 1833 | /* And anyone still marked as a 1 means a leaked handle somewhere */ |
| 1834 | for (n = rb_first(&dev->buffers); n; n = rb_next(n)) { |
| 1835 | struct ion_buffer *buf = rb_entry(n, struct ion_buffer, |
| 1836 | node); |
| 1837 | |
| 1838 | if (buf->marked == 1) |
| 1839 | seq_printf(s, "%16.x %16.s %16.x %16.d\n", |
| 1840 | (int)buf, buf->heap->name, buf->size, |
| 1841 | atomic_read(&buf->ref.refcount)); |
| 1842 | } |
| 1843 | mutex_unlock(&dev->lock); |
| 1844 | return 0; |
| 1845 | } |
| 1846 | |
| 1847 | static int ion_debug_leak_open(struct inode *inode, struct file *file) |
| 1848 | { |
| 1849 | return single_open(file, ion_debug_leak_show, inode->i_private); |
| 1850 | } |
| 1851 | |
| 1852 | static const struct file_operations debug_leak_fops = { |
| 1853 | .open = ion_debug_leak_open, |
| 1854 | .read = seq_read, |
| 1855 | .llseek = seq_lseek, |
| 1856 | .release = single_release, |
| 1857 | }; |
| 1858 | |
| 1859 | |
| 1860 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1861 | struct ion_device *ion_device_create(long (*custom_ioctl) |
| 1862 | (struct ion_client *client, |
| 1863 | unsigned int cmd, |
| 1864 | unsigned long arg)) |
| 1865 | { |
| 1866 | struct ion_device *idev; |
| 1867 | int ret; |
| 1868 | |
| 1869 | idev = kzalloc(sizeof(struct ion_device), GFP_KERNEL); |
| 1870 | if (!idev) |
| 1871 | return ERR_PTR(-ENOMEM); |
| 1872 | |
| 1873 | idev->dev.minor = MISC_DYNAMIC_MINOR; |
| 1874 | idev->dev.name = "ion"; |
| 1875 | idev->dev.fops = &ion_fops; |
| 1876 | idev->dev.parent = NULL; |
| 1877 | ret = misc_register(&idev->dev); |
| 1878 | if (ret) { |
| 1879 | pr_err("ion: failed to register misc device.\n"); |
| 1880 | return ERR_PTR(ret); |
| 1881 | } |
| 1882 | |
| 1883 | idev->debug_root = debugfs_create_dir("ion", NULL); |
| 1884 | if (IS_ERR_OR_NULL(idev->debug_root)) |
| 1885 | pr_err("ion: failed to create debug files.\n"); |
| 1886 | |
| 1887 | idev->custom_ioctl = custom_ioctl; |
| 1888 | idev->buffers = RB_ROOT; |
| 1889 | mutex_init(&idev->lock); |
| 1890 | idev->heaps = RB_ROOT; |
| 1891 | idev->user_clients = RB_ROOT; |
| 1892 | idev->kernel_clients = RB_ROOT; |
Laura Abbott | 404f824 | 2011-10-31 14:22:53 -0700 | [diff] [blame] | 1893 | debugfs_create_file("check_leaked_fds", 0664, idev->debug_root, idev, |
| 1894 | &debug_leak_fops); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1895 | return idev; |
| 1896 | } |
| 1897 | |
| 1898 | void ion_device_destroy(struct ion_device *dev) |
| 1899 | { |
| 1900 | misc_deregister(&dev->dev); |
| 1901 | /* XXX need to free the heaps and clients ? */ |
| 1902 | kfree(dev); |
| 1903 | } |