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. |
| 5 | * |
| 6 | * This software is licensed under the terms of the GNU General Public |
| 7 | * License version 2, as published by the Free Software Foundation, and |
| 8 | * may be copied, distributed, and modified under those terms. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | */ |
| 16 | |
| 17 | #include <linux/device.h> |
| 18 | #include <linux/file.h> |
| 19 | #include <linux/fs.h> |
| 20 | #include <linux/anon_inodes.h> |
| 21 | #include <linux/ion.h> |
| 22 | #include <linux/list.h> |
| 23 | #include <linux/miscdevice.h> |
| 24 | #include <linux/mm.h> |
| 25 | #include <linux/mm_types.h> |
| 26 | #include <linux/rbtree.h> |
| 27 | #include <linux/sched.h> |
| 28 | #include <linux/slab.h> |
| 29 | #include <linux/seq_file.h> |
| 30 | #include <linux/uaccess.h> |
| 31 | #include <linux/debugfs.h> |
| 32 | |
| 33 | #include "ion_priv.h" |
| 34 | #define DEBUG |
| 35 | |
| 36 | /** |
| 37 | * struct ion_device - the metadata of the ion device node |
| 38 | * @dev: the actual misc device |
| 39 | * @buffers: an rb tree of all the existing buffers |
| 40 | * @lock: lock protecting the buffers & heaps trees |
| 41 | * @heaps: list of all the heaps in the system |
| 42 | * @user_clients: list of all the clients created from userspace |
| 43 | */ |
| 44 | struct ion_device { |
| 45 | struct miscdevice dev; |
| 46 | struct rb_root buffers; |
| 47 | struct mutex lock; |
| 48 | struct rb_root heaps; |
| 49 | long (*custom_ioctl) (struct ion_client *client, unsigned int cmd, |
| 50 | unsigned long arg); |
| 51 | struct rb_root user_clients; |
| 52 | struct rb_root kernel_clients; |
| 53 | struct dentry *debug_root; |
| 54 | }; |
| 55 | |
| 56 | /** |
| 57 | * struct ion_client - a process/hw block local address space |
| 58 | * @ref: for reference counting the client |
| 59 | * @node: node in the tree of all clients |
| 60 | * @dev: backpointer to ion device |
| 61 | * @handles: an rb tree of all the handles in this client |
| 62 | * @lock: lock protecting the tree of handles |
| 63 | * @heap_mask: mask of all supported heaps |
| 64 | * @name: used for debugging |
| 65 | * @task: used for debugging |
| 66 | * |
| 67 | * A client represents a list of buffers this client may access. |
| 68 | * The mutex stored here is used to protect both handles tree |
| 69 | * as well as the handles themselves, and should be held while modifying either. |
| 70 | */ |
| 71 | struct ion_client { |
| 72 | struct kref ref; |
| 73 | struct rb_node node; |
| 74 | struct ion_device *dev; |
| 75 | struct rb_root handles; |
| 76 | struct mutex lock; |
| 77 | unsigned int heap_mask; |
| 78 | const char *name; |
| 79 | struct task_struct *task; |
| 80 | pid_t pid; |
| 81 | struct dentry *debug_root; |
| 82 | }; |
| 83 | |
| 84 | /** |
Rebecca Schultz Zavin | e6ee124 | 2011-06-30 12:19:55 -0700 | [diff] [blame] | 85 | * ion_handle - a client local reference to a buffer |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 86 | * @ref: reference count |
| 87 | * @client: back pointer to the client the buffer resides in |
| 88 | * @buffer: pointer to the buffer |
| 89 | * @node: node in the client's handle rbtree |
Rebecca Schultz Zavin | e6ee124 | 2011-06-30 12:19:55 -0700 | [diff] [blame] | 90 | * @kmap_cnt: count of times this client has mapped to kernel |
| 91 | * @dmap_cnt: count of times this client has mapped for dma |
| 92 | * @usermap_cnt: count of times this client has mapped for userspace |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 93 | * |
| 94 | * Modifications to node, map_cnt or mapping should be protected by the |
| 95 | * lock in the client. Other fields are never changed after initialization. |
| 96 | */ |
| 97 | struct ion_handle { |
| 98 | struct kref ref; |
| 99 | struct ion_client *client; |
| 100 | struct ion_buffer *buffer; |
| 101 | struct rb_node node; |
| 102 | unsigned int kmap_cnt; |
| 103 | unsigned int dmap_cnt; |
| 104 | unsigned int usermap_cnt; |
| 105 | }; |
| 106 | |
| 107 | /* this function should only be called while dev->lock is held */ |
| 108 | static void ion_buffer_add(struct ion_device *dev, |
| 109 | struct ion_buffer *buffer) |
| 110 | { |
| 111 | struct rb_node **p = &dev->buffers.rb_node; |
| 112 | struct rb_node *parent = NULL; |
| 113 | struct ion_buffer *entry; |
| 114 | |
| 115 | while (*p) { |
| 116 | parent = *p; |
| 117 | entry = rb_entry(parent, struct ion_buffer, node); |
| 118 | |
Rebecca Schultz Zavin | f9fb95e | 2011-06-30 18:09:05 -0700 | [diff] [blame] | 119 | if (buffer < entry) { |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 120 | p = &(*p)->rb_left; |
Rebecca Schultz Zavin | f9fb95e | 2011-06-30 18:09:05 -0700 | [diff] [blame] | 121 | } else if (buffer > entry) { |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 122 | p = &(*p)->rb_right; |
Rebecca Schultz Zavin | f9fb95e | 2011-06-30 18:09:05 -0700 | [diff] [blame] | 123 | } else { |
Rebecca Schultz Zavin | e6ee124 | 2011-06-30 12:19:55 -0700 | [diff] [blame] | 124 | pr_err("%s: buffer already found.", __func__); |
| 125 | BUG(); |
Rebecca Schultz Zavin | f9fb95e | 2011-06-30 18:09:05 -0700 | [diff] [blame] | 126 | } |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | rb_link_node(&buffer->node, parent, p); |
| 130 | rb_insert_color(&buffer->node, &dev->buffers); |
| 131 | } |
| 132 | |
| 133 | /* this function should only be called while dev->lock is held */ |
Iliyan Malchev | 3fe2436 | 2011-08-09 14:42:08 -0700 | [diff] [blame] | 134 | static struct ion_buffer *ion_buffer_create(struct ion_heap *heap, |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 135 | struct ion_device *dev, |
| 136 | unsigned long len, |
| 137 | unsigned long align, |
| 138 | unsigned long flags) |
| 139 | { |
| 140 | struct ion_buffer *buffer; |
| 141 | int ret; |
| 142 | |
| 143 | buffer = kzalloc(sizeof(struct ion_buffer), GFP_KERNEL); |
| 144 | if (!buffer) |
| 145 | return ERR_PTR(-ENOMEM); |
| 146 | |
| 147 | buffer->heap = heap; |
| 148 | kref_init(&buffer->ref); |
| 149 | |
| 150 | ret = heap->ops->allocate(heap, buffer, len, align, flags); |
Rebecca Schultz Zavin | e6ee124 | 2011-06-30 12:19:55 -0700 | [diff] [blame] | 151 | if (ret) { |
| 152 | kfree(buffer); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 153 | return ERR_PTR(ret); |
Rebecca Schultz Zavin | e6ee124 | 2011-06-30 12:19:55 -0700 | [diff] [blame] | 154 | } |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 155 | buffer->dev = dev; |
| 156 | buffer->size = len; |
| 157 | mutex_init(&buffer->lock); |
| 158 | ion_buffer_add(dev, buffer); |
| 159 | return buffer; |
| 160 | } |
| 161 | |
| 162 | static void ion_buffer_destroy(struct kref *kref) |
| 163 | { |
| 164 | struct ion_buffer *buffer = container_of(kref, struct ion_buffer, ref); |
| 165 | struct ion_device *dev = buffer->dev; |
| 166 | |
| 167 | buffer->heap->ops->free(buffer); |
| 168 | mutex_lock(&dev->lock); |
| 169 | rb_erase(&buffer->node, &dev->buffers); |
| 170 | mutex_unlock(&dev->lock); |
| 171 | kfree(buffer); |
| 172 | } |
| 173 | |
| 174 | static void ion_buffer_get(struct ion_buffer *buffer) |
| 175 | { |
| 176 | kref_get(&buffer->ref); |
| 177 | } |
| 178 | |
| 179 | static int ion_buffer_put(struct ion_buffer *buffer) |
| 180 | { |
| 181 | return kref_put(&buffer->ref, ion_buffer_destroy); |
| 182 | } |
| 183 | |
Iliyan Malchev | 3fe2436 | 2011-08-09 14:42:08 -0700 | [diff] [blame] | 184 | static struct ion_handle *ion_handle_create(struct ion_client *client, |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 185 | struct ion_buffer *buffer) |
| 186 | { |
| 187 | struct ion_handle *handle; |
| 188 | |
| 189 | handle = kzalloc(sizeof(struct ion_handle), GFP_KERNEL); |
| 190 | if (!handle) |
| 191 | return ERR_PTR(-ENOMEM); |
| 192 | kref_init(&handle->ref); |
Iliyan Malchev | 3fe2436 | 2011-08-09 14:42:08 -0700 | [diff] [blame] | 193 | rb_init_node(&handle->node); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 194 | handle->client = client; |
| 195 | ion_buffer_get(buffer); |
| 196 | handle->buffer = buffer; |
| 197 | |
| 198 | return handle; |
| 199 | } |
| 200 | |
| 201 | static void ion_handle_destroy(struct kref *kref) |
| 202 | { |
| 203 | struct ion_handle *handle = container_of(kref, struct ion_handle, ref); |
| 204 | /* XXX Can a handle be destroyed while it's map count is non-zero?: |
| 205 | if (handle->map_cnt) unmap |
| 206 | */ |
| 207 | ion_buffer_put(handle->buffer); |
| 208 | mutex_lock(&handle->client->lock); |
Iliyan Malchev | 3fe2436 | 2011-08-09 14:42:08 -0700 | [diff] [blame] | 209 | if (!RB_EMPTY_NODE(&handle->node)) |
| 210 | rb_erase(&handle->node, &handle->client->handles); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 211 | mutex_unlock(&handle->client->lock); |
| 212 | kfree(handle); |
| 213 | } |
| 214 | |
| 215 | struct ion_buffer *ion_handle_buffer(struct ion_handle *handle) |
| 216 | { |
| 217 | return handle->buffer; |
| 218 | } |
| 219 | |
| 220 | static void ion_handle_get(struct ion_handle *handle) |
| 221 | { |
| 222 | kref_get(&handle->ref); |
| 223 | } |
| 224 | |
| 225 | static int ion_handle_put(struct ion_handle *handle) |
| 226 | { |
| 227 | return kref_put(&handle->ref, ion_handle_destroy); |
| 228 | } |
| 229 | |
| 230 | static struct ion_handle *ion_handle_lookup(struct ion_client *client, |
| 231 | struct ion_buffer *buffer) |
| 232 | { |
| 233 | struct rb_node *n; |
| 234 | |
| 235 | for (n = rb_first(&client->handles); n; n = rb_next(n)) { |
| 236 | struct ion_handle *handle = rb_entry(n, struct ion_handle, |
| 237 | node); |
| 238 | if (handle->buffer == buffer) |
| 239 | return handle; |
| 240 | } |
| 241 | return NULL; |
| 242 | } |
| 243 | |
Iliyan Malchev | 3fe2436 | 2011-08-09 14:42:08 -0700 | [diff] [blame] | 244 | 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] | 245 | { |
| 246 | struct rb_node *n = client->handles.rb_node; |
| 247 | |
| 248 | while (n) { |
| 249 | struct ion_handle *handle_node = rb_entry(n, struct ion_handle, |
| 250 | node); |
| 251 | if (handle < handle_node) |
| 252 | n = n->rb_left; |
| 253 | else if (handle > handle_node) |
| 254 | n = n->rb_right; |
| 255 | else |
| 256 | return true; |
| 257 | } |
| 258 | return false; |
| 259 | } |
| 260 | |
| 261 | static void ion_handle_add(struct ion_client *client, struct ion_handle *handle) |
| 262 | { |
| 263 | struct rb_node **p = &client->handles.rb_node; |
| 264 | struct rb_node *parent = NULL; |
| 265 | struct ion_handle *entry; |
| 266 | |
| 267 | while (*p) { |
| 268 | parent = *p; |
| 269 | entry = rb_entry(parent, struct ion_handle, node); |
| 270 | |
| 271 | if (handle < entry) |
| 272 | p = &(*p)->rb_left; |
| 273 | else if (handle > entry) |
| 274 | p = &(*p)->rb_right; |
| 275 | else |
| 276 | WARN(1, "%s: buffer already found.", __func__); |
| 277 | } |
| 278 | |
| 279 | rb_link_node(&handle->node, parent, p); |
| 280 | rb_insert_color(&handle->node, &client->handles); |
| 281 | } |
| 282 | |
| 283 | struct ion_handle *ion_alloc(struct ion_client *client, size_t len, |
| 284 | size_t align, unsigned int flags) |
| 285 | { |
| 286 | struct rb_node *n; |
| 287 | struct ion_handle *handle; |
| 288 | struct ion_device *dev = client->dev; |
| 289 | struct ion_buffer *buffer = NULL; |
| 290 | |
| 291 | /* |
| 292 | * traverse the list of heaps available in this system in priority |
| 293 | * order. If the heap type is supported by the client, and matches the |
| 294 | * request of the caller allocate from it. Repeat until allocate has |
| 295 | * succeeded or all heaps have been tried |
| 296 | */ |
| 297 | mutex_lock(&dev->lock); |
| 298 | for (n = rb_first(&dev->heaps); n != NULL; n = rb_next(n)) { |
| 299 | struct ion_heap *heap = rb_entry(n, struct ion_heap, node); |
| 300 | /* if the client doesn't support this heap type */ |
| 301 | if (!((1 << heap->type) & client->heap_mask)) |
| 302 | continue; |
| 303 | /* if the caller didn't specify this heap type */ |
Rebecca Schultz Zavin | e6ee124 | 2011-06-30 12:19:55 -0700 | [diff] [blame] | 304 | if (!((1 << heap->id) & flags)) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 305 | continue; |
| 306 | buffer = ion_buffer_create(heap, dev, len, align, flags); |
| 307 | if (!IS_ERR_OR_NULL(buffer)) |
| 308 | break; |
| 309 | } |
| 310 | mutex_unlock(&dev->lock); |
| 311 | |
| 312 | if (IS_ERR_OR_NULL(buffer)) |
| 313 | return ERR_PTR(PTR_ERR(buffer)); |
| 314 | |
| 315 | handle = ion_handle_create(client, buffer); |
| 316 | |
| 317 | if (IS_ERR_OR_NULL(handle)) |
| 318 | goto end; |
| 319 | |
| 320 | /* |
| 321 | * ion_buffer_create will create a buffer with a ref_cnt of 1, |
| 322 | * and ion_handle_create will take a second reference, drop one here |
| 323 | */ |
| 324 | ion_buffer_put(buffer); |
| 325 | |
| 326 | mutex_lock(&client->lock); |
| 327 | ion_handle_add(client, handle); |
| 328 | mutex_unlock(&client->lock); |
| 329 | return handle; |
| 330 | |
| 331 | end: |
| 332 | ion_buffer_put(buffer); |
| 333 | return handle; |
| 334 | } |
| 335 | |
| 336 | void ion_free(struct ion_client *client, struct ion_handle *handle) |
| 337 | { |
Rebecca Schultz Zavin | c72866d | 2011-07-07 17:07:56 -0700 | [diff] [blame] | 338 | bool valid_handle; |
| 339 | |
| 340 | BUG_ON(client != handle->client); |
| 341 | |
| 342 | mutex_lock(&client->lock); |
| 343 | valid_handle = ion_handle_validate(client, handle); |
| 344 | mutex_unlock(&client->lock); |
| 345 | |
| 346 | if (!valid_handle) { |
| 347 | WARN("%s: invalid handle passed to free.\n", __func__); |
| 348 | return; |
| 349 | } |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 350 | ion_handle_put(handle); |
| 351 | } |
| 352 | |
| 353 | static void ion_client_get(struct ion_client *client); |
| 354 | static int ion_client_put(struct ion_client *client); |
| 355 | |
Iliyan Malchev | 3fe2436 | 2011-08-09 14:42:08 -0700 | [diff] [blame] | 356 | static bool _ion_map(int *buffer_cnt, int *handle_cnt) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 357 | { |
| 358 | bool map; |
| 359 | |
| 360 | BUG_ON(*handle_cnt != 0 && *buffer_cnt == 0); |
| 361 | |
| 362 | if (*buffer_cnt) |
| 363 | map = false; |
| 364 | else |
| 365 | map = true; |
| 366 | if (*handle_cnt == 0) |
| 367 | (*buffer_cnt)++; |
| 368 | (*handle_cnt)++; |
| 369 | return map; |
| 370 | } |
| 371 | |
Iliyan Malchev | 3fe2436 | 2011-08-09 14:42:08 -0700 | [diff] [blame] | 372 | static bool _ion_unmap(int *buffer_cnt, int *handle_cnt) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 373 | { |
| 374 | BUG_ON(*handle_cnt == 0); |
| 375 | (*handle_cnt)--; |
| 376 | if (*handle_cnt != 0) |
| 377 | return false; |
| 378 | BUG_ON(*buffer_cnt == 0); |
| 379 | (*buffer_cnt)--; |
| 380 | if (*buffer_cnt == 0) |
| 381 | return true; |
| 382 | return false; |
| 383 | } |
| 384 | |
| 385 | int ion_phys(struct ion_client *client, struct ion_handle *handle, |
| 386 | ion_phys_addr_t *addr, size_t *len) |
| 387 | { |
| 388 | struct ion_buffer *buffer; |
| 389 | int ret; |
| 390 | |
| 391 | mutex_lock(&client->lock); |
| 392 | if (!ion_handle_validate(client, handle)) { |
Rebecca Schultz Zavin | e6ee124 | 2011-06-30 12:19:55 -0700 | [diff] [blame] | 393 | mutex_unlock(&client->lock); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 394 | return -EINVAL; |
| 395 | } |
| 396 | |
| 397 | buffer = handle->buffer; |
| 398 | |
| 399 | if (!buffer->heap->ops->phys) { |
| 400 | pr_err("%s: ion_phys is not implemented by this heap.\n", |
| 401 | __func__); |
Rebecca Schultz Zavin | e6ee124 | 2011-06-30 12:19:55 -0700 | [diff] [blame] | 402 | mutex_unlock(&client->lock); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 403 | return -ENODEV; |
| 404 | } |
| 405 | mutex_unlock(&client->lock); |
| 406 | ret = buffer->heap->ops->phys(buffer->heap, buffer, addr, len); |
| 407 | return ret; |
| 408 | } |
| 409 | |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 410 | void *ion_map_kernel(struct ion_client *client, struct ion_handle *handle, |
| 411 | unsigned long flags) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 412 | { |
| 413 | struct ion_buffer *buffer; |
| 414 | void *vaddr; |
| 415 | |
| 416 | mutex_lock(&client->lock); |
| 417 | if (!ion_handle_validate(client, handle)) { |
| 418 | pr_err("%s: invalid handle passed to map_kernel.\n", |
| 419 | __func__); |
Rebecca Schultz Zavin | e6ee124 | 2011-06-30 12:19:55 -0700 | [diff] [blame] | 420 | mutex_unlock(&client->lock); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 421 | return ERR_PTR(-EINVAL); |
| 422 | } |
| 423 | |
| 424 | buffer = handle->buffer; |
| 425 | mutex_lock(&buffer->lock); |
| 426 | |
| 427 | if (!handle->buffer->heap->ops->map_kernel) { |
| 428 | pr_err("%s: map_kernel is not implemented by this heap.\n", |
| 429 | __func__); |
Rebecca Schultz Zavin | e6ee124 | 2011-06-30 12:19:55 -0700 | [diff] [blame] | 430 | mutex_unlock(&buffer->lock); |
| 431 | mutex_unlock(&client->lock); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 432 | return ERR_PTR(-ENODEV); |
| 433 | } |
| 434 | |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 435 | if (buffer->kmap_cnt || buffer->dmap_cnt || buffer->umap_cnt) { |
| 436 | if (buffer->flags != flags) { |
| 437 | pr_err("%s: buffer was already mapped with flags %lx," |
| 438 | " cannot map with flags %lx\n", __func__, |
| 439 | buffer->flags, flags); |
| 440 | vaddr = ERR_PTR(-EEXIST); |
| 441 | goto out; |
| 442 | } |
| 443 | |
| 444 | } else { |
| 445 | buffer->flags = flags; |
| 446 | } |
| 447 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 448 | if (_ion_map(&buffer->kmap_cnt, &handle->kmap_cnt)) { |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 449 | vaddr = buffer->heap->ops->map_kernel(buffer->heap, buffer, |
| 450 | flags); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 451 | if (IS_ERR_OR_NULL(vaddr)) |
| 452 | _ion_unmap(&buffer->kmap_cnt, &handle->kmap_cnt); |
| 453 | buffer->vaddr = vaddr; |
| 454 | } else { |
| 455 | vaddr = buffer->vaddr; |
| 456 | } |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 457 | |
| 458 | out: |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 459 | mutex_unlock(&buffer->lock); |
| 460 | mutex_unlock(&client->lock); |
| 461 | return vaddr; |
| 462 | } |
| 463 | |
| 464 | struct scatterlist *ion_map_dma(struct ion_client *client, |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 465 | struct ion_handle *handle, |
| 466 | unsigned long flags) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 467 | { |
| 468 | struct ion_buffer *buffer; |
| 469 | struct scatterlist *sglist; |
| 470 | |
| 471 | mutex_lock(&client->lock); |
| 472 | if (!ion_handle_validate(client, handle)) { |
| 473 | pr_err("%s: invalid handle passed to map_dma.\n", |
| 474 | __func__); |
Rebecca Schultz Zavin | e6ee124 | 2011-06-30 12:19:55 -0700 | [diff] [blame] | 475 | mutex_unlock(&client->lock); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 476 | return ERR_PTR(-EINVAL); |
| 477 | } |
| 478 | buffer = handle->buffer; |
| 479 | mutex_lock(&buffer->lock); |
| 480 | |
| 481 | if (!handle->buffer->heap->ops->map_dma) { |
| 482 | pr_err("%s: map_kernel is not implemented by this heap.\n", |
| 483 | __func__); |
Rebecca Schultz Zavin | e6ee124 | 2011-06-30 12:19:55 -0700 | [diff] [blame] | 484 | mutex_unlock(&buffer->lock); |
| 485 | mutex_unlock(&client->lock); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 486 | return ERR_PTR(-ENODEV); |
| 487 | } |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 488 | |
| 489 | if (buffer->kmap_cnt || buffer->dmap_cnt || buffer->umap_cnt) { |
| 490 | if (buffer->flags != flags) { |
| 491 | pr_err("%s: buffer was already mapped with flags %lx," |
| 492 | " cannot map with flags %lx\n", __func__, |
| 493 | buffer->flags, flags); |
| 494 | sglist = ERR_PTR(-EEXIST); |
| 495 | goto out; |
| 496 | } |
| 497 | |
| 498 | } else { |
| 499 | buffer->flags = flags; |
| 500 | } |
| 501 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 502 | if (_ion_map(&buffer->dmap_cnt, &handle->dmap_cnt)) { |
| 503 | sglist = buffer->heap->ops->map_dma(buffer->heap, buffer); |
| 504 | if (IS_ERR_OR_NULL(sglist)) |
| 505 | _ion_unmap(&buffer->dmap_cnt, &handle->dmap_cnt); |
| 506 | buffer->sglist = sglist; |
| 507 | } else { |
| 508 | sglist = buffer->sglist; |
| 509 | } |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 510 | |
| 511 | out: |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 512 | mutex_unlock(&buffer->lock); |
| 513 | mutex_unlock(&client->lock); |
| 514 | return sglist; |
| 515 | } |
| 516 | |
| 517 | void ion_unmap_kernel(struct ion_client *client, struct ion_handle *handle) |
| 518 | { |
| 519 | struct ion_buffer *buffer; |
| 520 | |
| 521 | mutex_lock(&client->lock); |
| 522 | buffer = handle->buffer; |
| 523 | mutex_lock(&buffer->lock); |
| 524 | if (_ion_unmap(&buffer->kmap_cnt, &handle->kmap_cnt)) { |
| 525 | buffer->heap->ops->unmap_kernel(buffer->heap, buffer); |
| 526 | buffer->vaddr = NULL; |
| 527 | } |
| 528 | mutex_unlock(&buffer->lock); |
| 529 | mutex_unlock(&client->lock); |
| 530 | } |
| 531 | |
| 532 | void ion_unmap_dma(struct ion_client *client, struct ion_handle *handle) |
| 533 | { |
| 534 | struct ion_buffer *buffer; |
| 535 | |
| 536 | mutex_lock(&client->lock); |
| 537 | buffer = handle->buffer; |
| 538 | mutex_lock(&buffer->lock); |
| 539 | if (_ion_unmap(&buffer->dmap_cnt, &handle->dmap_cnt)) { |
| 540 | buffer->heap->ops->unmap_dma(buffer->heap, buffer); |
| 541 | buffer->sglist = NULL; |
| 542 | } |
| 543 | mutex_unlock(&buffer->lock); |
| 544 | mutex_unlock(&client->lock); |
| 545 | } |
| 546 | |
| 547 | |
| 548 | struct ion_buffer *ion_share(struct ion_client *client, |
| 549 | struct ion_handle *handle) |
| 550 | { |
Rebecca Schultz Zavin | c72866d | 2011-07-07 17:07:56 -0700 | [diff] [blame] | 551 | bool valid_handle; |
| 552 | |
| 553 | mutex_lock(&client->lock); |
| 554 | valid_handle = ion_handle_validate(client, handle); |
| 555 | mutex_unlock(&client->lock); |
| 556 | if (!valid_handle) { |
| 557 | WARN("%s: invalid handle passed to share.\n", __func__); |
| 558 | return ERR_PTR(-EINVAL); |
| 559 | } |
| 560 | |
Iliyan Malchev | 3fe2436 | 2011-08-09 14:42:08 -0700 | [diff] [blame] | 561 | /* 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] | 562 | * to make sure the buffer doesn't go away while it's passing it |
| 563 | * to another client -- ion_free should not be called on this handle |
| 564 | * until the buffer has been imported into the other client |
| 565 | */ |
| 566 | return handle->buffer; |
| 567 | } |
| 568 | |
| 569 | struct ion_handle *ion_import(struct ion_client *client, |
| 570 | struct ion_buffer *buffer) |
| 571 | { |
| 572 | struct ion_handle *handle = NULL; |
| 573 | |
| 574 | mutex_lock(&client->lock); |
| 575 | /* if a handle exists for this buffer just take a reference to it */ |
| 576 | handle = ion_handle_lookup(client, buffer); |
| 577 | if (!IS_ERR_OR_NULL(handle)) { |
| 578 | ion_handle_get(handle); |
| 579 | goto end; |
| 580 | } |
| 581 | handle = ion_handle_create(client, buffer); |
| 582 | if (IS_ERR_OR_NULL(handle)) |
| 583 | goto end; |
| 584 | ion_handle_add(client, handle); |
| 585 | end: |
| 586 | mutex_unlock(&client->lock); |
| 587 | return handle; |
| 588 | } |
| 589 | |
| 590 | static const struct file_operations ion_share_fops; |
| 591 | |
| 592 | struct ion_handle *ion_import_fd(struct ion_client *client, int fd) |
| 593 | { |
| 594 | struct file *file = fget(fd); |
| 595 | struct ion_handle *handle; |
| 596 | |
| 597 | if (!file) { |
| 598 | pr_err("%s: imported fd not found in file table.\n", __func__); |
| 599 | return ERR_PTR(-EINVAL); |
| 600 | } |
| 601 | if (file->f_op != &ion_share_fops) { |
| 602 | pr_err("%s: imported file is not a shared ion file.\n", |
| 603 | __func__); |
| 604 | handle = ERR_PTR(-EINVAL); |
| 605 | goto end; |
| 606 | } |
| 607 | handle = ion_import(client, file->private_data); |
| 608 | end: |
| 609 | fput(file); |
| 610 | return handle; |
| 611 | } |
| 612 | |
| 613 | static int ion_debug_client_show(struct seq_file *s, void *unused) |
| 614 | { |
| 615 | struct ion_client *client = s->private; |
| 616 | struct rb_node *n; |
| 617 | size_t sizes[ION_NUM_HEAPS] = {0}; |
| 618 | const char *names[ION_NUM_HEAPS] = {0}; |
| 619 | int i; |
| 620 | |
| 621 | mutex_lock(&client->lock); |
| 622 | for (n = rb_first(&client->handles); n; n = rb_next(n)) { |
| 623 | struct ion_handle *handle = rb_entry(n, struct ion_handle, |
| 624 | node); |
| 625 | enum ion_heap_type type = handle->buffer->heap->type; |
| 626 | |
| 627 | if (!names[type]) |
| 628 | names[type] = handle->buffer->heap->name; |
| 629 | sizes[type] += handle->buffer->size; |
| 630 | } |
| 631 | mutex_unlock(&client->lock); |
| 632 | |
| 633 | seq_printf(s, "%16.16s: %16.16s\n", "heap_name", "size_in_bytes"); |
| 634 | for (i = 0; i < ION_NUM_HEAPS; i++) { |
| 635 | if (!names[i]) |
| 636 | continue; |
| 637 | seq_printf(s, "%16.16s: %16u %d\n", names[i], sizes[i], |
| 638 | atomic_read(&client->ref.refcount)); |
| 639 | } |
| 640 | return 0; |
| 641 | } |
| 642 | |
| 643 | static int ion_debug_client_open(struct inode *inode, struct file *file) |
| 644 | { |
| 645 | return single_open(file, ion_debug_client_show, inode->i_private); |
| 646 | } |
| 647 | |
| 648 | static const struct file_operations debug_client_fops = { |
| 649 | .open = ion_debug_client_open, |
| 650 | .read = seq_read, |
| 651 | .llseek = seq_lseek, |
| 652 | .release = single_release, |
| 653 | }; |
| 654 | |
Rebecca Schultz Zavin | 83e3dab | 2011-07-01 20:41:25 -0700 | [diff] [blame] | 655 | static struct ion_client *ion_client_lookup(struct ion_device *dev, |
| 656 | struct task_struct *task) |
| 657 | { |
| 658 | struct rb_node *n = dev->user_clients.rb_node; |
| 659 | struct ion_client *client; |
| 660 | |
| 661 | mutex_lock(&dev->lock); |
| 662 | while (n) { |
| 663 | client = rb_entry(n, struct ion_client, node); |
| 664 | if (task == client->task) { |
| 665 | ion_client_get(client); |
| 666 | mutex_unlock(&dev->lock); |
| 667 | return client; |
| 668 | } else if (task < client->task) { |
| 669 | n = n->rb_left; |
| 670 | } else if (task > client->task) { |
| 671 | n = n->rb_right; |
| 672 | } |
| 673 | } |
| 674 | mutex_unlock(&dev->lock); |
| 675 | return NULL; |
| 676 | } |
| 677 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 678 | struct ion_client *ion_client_create(struct ion_device *dev, |
| 679 | unsigned int heap_mask, |
| 680 | const char *name) |
| 681 | { |
| 682 | struct ion_client *client; |
| 683 | struct task_struct *task; |
| 684 | struct rb_node **p; |
| 685 | struct rb_node *parent = NULL; |
| 686 | struct ion_client *entry; |
| 687 | char debug_name[64]; |
Rebecca Schultz Zavin | 83e3dab | 2011-07-01 20:41:25 -0700 | [diff] [blame] | 688 | pid_t pid; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 689 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 690 | get_task_struct(current->group_leader); |
| 691 | task_lock(current->group_leader); |
Rebecca Schultz Zavin | 83e3dab | 2011-07-01 20:41:25 -0700 | [diff] [blame] | 692 | pid = task_pid_nr(current->group_leader); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 693 | /* don't bother to store task struct for kernel threads, |
| 694 | they can't be killed anyway */ |
| 695 | if (current->group_leader->flags & PF_KTHREAD) { |
| 696 | put_task_struct(current->group_leader); |
| 697 | task = NULL; |
| 698 | } else { |
| 699 | task = current->group_leader; |
| 700 | } |
| 701 | task_unlock(current->group_leader); |
Rebecca Schultz Zavin | 83e3dab | 2011-07-01 20:41:25 -0700 | [diff] [blame] | 702 | |
| 703 | /* if this isn't a kernel thread, see if a client already |
| 704 | exists */ |
| 705 | if (task) { |
| 706 | client = ion_client_lookup(dev, task); |
| 707 | if (!IS_ERR_OR_NULL(client)) { |
| 708 | put_task_struct(current->group_leader); |
| 709 | return client; |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | client = kzalloc(sizeof(struct ion_client), GFP_KERNEL); |
| 714 | if (!client) { |
| 715 | put_task_struct(current->group_leader); |
| 716 | return ERR_PTR(-ENOMEM); |
| 717 | } |
| 718 | |
| 719 | client->dev = dev; |
| 720 | client->handles = RB_ROOT; |
| 721 | mutex_init(&client->lock); |
| 722 | client->name = name; |
| 723 | client->heap_mask = heap_mask; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 724 | client->task = task; |
Rebecca Schultz Zavin | 83e3dab | 2011-07-01 20:41:25 -0700 | [diff] [blame] | 725 | client->pid = pid; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 726 | kref_init(&client->ref); |
| 727 | |
| 728 | mutex_lock(&dev->lock); |
| 729 | if (task) { |
| 730 | p = &dev->user_clients.rb_node; |
| 731 | while (*p) { |
| 732 | parent = *p; |
| 733 | entry = rb_entry(parent, struct ion_client, node); |
| 734 | |
| 735 | if (task < entry->task) |
| 736 | p = &(*p)->rb_left; |
| 737 | else if (task > entry->task) |
| 738 | p = &(*p)->rb_right; |
| 739 | } |
| 740 | rb_link_node(&client->node, parent, p); |
| 741 | rb_insert_color(&client->node, &dev->user_clients); |
| 742 | } else { |
| 743 | p = &dev->kernel_clients.rb_node; |
| 744 | while (*p) { |
| 745 | parent = *p; |
| 746 | entry = rb_entry(parent, struct ion_client, node); |
| 747 | |
| 748 | if (client < entry) |
| 749 | p = &(*p)->rb_left; |
| 750 | else if (client > entry) |
| 751 | p = &(*p)->rb_right; |
| 752 | } |
| 753 | rb_link_node(&client->node, parent, p); |
| 754 | rb_insert_color(&client->node, &dev->kernel_clients); |
| 755 | } |
| 756 | |
| 757 | snprintf(debug_name, 64, "%u", client->pid); |
| 758 | client->debug_root = debugfs_create_file(debug_name, 0664, |
| 759 | dev->debug_root, client, |
| 760 | &debug_client_fops); |
| 761 | mutex_unlock(&dev->lock); |
| 762 | |
| 763 | return client; |
| 764 | } |
| 765 | |
Rebecca Schultz Zavin | 0b7e8ae | 2011-07-06 18:07:01 -0700 | [diff] [blame] | 766 | static void _ion_client_destroy(struct kref *kref) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 767 | { |
Rebecca Schultz Zavin | 0b7e8ae | 2011-07-06 18:07:01 -0700 | [diff] [blame] | 768 | struct ion_client *client = container_of(kref, struct ion_client, ref); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 769 | struct ion_device *dev = client->dev; |
| 770 | struct rb_node *n; |
| 771 | |
| 772 | pr_debug("%s: %d\n", __func__, __LINE__); |
| 773 | while ((n = rb_first(&client->handles))) { |
| 774 | struct ion_handle *handle = rb_entry(n, struct ion_handle, |
| 775 | node); |
| 776 | ion_handle_destroy(&handle->ref); |
| 777 | } |
| 778 | mutex_lock(&dev->lock); |
| 779 | if (client->task) { |
| 780 | rb_erase(&client->node, &dev->user_clients); |
| 781 | put_task_struct(client->task); |
| 782 | } else { |
| 783 | rb_erase(&client->node, &dev->kernel_clients); |
| 784 | } |
| 785 | debugfs_remove_recursive(client->debug_root); |
| 786 | mutex_unlock(&dev->lock); |
| 787 | |
| 788 | kfree(client); |
| 789 | } |
| 790 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 791 | static void ion_client_get(struct ion_client *client) |
| 792 | { |
| 793 | kref_get(&client->ref); |
| 794 | } |
| 795 | |
| 796 | static int ion_client_put(struct ion_client *client) |
| 797 | { |
| 798 | return kref_put(&client->ref, _ion_client_destroy); |
| 799 | } |
| 800 | |
Rebecca Schultz Zavin | 0b7e8ae | 2011-07-06 18:07:01 -0700 | [diff] [blame] | 801 | void ion_client_destroy(struct ion_client *client) |
| 802 | { |
| 803 | ion_client_put(client); |
| 804 | } |
| 805 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 806 | static int ion_share_release(struct inode *inode, struct file* file) |
| 807 | { |
| 808 | struct ion_buffer *buffer = file->private_data; |
| 809 | |
| 810 | pr_debug("%s: %d\n", __func__, __LINE__); |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 811 | mutex_lock(&buffer->lock); |
| 812 | buffer->umap_cnt--; |
| 813 | mutex_unlock(&buffer->lock); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 814 | /* drop the reference to the buffer -- this prevents the |
| 815 | buffer from going away because the client holding it exited |
| 816 | while it was being passed */ |
| 817 | ion_buffer_put(buffer); |
| 818 | return 0; |
| 819 | } |
| 820 | |
| 821 | static void ion_vma_open(struct vm_area_struct *vma) |
| 822 | { |
| 823 | |
| 824 | struct ion_buffer *buffer = vma->vm_file->private_data; |
| 825 | struct ion_handle *handle = vma->vm_private_data; |
| 826 | struct ion_client *client; |
| 827 | |
| 828 | pr_debug("%s: %d\n", __func__, __LINE__); |
| 829 | /* check that the client still exists and take a reference so |
| 830 | it can't go away until this vma is closed */ |
| 831 | client = ion_client_lookup(buffer->dev, current->group_leader); |
| 832 | if (IS_ERR_OR_NULL(client)) { |
| 833 | vma->vm_private_data = NULL; |
| 834 | return; |
| 835 | } |
| 836 | pr_debug("%s: %d client_cnt %d handle_cnt %d alloc_cnt %d\n", |
| 837 | __func__, __LINE__, |
| 838 | atomic_read(&client->ref.refcount), |
| 839 | atomic_read(&handle->ref.refcount), |
| 840 | atomic_read(&buffer->ref.refcount)); |
| 841 | } |
| 842 | |
| 843 | static void ion_vma_close(struct vm_area_struct *vma) |
| 844 | { |
| 845 | struct ion_handle *handle = vma->vm_private_data; |
| 846 | struct ion_buffer *buffer = vma->vm_file->private_data; |
| 847 | struct ion_client *client; |
| 848 | |
| 849 | pr_debug("%s: %d\n", __func__, __LINE__); |
| 850 | /* this indicates the client is gone, nothing to do here */ |
| 851 | if (!handle) |
| 852 | return; |
| 853 | client = handle->client; |
| 854 | pr_debug("%s: %d client_cnt %d handle_cnt %d alloc_cnt %d\n", |
| 855 | __func__, __LINE__, |
| 856 | atomic_read(&client->ref.refcount), |
| 857 | atomic_read(&handle->ref.refcount), |
| 858 | atomic_read(&buffer->ref.refcount)); |
| 859 | ion_handle_put(handle); |
| 860 | ion_client_put(client); |
| 861 | pr_debug("%s: %d client_cnt %d handle_cnt %d alloc_cnt %d\n", |
| 862 | __func__, __LINE__, |
| 863 | atomic_read(&client->ref.refcount), |
| 864 | atomic_read(&handle->ref.refcount), |
| 865 | atomic_read(&buffer->ref.refcount)); |
| 866 | } |
| 867 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 868 | static struct vm_operations_struct ion_vm_ops = { |
| 869 | .open = ion_vma_open, |
| 870 | .close = ion_vma_close, |
| 871 | }; |
| 872 | |
| 873 | static int ion_share_mmap(struct file *file, struct vm_area_struct *vma) |
| 874 | { |
| 875 | struct ion_buffer *buffer = file->private_data; |
| 876 | unsigned long size = vma->vm_end - vma->vm_start; |
| 877 | struct ion_client *client; |
| 878 | struct ion_handle *handle; |
| 879 | int ret; |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 880 | unsigned long flags = file->f_flags & O_DSYNC ? |
| 881 | ION_SET_CACHE(UNCACHED) : |
| 882 | ION_SET_CACHE(CACHED); |
| 883 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 884 | |
| 885 | pr_debug("%s: %d\n", __func__, __LINE__); |
| 886 | /* make sure the client still exists, it's possible for the client to |
| 887 | have gone away but the map/share fd still to be around, take |
| 888 | a reference to it so it can't go away while this mapping exists */ |
| 889 | client = ion_client_lookup(buffer->dev, current->group_leader); |
| 890 | if (IS_ERR_OR_NULL(client)) { |
| 891 | pr_err("%s: trying to mmap an ion handle in a process with no " |
| 892 | "ion client\n", __func__); |
| 893 | return -EINVAL; |
| 894 | } |
| 895 | |
| 896 | if ((size > buffer->size) || (size + (vma->vm_pgoff << PAGE_SHIFT) > |
| 897 | buffer->size)) { |
| 898 | pr_err("%s: trying to map larger area than handle has available" |
| 899 | "\n", __func__); |
| 900 | ret = -EINVAL; |
| 901 | goto err; |
| 902 | } |
| 903 | |
| 904 | /* find the handle and take a reference to it */ |
| 905 | handle = ion_import(client, buffer); |
| 906 | if (IS_ERR_OR_NULL(handle)) { |
| 907 | ret = -EINVAL; |
| 908 | goto err; |
| 909 | } |
| 910 | |
| 911 | if (!handle->buffer->heap->ops->map_user) { |
| 912 | pr_err("%s: this heap does not define a method for mapping " |
| 913 | "to userspace\n", __func__); |
| 914 | ret = -EINVAL; |
| 915 | goto err1; |
| 916 | } |
| 917 | |
| 918 | mutex_lock(&buffer->lock); |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 919 | if (buffer->kmap_cnt || buffer->dmap_cnt || buffer->umap_cnt) { |
| 920 | if (buffer->flags != flags) { |
| 921 | pr_err("%s: buffer was already mapped with flags %lx," |
| 922 | " cannot map with flags %lx\n", __func__, |
| 923 | buffer->flags, flags); |
| 924 | ret = -EEXIST; |
| 925 | mutex_unlock(&buffer->lock); |
| 926 | goto err1; |
| 927 | } |
| 928 | |
| 929 | } else { |
| 930 | buffer->flags = flags; |
| 931 | } |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 932 | /* now map it to userspace */ |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 933 | ret = buffer->heap->ops->map_user(buffer->heap, buffer, vma, |
| 934 | flags); |
| 935 | buffer->umap_cnt++; |
Rebecca Schultz Zavin | e6ee124 | 2011-06-30 12:19:55 -0700 | [diff] [blame] | 936 | mutex_unlock(&buffer->lock); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 937 | if (ret) { |
| 938 | pr_err("%s: failure mapping buffer to userspace\n", |
| 939 | __func__); |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 940 | goto err2; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 941 | } |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 942 | |
| 943 | vma->vm_ops = &ion_vm_ops; |
| 944 | /* move the handle into the vm_private_data so we can access it from |
| 945 | vma_open/close */ |
| 946 | vma->vm_private_data = handle; |
| 947 | pr_debug("%s: %d client_cnt %d handle_cnt %d alloc_cnt %d\n", |
| 948 | __func__, __LINE__, |
| 949 | atomic_read(&client->ref.refcount), |
| 950 | atomic_read(&handle->ref.refcount), |
| 951 | atomic_read(&buffer->ref.refcount)); |
| 952 | return 0; |
| 953 | |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 954 | err2: |
| 955 | buffer->umap_cnt--; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 956 | /* drop the reference to the handle */ |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 957 | err1: |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 958 | ion_handle_put(handle); |
| 959 | err: |
Iliyan Malchev | 3fe2436 | 2011-08-09 14:42:08 -0700 | [diff] [blame] | 960 | /* drop the reference to the client */ |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 961 | ion_client_put(client); |
| 962 | return ret; |
| 963 | } |
| 964 | |
| 965 | static const struct file_operations ion_share_fops = { |
| 966 | .owner = THIS_MODULE, |
| 967 | .release = ion_share_release, |
| 968 | .mmap = ion_share_mmap, |
| 969 | }; |
| 970 | |
| 971 | static int ion_ioctl_share(struct file *parent, struct ion_client *client, |
| 972 | struct ion_handle *handle) |
| 973 | { |
| 974 | int fd = get_unused_fd(); |
| 975 | struct file *file; |
| 976 | |
| 977 | if (fd < 0) |
| 978 | return -ENFILE; |
| 979 | |
| 980 | file = anon_inode_getfile("ion_share_fd", &ion_share_fops, |
| 981 | handle->buffer, O_RDWR); |
| 982 | if (IS_ERR_OR_NULL(file)) |
| 983 | goto err; |
Laura Abbott | 4b5d048 | 2011-09-27 18:35:14 -0700 | [diff] [blame^] | 984 | |
| 985 | if (parent->f_flags & O_DSYNC) |
| 986 | file->f_flags |= O_DSYNC; |
| 987 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 988 | ion_buffer_get(handle->buffer); |
| 989 | fd_install(fd, file); |
| 990 | |
| 991 | return fd; |
| 992 | |
| 993 | err: |
| 994 | put_unused_fd(fd); |
| 995 | return -ENFILE; |
| 996 | } |
| 997 | |
| 998 | static long ion_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
| 999 | { |
| 1000 | struct ion_client *client = filp->private_data; |
| 1001 | |
| 1002 | switch (cmd) { |
| 1003 | case ION_IOC_ALLOC: |
| 1004 | { |
| 1005 | struct ion_allocation_data data; |
| 1006 | |
| 1007 | if (copy_from_user(&data, (void __user *)arg, sizeof(data))) |
| 1008 | return -EFAULT; |
| 1009 | data.handle = ion_alloc(client, data.len, data.align, |
| 1010 | data.flags); |
| 1011 | if (copy_to_user((void __user *)arg, &data, sizeof(data))) |
| 1012 | return -EFAULT; |
| 1013 | break; |
| 1014 | } |
| 1015 | case ION_IOC_FREE: |
| 1016 | { |
| 1017 | struct ion_handle_data data; |
Rebecca Schultz Zavin | e6ee124 | 2011-06-30 12:19:55 -0700 | [diff] [blame] | 1018 | bool valid; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1019 | |
| 1020 | if (copy_from_user(&data, (void __user *)arg, |
| 1021 | sizeof(struct ion_handle_data))) |
| 1022 | return -EFAULT; |
| 1023 | mutex_lock(&client->lock); |
Rebecca Schultz Zavin | e6ee124 | 2011-06-30 12:19:55 -0700 | [diff] [blame] | 1024 | valid = ion_handle_validate(client, data.handle); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1025 | mutex_unlock(&client->lock); |
Rebecca Schultz Zavin | e6ee124 | 2011-06-30 12:19:55 -0700 | [diff] [blame] | 1026 | if (!valid) |
| 1027 | return -EINVAL; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1028 | ion_free(client, data.handle); |
| 1029 | break; |
| 1030 | } |
| 1031 | case ION_IOC_MAP: |
| 1032 | case ION_IOC_SHARE: |
| 1033 | { |
| 1034 | struct ion_fd_data data; |
| 1035 | |
| 1036 | if (copy_from_user(&data, (void __user *)arg, sizeof(data))) |
| 1037 | return -EFAULT; |
| 1038 | mutex_lock(&client->lock); |
| 1039 | if (!ion_handle_validate(client, data.handle)) { |
| 1040 | pr_err("%s: invalid handle passed to share ioctl.\n", |
| 1041 | __func__); |
Rebecca Schultz Zavin | e6ee124 | 2011-06-30 12:19:55 -0700 | [diff] [blame] | 1042 | mutex_unlock(&client->lock); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1043 | return -EINVAL; |
| 1044 | } |
| 1045 | data.fd = ion_ioctl_share(filp, client, data.handle); |
| 1046 | mutex_unlock(&client->lock); |
| 1047 | if (copy_to_user((void __user *)arg, &data, sizeof(data))) |
| 1048 | return -EFAULT; |
| 1049 | break; |
| 1050 | } |
| 1051 | case ION_IOC_IMPORT: |
| 1052 | { |
| 1053 | struct ion_fd_data data; |
| 1054 | if (copy_from_user(&data, (void __user *)arg, |
| 1055 | sizeof(struct ion_fd_data))) |
| 1056 | return -EFAULT; |
| 1057 | |
| 1058 | data.handle = ion_import_fd(client, data.fd); |
| 1059 | if (IS_ERR(data.handle)) |
| 1060 | data.handle = NULL; |
| 1061 | if (copy_to_user((void __user *)arg, &data, |
| 1062 | sizeof(struct ion_fd_data))) |
| 1063 | return -EFAULT; |
| 1064 | break; |
| 1065 | } |
| 1066 | case ION_IOC_CUSTOM: |
| 1067 | { |
| 1068 | struct ion_device *dev = client->dev; |
| 1069 | struct ion_custom_data data; |
| 1070 | |
| 1071 | if (!dev->custom_ioctl) |
| 1072 | return -ENOTTY; |
| 1073 | if (copy_from_user(&data, (void __user *)arg, |
| 1074 | sizeof(struct ion_custom_data))) |
| 1075 | return -EFAULT; |
| 1076 | return dev->custom_ioctl(client, data.cmd, data.arg); |
| 1077 | } |
| 1078 | default: |
| 1079 | return -ENOTTY; |
| 1080 | } |
| 1081 | return 0; |
| 1082 | } |
| 1083 | |
| 1084 | static int ion_release(struct inode *inode, struct file *file) |
| 1085 | { |
| 1086 | struct ion_client *client = file->private_data; |
| 1087 | |
| 1088 | pr_debug("%s: %d\n", __func__, __LINE__); |
| 1089 | ion_client_put(client); |
| 1090 | return 0; |
| 1091 | } |
| 1092 | |
| 1093 | static int ion_open(struct inode *inode, struct file *file) |
| 1094 | { |
| 1095 | struct miscdevice *miscdev = file->private_data; |
| 1096 | struct ion_device *dev = container_of(miscdev, struct ion_device, dev); |
| 1097 | struct ion_client *client; |
| 1098 | |
| 1099 | pr_debug("%s: %d\n", __func__, __LINE__); |
Rebecca Schultz Zavin | 6d3b958 | 2011-07-06 18:07:24 -0700 | [diff] [blame] | 1100 | client = ion_client_create(dev, -1, "user"); |
| 1101 | if (IS_ERR_OR_NULL(client)) |
| 1102 | return PTR_ERR(client); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1103 | file->private_data = client; |
| 1104 | |
| 1105 | return 0; |
| 1106 | } |
| 1107 | |
| 1108 | static const struct file_operations ion_fops = { |
| 1109 | .owner = THIS_MODULE, |
| 1110 | .open = ion_open, |
| 1111 | .release = ion_release, |
| 1112 | .unlocked_ioctl = ion_ioctl, |
| 1113 | }; |
| 1114 | |
| 1115 | static size_t ion_debug_heap_total(struct ion_client *client, |
| 1116 | enum ion_heap_type type) |
| 1117 | { |
| 1118 | size_t size = 0; |
| 1119 | struct rb_node *n; |
| 1120 | |
| 1121 | mutex_lock(&client->lock); |
| 1122 | for (n = rb_first(&client->handles); n; n = rb_next(n)) { |
| 1123 | struct ion_handle *handle = rb_entry(n, |
| 1124 | struct ion_handle, |
| 1125 | node); |
| 1126 | if (handle->buffer->heap->type == type) |
| 1127 | size += handle->buffer->size; |
| 1128 | } |
| 1129 | mutex_unlock(&client->lock); |
| 1130 | return size; |
| 1131 | } |
| 1132 | |
| 1133 | static int ion_debug_heap_show(struct seq_file *s, void *unused) |
| 1134 | { |
| 1135 | struct ion_heap *heap = s->private; |
| 1136 | struct ion_device *dev = heap->dev; |
| 1137 | struct rb_node *n; |
| 1138 | |
| 1139 | seq_printf(s, "%16.s %16.s %16.s\n", "client", "pid", "size"); |
| 1140 | for (n = rb_first(&dev->user_clients); n; n = rb_next(n)) { |
| 1141 | struct ion_client *client = rb_entry(n, struct ion_client, |
| 1142 | node); |
| 1143 | char task_comm[TASK_COMM_LEN]; |
| 1144 | size_t size = ion_debug_heap_total(client, heap->type); |
| 1145 | if (!size) |
| 1146 | continue; |
| 1147 | |
| 1148 | get_task_comm(task_comm, client->task); |
| 1149 | seq_printf(s, "%16.s %16u %16u\n", task_comm, client->pid, |
| 1150 | size); |
| 1151 | } |
| 1152 | |
| 1153 | for (n = rb_first(&dev->kernel_clients); n; n = rb_next(n)) { |
| 1154 | struct ion_client *client = rb_entry(n, struct ion_client, |
| 1155 | node); |
| 1156 | size_t size = ion_debug_heap_total(client, heap->type); |
| 1157 | if (!size) |
| 1158 | continue; |
| 1159 | seq_printf(s, "%16.s %16u %16u\n", client->name, client->pid, |
| 1160 | size); |
| 1161 | } |
| 1162 | return 0; |
| 1163 | } |
| 1164 | |
| 1165 | static int ion_debug_heap_open(struct inode *inode, struct file *file) |
| 1166 | { |
| 1167 | return single_open(file, ion_debug_heap_show, inode->i_private); |
| 1168 | } |
| 1169 | |
| 1170 | static const struct file_operations debug_heap_fops = { |
| 1171 | .open = ion_debug_heap_open, |
| 1172 | .read = seq_read, |
| 1173 | .llseek = seq_lseek, |
| 1174 | .release = single_release, |
| 1175 | }; |
| 1176 | |
| 1177 | void ion_device_add_heap(struct ion_device *dev, struct ion_heap *heap) |
| 1178 | { |
| 1179 | struct rb_node **p = &dev->heaps.rb_node; |
| 1180 | struct rb_node *parent = NULL; |
| 1181 | struct ion_heap *entry; |
| 1182 | |
| 1183 | heap->dev = dev; |
| 1184 | mutex_lock(&dev->lock); |
| 1185 | while (*p) { |
| 1186 | parent = *p; |
| 1187 | entry = rb_entry(parent, struct ion_heap, node); |
| 1188 | |
Rebecca Schultz Zavin | e6ee124 | 2011-06-30 12:19:55 -0700 | [diff] [blame] | 1189 | if (heap->id < entry->id) { |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1190 | p = &(*p)->rb_left; |
Rebecca Schultz Zavin | e6ee124 | 2011-06-30 12:19:55 -0700 | [diff] [blame] | 1191 | } else if (heap->id > entry->id ) { |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1192 | p = &(*p)->rb_right; |
| 1193 | } else { |
| 1194 | pr_err("%s: can not insert multiple heaps with " |
Rebecca Schultz Zavin | e6ee124 | 2011-06-30 12:19:55 -0700 | [diff] [blame] | 1195 | "id %d\n", __func__, heap->id); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1196 | goto end; |
| 1197 | } |
| 1198 | } |
| 1199 | |
| 1200 | rb_link_node(&heap->node, parent, p); |
| 1201 | rb_insert_color(&heap->node, &dev->heaps); |
| 1202 | debugfs_create_file(heap->name, 0664, dev->debug_root, heap, |
| 1203 | &debug_heap_fops); |
| 1204 | end: |
| 1205 | mutex_unlock(&dev->lock); |
| 1206 | } |
| 1207 | |
| 1208 | struct ion_device *ion_device_create(long (*custom_ioctl) |
| 1209 | (struct ion_client *client, |
| 1210 | unsigned int cmd, |
| 1211 | unsigned long arg)) |
| 1212 | { |
| 1213 | struct ion_device *idev; |
| 1214 | int ret; |
| 1215 | |
| 1216 | idev = kzalloc(sizeof(struct ion_device), GFP_KERNEL); |
| 1217 | if (!idev) |
| 1218 | return ERR_PTR(-ENOMEM); |
| 1219 | |
| 1220 | idev->dev.minor = MISC_DYNAMIC_MINOR; |
| 1221 | idev->dev.name = "ion"; |
| 1222 | idev->dev.fops = &ion_fops; |
| 1223 | idev->dev.parent = NULL; |
| 1224 | ret = misc_register(&idev->dev); |
| 1225 | if (ret) { |
| 1226 | pr_err("ion: failed to register misc device.\n"); |
| 1227 | return ERR_PTR(ret); |
| 1228 | } |
| 1229 | |
| 1230 | idev->debug_root = debugfs_create_dir("ion", NULL); |
| 1231 | if (IS_ERR_OR_NULL(idev->debug_root)) |
| 1232 | pr_err("ion: failed to create debug files.\n"); |
| 1233 | |
| 1234 | idev->custom_ioctl = custom_ioctl; |
| 1235 | idev->buffers = RB_ROOT; |
| 1236 | mutex_init(&idev->lock); |
| 1237 | idev->heaps = RB_ROOT; |
| 1238 | idev->user_clients = RB_ROOT; |
| 1239 | idev->kernel_clients = RB_ROOT; |
| 1240 | return idev; |
| 1241 | } |
| 1242 | |
| 1243 | void ion_device_destroy(struct ion_device *dev) |
| 1244 | { |
| 1245 | misc_deregister(&dev->dev); |
| 1246 | /* XXX need to free the heaps and clients ? */ |
| 1247 | kfree(dev); |
| 1248 | } |