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