blob: 1a63ebff5eee9368a4c9cbbf987eee73186f47bc [file] [log] [blame]
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001/*
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 */
44struct 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 */
71struct 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 Zavine6ee1242011-06-30 12:19:55 -070085 * ion_handle - a client local reference to a buffer
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070086 * @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 Zavine6ee1242011-06-30 12:19:55 -070090 * @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 Zavinc80005a2011-06-29 19:44:29 -070093 *
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 */
97struct 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 */
108static 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 Zavinf9fb95e2011-06-30 18:09:05 -0700119 if (buffer < entry) {
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700120 p = &(*p)->rb_left;
Rebecca Schultz Zavinf9fb95e2011-06-30 18:09:05 -0700121 } else if (buffer > entry) {
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700122 p = &(*p)->rb_right;
Rebecca Schultz Zavinf9fb95e2011-06-30 18:09:05 -0700123 } else {
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700124 pr_err("%s: buffer already found.", __func__);
125 BUG();
Rebecca Schultz Zavinf9fb95e2011-06-30 18:09:05 -0700126 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700127 }
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 */
134struct ion_buffer *ion_buffer_create(struct ion_heap *heap,
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 Zavine6ee1242011-06-30 12:19:55 -0700151 if (ret) {
152 kfree(buffer);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700153 return ERR_PTR(ret);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700154 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700155 buffer->dev = dev;
156 buffer->size = len;
157 mutex_init(&buffer->lock);
158 ion_buffer_add(dev, buffer);
159 return buffer;
160}
161
162static 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
174static void ion_buffer_get(struct ion_buffer *buffer)
175{
176 kref_get(&buffer->ref);
177}
178
179static int ion_buffer_put(struct ion_buffer *buffer)
180{
181 return kref_put(&buffer->ref, ion_buffer_destroy);
182}
183
184struct ion_handle *ion_handle_create(struct ion_client *client,
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);
193 handle->client = client;
194 ion_buffer_get(buffer);
195 handle->buffer = buffer;
196
197 return handle;
198}
199
200static void ion_handle_destroy(struct kref *kref)
201{
202 struct ion_handle *handle = container_of(kref, struct ion_handle, ref);
203 /* XXX Can a handle be destroyed while it's map count is non-zero?:
204 if (handle->map_cnt) unmap
205 */
206 ion_buffer_put(handle->buffer);
207 mutex_lock(&handle->client->lock);
208 rb_erase(&handle->node, &handle->client->handles);
209 mutex_unlock(&handle->client->lock);
210 kfree(handle);
211}
212
213struct ion_buffer *ion_handle_buffer(struct ion_handle *handle)
214{
215 return handle->buffer;
216}
217
218static void ion_handle_get(struct ion_handle *handle)
219{
220 kref_get(&handle->ref);
221}
222
223static int ion_handle_put(struct ion_handle *handle)
224{
225 return kref_put(&handle->ref, ion_handle_destroy);
226}
227
228static struct ion_handle *ion_handle_lookup(struct ion_client *client,
229 struct ion_buffer *buffer)
230{
231 struct rb_node *n;
232
233 for (n = rb_first(&client->handles); n; n = rb_next(n)) {
234 struct ion_handle *handle = rb_entry(n, struct ion_handle,
235 node);
236 if (handle->buffer == buffer)
237 return handle;
238 }
239 return NULL;
240}
241
242bool ion_handle_validate(struct ion_client *client, struct ion_handle *handle)
243{
244 struct rb_node *n = client->handles.rb_node;
245
246 while (n) {
247 struct ion_handle *handle_node = rb_entry(n, struct ion_handle,
248 node);
249 if (handle < handle_node)
250 n = n->rb_left;
251 else if (handle > handle_node)
252 n = n->rb_right;
253 else
254 return true;
255 }
256 return false;
257}
258
259static void ion_handle_add(struct ion_client *client, struct ion_handle *handle)
260{
261 struct rb_node **p = &client->handles.rb_node;
262 struct rb_node *parent = NULL;
263 struct ion_handle *entry;
264
265 while (*p) {
266 parent = *p;
267 entry = rb_entry(parent, struct ion_handle, node);
268
269 if (handle < entry)
270 p = &(*p)->rb_left;
271 else if (handle > entry)
272 p = &(*p)->rb_right;
273 else
274 WARN(1, "%s: buffer already found.", __func__);
275 }
276
277 rb_link_node(&handle->node, parent, p);
278 rb_insert_color(&handle->node, &client->handles);
279}
280
281struct ion_handle *ion_alloc(struct ion_client *client, size_t len,
282 size_t align, unsigned int flags)
283{
284 struct rb_node *n;
285 struct ion_handle *handle;
286 struct ion_device *dev = client->dev;
287 struct ion_buffer *buffer = NULL;
288
289 /*
290 * traverse the list of heaps available in this system in priority
291 * order. If the heap type is supported by the client, and matches the
292 * request of the caller allocate from it. Repeat until allocate has
293 * succeeded or all heaps have been tried
294 */
295 mutex_lock(&dev->lock);
296 for (n = rb_first(&dev->heaps); n != NULL; n = rb_next(n)) {
297 struct ion_heap *heap = rb_entry(n, struct ion_heap, node);
298 /* if the client doesn't support this heap type */
299 if (!((1 << heap->type) & client->heap_mask))
300 continue;
301 /* if the caller didn't specify this heap type */
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700302 if (!((1 << heap->id) & flags))
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700303 continue;
304 buffer = ion_buffer_create(heap, dev, len, align, flags);
305 if (!IS_ERR_OR_NULL(buffer))
306 break;
307 }
308 mutex_unlock(&dev->lock);
309
310 if (IS_ERR_OR_NULL(buffer))
311 return ERR_PTR(PTR_ERR(buffer));
312
313 handle = ion_handle_create(client, buffer);
314
315 if (IS_ERR_OR_NULL(handle))
316 goto end;
317
318 /*
319 * ion_buffer_create will create a buffer with a ref_cnt of 1,
320 * and ion_handle_create will take a second reference, drop one here
321 */
322 ion_buffer_put(buffer);
323
324 mutex_lock(&client->lock);
325 ion_handle_add(client, handle);
326 mutex_unlock(&client->lock);
327 return handle;
328
329end:
330 ion_buffer_put(buffer);
331 return handle;
332}
333
334void ion_free(struct ion_client *client, struct ion_handle *handle)
335{
336 ion_handle_put(handle);
337}
338
339static void ion_client_get(struct ion_client *client);
340static int ion_client_put(struct ion_client *client);
341
342bool _ion_map(int *buffer_cnt, int *handle_cnt)
343{
344 bool map;
345
346 BUG_ON(*handle_cnt != 0 && *buffer_cnt == 0);
347
348 if (*buffer_cnt)
349 map = false;
350 else
351 map = true;
352 if (*handle_cnt == 0)
353 (*buffer_cnt)++;
354 (*handle_cnt)++;
355 return map;
356}
357
358bool _ion_unmap(int *buffer_cnt, int *handle_cnt)
359{
360 BUG_ON(*handle_cnt == 0);
361 (*handle_cnt)--;
362 if (*handle_cnt != 0)
363 return false;
364 BUG_ON(*buffer_cnt == 0);
365 (*buffer_cnt)--;
366 if (*buffer_cnt == 0)
367 return true;
368 return false;
369}
370
371int ion_phys(struct ion_client *client, struct ion_handle *handle,
372 ion_phys_addr_t *addr, size_t *len)
373{
374 struct ion_buffer *buffer;
375 int ret;
376
377 mutex_lock(&client->lock);
378 if (!ion_handle_validate(client, handle)) {
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700379 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700380 return -EINVAL;
381 }
382
383 buffer = handle->buffer;
384
385 if (!buffer->heap->ops->phys) {
386 pr_err("%s: ion_phys is not implemented by this heap.\n",
387 __func__);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700388 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700389 return -ENODEV;
390 }
391 mutex_unlock(&client->lock);
392 ret = buffer->heap->ops->phys(buffer->heap, buffer, addr, len);
393 return ret;
394}
395
396void *ion_map_kernel(struct ion_client *client, struct ion_handle *handle)
397{
398 struct ion_buffer *buffer;
399 void *vaddr;
400
401 mutex_lock(&client->lock);
402 if (!ion_handle_validate(client, handle)) {
403 pr_err("%s: invalid handle passed to map_kernel.\n",
404 __func__);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700405 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700406 return ERR_PTR(-EINVAL);
407 }
408
409 buffer = handle->buffer;
410 mutex_lock(&buffer->lock);
411
412 if (!handle->buffer->heap->ops->map_kernel) {
413 pr_err("%s: map_kernel is not implemented by this heap.\n",
414 __func__);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700415 mutex_unlock(&buffer->lock);
416 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700417 return ERR_PTR(-ENODEV);
418 }
419
420 if (_ion_map(&buffer->kmap_cnt, &handle->kmap_cnt)) {
421 vaddr = buffer->heap->ops->map_kernel(buffer->heap, buffer);
422 if (IS_ERR_OR_NULL(vaddr))
423 _ion_unmap(&buffer->kmap_cnt, &handle->kmap_cnt);
424 buffer->vaddr = vaddr;
425 } else {
426 vaddr = buffer->vaddr;
427 }
428 mutex_unlock(&buffer->lock);
429 mutex_unlock(&client->lock);
430 return vaddr;
431}
432
433struct scatterlist *ion_map_dma(struct ion_client *client,
434 struct ion_handle *handle)
435{
436 struct ion_buffer *buffer;
437 struct scatterlist *sglist;
438
439 mutex_lock(&client->lock);
440 if (!ion_handle_validate(client, handle)) {
441 pr_err("%s: invalid handle passed to map_dma.\n",
442 __func__);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700443 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700444 return ERR_PTR(-EINVAL);
445 }
446 buffer = handle->buffer;
447 mutex_lock(&buffer->lock);
448
449 if (!handle->buffer->heap->ops->map_dma) {
450 pr_err("%s: map_kernel is not implemented by this heap.\n",
451 __func__);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700452 mutex_unlock(&buffer->lock);
453 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700454 return ERR_PTR(-ENODEV);
455 }
456 if (_ion_map(&buffer->dmap_cnt, &handle->dmap_cnt)) {
457 sglist = buffer->heap->ops->map_dma(buffer->heap, buffer);
458 if (IS_ERR_OR_NULL(sglist))
459 _ion_unmap(&buffer->dmap_cnt, &handle->dmap_cnt);
460 buffer->sglist = sglist;
461 } else {
462 sglist = buffer->sglist;
463 }
464 mutex_unlock(&buffer->lock);
465 mutex_unlock(&client->lock);
466 return sglist;
467}
468
469void ion_unmap_kernel(struct ion_client *client, struct ion_handle *handle)
470{
471 struct ion_buffer *buffer;
472
473 mutex_lock(&client->lock);
474 buffer = handle->buffer;
475 mutex_lock(&buffer->lock);
476 if (_ion_unmap(&buffer->kmap_cnt, &handle->kmap_cnt)) {
477 buffer->heap->ops->unmap_kernel(buffer->heap, buffer);
478 buffer->vaddr = NULL;
479 }
480 mutex_unlock(&buffer->lock);
481 mutex_unlock(&client->lock);
482}
483
484void ion_unmap_dma(struct ion_client *client, struct ion_handle *handle)
485{
486 struct ion_buffer *buffer;
487
488 mutex_lock(&client->lock);
489 buffer = handle->buffer;
490 mutex_lock(&buffer->lock);
491 if (_ion_unmap(&buffer->dmap_cnt, &handle->dmap_cnt)) {
492 buffer->heap->ops->unmap_dma(buffer->heap, buffer);
493 buffer->sglist = NULL;
494 }
495 mutex_unlock(&buffer->lock);
496 mutex_unlock(&client->lock);
497}
498
499
500struct ion_buffer *ion_share(struct ion_client *client,
501 struct ion_handle *handle)
502{
503 /* don't not take an extra refernce here, the burden is on the caller
504 * to make sure the buffer doesn't go away while it's passing it
505 * to another client -- ion_free should not be called on this handle
506 * until the buffer has been imported into the other client
507 */
508 return handle->buffer;
509}
510
511struct ion_handle *ion_import(struct ion_client *client,
512 struct ion_buffer *buffer)
513{
514 struct ion_handle *handle = NULL;
515
516 mutex_lock(&client->lock);
517 /* if a handle exists for this buffer just take a reference to it */
518 handle = ion_handle_lookup(client, buffer);
519 if (!IS_ERR_OR_NULL(handle)) {
520 ion_handle_get(handle);
521 goto end;
522 }
523 handle = ion_handle_create(client, buffer);
524 if (IS_ERR_OR_NULL(handle))
525 goto end;
526 ion_handle_add(client, handle);
527end:
528 mutex_unlock(&client->lock);
529 return handle;
530}
531
532static const struct file_operations ion_share_fops;
533
534struct ion_handle *ion_import_fd(struct ion_client *client, int fd)
535{
536 struct file *file = fget(fd);
537 struct ion_handle *handle;
538
539 if (!file) {
540 pr_err("%s: imported fd not found in file table.\n", __func__);
541 return ERR_PTR(-EINVAL);
542 }
543 if (file->f_op != &ion_share_fops) {
544 pr_err("%s: imported file is not a shared ion file.\n",
545 __func__);
546 handle = ERR_PTR(-EINVAL);
547 goto end;
548 }
549 handle = ion_import(client, file->private_data);
550end:
551 fput(file);
552 return handle;
553}
554
555static int ion_debug_client_show(struct seq_file *s, void *unused)
556{
557 struct ion_client *client = s->private;
558 struct rb_node *n;
559 size_t sizes[ION_NUM_HEAPS] = {0};
560 const char *names[ION_NUM_HEAPS] = {0};
561 int i;
562
563 mutex_lock(&client->lock);
564 for (n = rb_first(&client->handles); n; n = rb_next(n)) {
565 struct ion_handle *handle = rb_entry(n, struct ion_handle,
566 node);
567 enum ion_heap_type type = handle->buffer->heap->type;
568
569 if (!names[type])
570 names[type] = handle->buffer->heap->name;
571 sizes[type] += handle->buffer->size;
572 }
573 mutex_unlock(&client->lock);
574
575 seq_printf(s, "%16.16s: %16.16s\n", "heap_name", "size_in_bytes");
576 for (i = 0; i < ION_NUM_HEAPS; i++) {
577 if (!names[i])
578 continue;
579 seq_printf(s, "%16.16s: %16u %d\n", names[i], sizes[i],
580 atomic_read(&client->ref.refcount));
581 }
582 return 0;
583}
584
585static int ion_debug_client_open(struct inode *inode, struct file *file)
586{
587 return single_open(file, ion_debug_client_show, inode->i_private);
588}
589
590static const struct file_operations debug_client_fops = {
591 .open = ion_debug_client_open,
592 .read = seq_read,
593 .llseek = seq_lseek,
594 .release = single_release,
595};
596
Rebecca Schultz Zavin83e3dab2011-07-01 20:41:25 -0700597static struct ion_client *ion_client_lookup(struct ion_device *dev,
598 struct task_struct *task)
599{
600 struct rb_node *n = dev->user_clients.rb_node;
601 struct ion_client *client;
602
603 mutex_lock(&dev->lock);
604 while (n) {
605 client = rb_entry(n, struct ion_client, node);
606 if (task == client->task) {
607 ion_client_get(client);
608 mutex_unlock(&dev->lock);
609 return client;
610 } else if (task < client->task) {
611 n = n->rb_left;
612 } else if (task > client->task) {
613 n = n->rb_right;
614 }
615 }
616 mutex_unlock(&dev->lock);
617 return NULL;
618}
619
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700620struct ion_client *ion_client_create(struct ion_device *dev,
621 unsigned int heap_mask,
622 const char *name)
623{
624 struct ion_client *client;
625 struct task_struct *task;
626 struct rb_node **p;
627 struct rb_node *parent = NULL;
628 struct ion_client *entry;
629 char debug_name[64];
Rebecca Schultz Zavin83e3dab2011-07-01 20:41:25 -0700630 pid_t pid;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700631
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700632 get_task_struct(current->group_leader);
633 task_lock(current->group_leader);
Rebecca Schultz Zavin83e3dab2011-07-01 20:41:25 -0700634 pid = task_pid_nr(current->group_leader);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700635 /* don't bother to store task struct for kernel threads,
636 they can't be killed anyway */
637 if (current->group_leader->flags & PF_KTHREAD) {
638 put_task_struct(current->group_leader);
639 task = NULL;
640 } else {
641 task = current->group_leader;
642 }
643 task_unlock(current->group_leader);
Rebecca Schultz Zavin83e3dab2011-07-01 20:41:25 -0700644
645 /* if this isn't a kernel thread, see if a client already
646 exists */
647 if (task) {
648 client = ion_client_lookup(dev, task);
649 if (!IS_ERR_OR_NULL(client)) {
650 put_task_struct(current->group_leader);
651 return client;
652 }
653 }
654
655 client = kzalloc(sizeof(struct ion_client), GFP_KERNEL);
656 if (!client) {
657 put_task_struct(current->group_leader);
658 return ERR_PTR(-ENOMEM);
659 }
660
661 client->dev = dev;
662 client->handles = RB_ROOT;
663 mutex_init(&client->lock);
664 client->name = name;
665 client->heap_mask = heap_mask;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700666 client->task = task;
Rebecca Schultz Zavin83e3dab2011-07-01 20:41:25 -0700667 client->pid = pid;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700668 kref_init(&client->ref);
669
670 mutex_lock(&dev->lock);
671 if (task) {
672 p = &dev->user_clients.rb_node;
673 while (*p) {
674 parent = *p;
675 entry = rb_entry(parent, struct ion_client, node);
676
677 if (task < entry->task)
678 p = &(*p)->rb_left;
679 else if (task > entry->task)
680 p = &(*p)->rb_right;
681 }
682 rb_link_node(&client->node, parent, p);
683 rb_insert_color(&client->node, &dev->user_clients);
684 } else {
685 p = &dev->kernel_clients.rb_node;
686 while (*p) {
687 parent = *p;
688 entry = rb_entry(parent, struct ion_client, node);
689
690 if (client < entry)
691 p = &(*p)->rb_left;
692 else if (client > entry)
693 p = &(*p)->rb_right;
694 }
695 rb_link_node(&client->node, parent, p);
696 rb_insert_color(&client->node, &dev->kernel_clients);
697 }
698
699 snprintf(debug_name, 64, "%u", client->pid);
700 client->debug_root = debugfs_create_file(debug_name, 0664,
701 dev->debug_root, client,
702 &debug_client_fops);
703 mutex_unlock(&dev->lock);
704
705 return client;
706}
707
708void ion_client_destroy(struct ion_client *client)
709{
710 struct ion_device *dev = client->dev;
711 struct rb_node *n;
712
713 pr_debug("%s: %d\n", __func__, __LINE__);
714 while ((n = rb_first(&client->handles))) {
715 struct ion_handle *handle = rb_entry(n, struct ion_handle,
716 node);
717 ion_handle_destroy(&handle->ref);
718 }
719 mutex_lock(&dev->lock);
720 if (client->task) {
721 rb_erase(&client->node, &dev->user_clients);
722 put_task_struct(client->task);
723 } else {
724 rb_erase(&client->node, &dev->kernel_clients);
725 }
726 debugfs_remove_recursive(client->debug_root);
727 mutex_unlock(&dev->lock);
728
729 kfree(client);
730}
731
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700732static void _ion_client_destroy(struct kref *kref)
733{
734 struct ion_client *client = container_of(kref, struct ion_client, ref);
735 ion_client_destroy(client);
736}
737
738static void ion_client_get(struct ion_client *client)
739{
740 kref_get(&client->ref);
741}
742
743static int ion_client_put(struct ion_client *client)
744{
745 return kref_put(&client->ref, _ion_client_destroy);
746}
747
748static int ion_share_release(struct inode *inode, struct file* file)
749{
750 struct ion_buffer *buffer = file->private_data;
751
752 pr_debug("%s: %d\n", __func__, __LINE__);
753 /* drop the reference to the buffer -- this prevents the
754 buffer from going away because the client holding it exited
755 while it was being passed */
756 ion_buffer_put(buffer);
757 return 0;
758}
759
760static void ion_vma_open(struct vm_area_struct *vma)
761{
762
763 struct ion_buffer *buffer = vma->vm_file->private_data;
764 struct ion_handle *handle = vma->vm_private_data;
765 struct ion_client *client;
766
767 pr_debug("%s: %d\n", __func__, __LINE__);
768 /* check that the client still exists and take a reference so
769 it can't go away until this vma is closed */
770 client = ion_client_lookup(buffer->dev, current->group_leader);
771 if (IS_ERR_OR_NULL(client)) {
772 vma->vm_private_data = NULL;
773 return;
774 }
775 pr_debug("%s: %d client_cnt %d handle_cnt %d alloc_cnt %d\n",
776 __func__, __LINE__,
777 atomic_read(&client->ref.refcount),
778 atomic_read(&handle->ref.refcount),
779 atomic_read(&buffer->ref.refcount));
780}
781
782static void ion_vma_close(struct vm_area_struct *vma)
783{
784 struct ion_handle *handle = vma->vm_private_data;
785 struct ion_buffer *buffer = vma->vm_file->private_data;
786 struct ion_client *client;
787
788 pr_debug("%s: %d\n", __func__, __LINE__);
789 /* this indicates the client is gone, nothing to do here */
790 if (!handle)
791 return;
792 client = handle->client;
793 pr_debug("%s: %d client_cnt %d handle_cnt %d alloc_cnt %d\n",
794 __func__, __LINE__,
795 atomic_read(&client->ref.refcount),
796 atomic_read(&handle->ref.refcount),
797 atomic_read(&buffer->ref.refcount));
798 ion_handle_put(handle);
799 ion_client_put(client);
800 pr_debug("%s: %d client_cnt %d handle_cnt %d alloc_cnt %d\n",
801 __func__, __LINE__,
802 atomic_read(&client->ref.refcount),
803 atomic_read(&handle->ref.refcount),
804 atomic_read(&buffer->ref.refcount));
805}
806
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700807static struct vm_operations_struct ion_vm_ops = {
808 .open = ion_vma_open,
809 .close = ion_vma_close,
810};
811
812static int ion_share_mmap(struct file *file, struct vm_area_struct *vma)
813{
814 struct ion_buffer *buffer = file->private_data;
815 unsigned long size = vma->vm_end - vma->vm_start;
816 struct ion_client *client;
817 struct ion_handle *handle;
818 int ret;
819
820 pr_debug("%s: %d\n", __func__, __LINE__);
821 /* make sure the client still exists, it's possible for the client to
822 have gone away but the map/share fd still to be around, take
823 a reference to it so it can't go away while this mapping exists */
824 client = ion_client_lookup(buffer->dev, current->group_leader);
825 if (IS_ERR_OR_NULL(client)) {
826 pr_err("%s: trying to mmap an ion handle in a process with no "
827 "ion client\n", __func__);
828 return -EINVAL;
829 }
830
831 if ((size > buffer->size) || (size + (vma->vm_pgoff << PAGE_SHIFT) >
832 buffer->size)) {
833 pr_err("%s: trying to map larger area than handle has available"
834 "\n", __func__);
835 ret = -EINVAL;
836 goto err;
837 }
838
839 /* find the handle and take a reference to it */
840 handle = ion_import(client, buffer);
841 if (IS_ERR_OR_NULL(handle)) {
842 ret = -EINVAL;
843 goto err;
844 }
845
846 if (!handle->buffer->heap->ops->map_user) {
847 pr_err("%s: this heap does not define a method for mapping "
848 "to userspace\n", __func__);
849 ret = -EINVAL;
850 goto err1;
851 }
852
853 mutex_lock(&buffer->lock);
854 /* now map it to userspace */
855 ret = buffer->heap->ops->map_user(buffer->heap, buffer, vma);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700856 mutex_unlock(&buffer->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700857 if (ret) {
858 pr_err("%s: failure mapping buffer to userspace\n",
859 __func__);
860 goto err1;
861 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700862
863 vma->vm_ops = &ion_vm_ops;
864 /* move the handle into the vm_private_data so we can access it from
865 vma_open/close */
866 vma->vm_private_data = handle;
867 pr_debug("%s: %d client_cnt %d handle_cnt %d alloc_cnt %d\n",
868 __func__, __LINE__,
869 atomic_read(&client->ref.refcount),
870 atomic_read(&handle->ref.refcount),
871 atomic_read(&buffer->ref.refcount));
872 return 0;
873
874err1:
875 /* drop the reference to the handle */
876 ion_handle_put(handle);
877err:
878 /* drop the refernce to the client */
879 ion_client_put(client);
880 return ret;
881}
882
883static const struct file_operations ion_share_fops = {
884 .owner = THIS_MODULE,
885 .release = ion_share_release,
886 .mmap = ion_share_mmap,
887};
888
889static int ion_ioctl_share(struct file *parent, struct ion_client *client,
890 struct ion_handle *handle)
891{
892 int fd = get_unused_fd();
893 struct file *file;
894
895 if (fd < 0)
896 return -ENFILE;
897
898 file = anon_inode_getfile("ion_share_fd", &ion_share_fops,
899 handle->buffer, O_RDWR);
900 if (IS_ERR_OR_NULL(file))
901 goto err;
902 ion_buffer_get(handle->buffer);
903 fd_install(fd, file);
904
905 return fd;
906
907err:
908 put_unused_fd(fd);
909 return -ENFILE;
910}
911
912static long ion_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
913{
914 struct ion_client *client = filp->private_data;
915
916 switch (cmd) {
917 case ION_IOC_ALLOC:
918 {
919 struct ion_allocation_data data;
920
921 if (copy_from_user(&data, (void __user *)arg, sizeof(data)))
922 return -EFAULT;
923 data.handle = ion_alloc(client, data.len, data.align,
924 data.flags);
925 if (copy_to_user((void __user *)arg, &data, sizeof(data)))
926 return -EFAULT;
927 break;
928 }
929 case ION_IOC_FREE:
930 {
931 struct ion_handle_data data;
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700932 bool valid;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700933
934 if (copy_from_user(&data, (void __user *)arg,
935 sizeof(struct ion_handle_data)))
936 return -EFAULT;
937 mutex_lock(&client->lock);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700938 valid = ion_handle_validate(client, data.handle);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700939 mutex_unlock(&client->lock);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700940 if (!valid)
941 return -EINVAL;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700942 ion_free(client, data.handle);
943 break;
944 }
945 case ION_IOC_MAP:
946 case ION_IOC_SHARE:
947 {
948 struct ion_fd_data data;
949
950 if (copy_from_user(&data, (void __user *)arg, sizeof(data)))
951 return -EFAULT;
952 mutex_lock(&client->lock);
953 if (!ion_handle_validate(client, data.handle)) {
954 pr_err("%s: invalid handle passed to share ioctl.\n",
955 __func__);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700956 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700957 return -EINVAL;
958 }
959 data.fd = ion_ioctl_share(filp, client, data.handle);
960 mutex_unlock(&client->lock);
961 if (copy_to_user((void __user *)arg, &data, sizeof(data)))
962 return -EFAULT;
963 break;
964 }
965 case ION_IOC_IMPORT:
966 {
967 struct ion_fd_data data;
968 if (copy_from_user(&data, (void __user *)arg,
969 sizeof(struct ion_fd_data)))
970 return -EFAULT;
971
972 data.handle = ion_import_fd(client, data.fd);
973 if (IS_ERR(data.handle))
974 data.handle = NULL;
975 if (copy_to_user((void __user *)arg, &data,
976 sizeof(struct ion_fd_data)))
977 return -EFAULT;
978 break;
979 }
980 case ION_IOC_CUSTOM:
981 {
982 struct ion_device *dev = client->dev;
983 struct ion_custom_data data;
984
985 if (!dev->custom_ioctl)
986 return -ENOTTY;
987 if (copy_from_user(&data, (void __user *)arg,
988 sizeof(struct ion_custom_data)))
989 return -EFAULT;
990 return dev->custom_ioctl(client, data.cmd, data.arg);
991 }
992 default:
993 return -ENOTTY;
994 }
995 return 0;
996}
997
998static int ion_release(struct inode *inode, struct file *file)
999{
1000 struct ion_client *client = file->private_data;
1001
1002 pr_debug("%s: %d\n", __func__, __LINE__);
1003 ion_client_put(client);
1004 return 0;
1005}
1006
1007static int ion_open(struct inode *inode, struct file *file)
1008{
1009 struct miscdevice *miscdev = file->private_data;
1010 struct ion_device *dev = container_of(miscdev, struct ion_device, dev);
1011 struct ion_client *client;
1012
1013 pr_debug("%s: %d\n", __func__, __LINE__);
1014 client = ion_client_lookup(dev, current->group_leader);
1015 if (IS_ERR_OR_NULL(client)) {
1016 /* XXX: consider replacing "user" with cmdline */
1017 client = ion_client_create(dev, -1, "user");
1018 if (IS_ERR_OR_NULL(client))
1019 return PTR_ERR(client);
1020 }
1021 file->private_data = client;
1022
1023 return 0;
1024}
1025
1026static const struct file_operations ion_fops = {
1027 .owner = THIS_MODULE,
1028 .open = ion_open,
1029 .release = ion_release,
1030 .unlocked_ioctl = ion_ioctl,
1031};
1032
1033static size_t ion_debug_heap_total(struct ion_client *client,
1034 enum ion_heap_type type)
1035{
1036 size_t size = 0;
1037 struct rb_node *n;
1038
1039 mutex_lock(&client->lock);
1040 for (n = rb_first(&client->handles); n; n = rb_next(n)) {
1041 struct ion_handle *handle = rb_entry(n,
1042 struct ion_handle,
1043 node);
1044 if (handle->buffer->heap->type == type)
1045 size += handle->buffer->size;
1046 }
1047 mutex_unlock(&client->lock);
1048 return size;
1049}
1050
1051static int ion_debug_heap_show(struct seq_file *s, void *unused)
1052{
1053 struct ion_heap *heap = s->private;
1054 struct ion_device *dev = heap->dev;
1055 struct rb_node *n;
1056
1057 seq_printf(s, "%16.s %16.s %16.s\n", "client", "pid", "size");
1058 for (n = rb_first(&dev->user_clients); n; n = rb_next(n)) {
1059 struct ion_client *client = rb_entry(n, struct ion_client,
1060 node);
1061 char task_comm[TASK_COMM_LEN];
1062 size_t size = ion_debug_heap_total(client, heap->type);
1063 if (!size)
1064 continue;
1065
1066 get_task_comm(task_comm, client->task);
1067 seq_printf(s, "%16.s %16u %16u\n", task_comm, client->pid,
1068 size);
1069 }
1070
1071 for (n = rb_first(&dev->kernel_clients); n; n = rb_next(n)) {
1072 struct ion_client *client = rb_entry(n, struct ion_client,
1073 node);
1074 size_t size = ion_debug_heap_total(client, heap->type);
1075 if (!size)
1076 continue;
1077 seq_printf(s, "%16.s %16u %16u\n", client->name, client->pid,
1078 size);
1079 }
1080 return 0;
1081}
1082
1083static int ion_debug_heap_open(struct inode *inode, struct file *file)
1084{
1085 return single_open(file, ion_debug_heap_show, inode->i_private);
1086}
1087
1088static const struct file_operations debug_heap_fops = {
1089 .open = ion_debug_heap_open,
1090 .read = seq_read,
1091 .llseek = seq_lseek,
1092 .release = single_release,
1093};
1094
1095void ion_device_add_heap(struct ion_device *dev, struct ion_heap *heap)
1096{
1097 struct rb_node **p = &dev->heaps.rb_node;
1098 struct rb_node *parent = NULL;
1099 struct ion_heap *entry;
1100
1101 heap->dev = dev;
1102 mutex_lock(&dev->lock);
1103 while (*p) {
1104 parent = *p;
1105 entry = rb_entry(parent, struct ion_heap, node);
1106
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001107 if (heap->id < entry->id) {
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001108 p = &(*p)->rb_left;
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001109 } else if (heap->id > entry->id ) {
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001110 p = &(*p)->rb_right;
1111 } else {
1112 pr_err("%s: can not insert multiple heaps with "
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001113 "id %d\n", __func__, heap->id);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001114 goto end;
1115 }
1116 }
1117
1118 rb_link_node(&heap->node, parent, p);
1119 rb_insert_color(&heap->node, &dev->heaps);
1120 debugfs_create_file(heap->name, 0664, dev->debug_root, heap,
1121 &debug_heap_fops);
1122end:
1123 mutex_unlock(&dev->lock);
1124}
1125
1126struct ion_device *ion_device_create(long (*custom_ioctl)
1127 (struct ion_client *client,
1128 unsigned int cmd,
1129 unsigned long arg))
1130{
1131 struct ion_device *idev;
1132 int ret;
1133
1134 idev = kzalloc(sizeof(struct ion_device), GFP_KERNEL);
1135 if (!idev)
1136 return ERR_PTR(-ENOMEM);
1137
1138 idev->dev.minor = MISC_DYNAMIC_MINOR;
1139 idev->dev.name = "ion";
1140 idev->dev.fops = &ion_fops;
1141 idev->dev.parent = NULL;
1142 ret = misc_register(&idev->dev);
1143 if (ret) {
1144 pr_err("ion: failed to register misc device.\n");
1145 return ERR_PTR(ret);
1146 }
1147
1148 idev->debug_root = debugfs_create_dir("ion", NULL);
1149 if (IS_ERR_OR_NULL(idev->debug_root))
1150 pr_err("ion: failed to create debug files.\n");
1151
1152 idev->custom_ioctl = custom_ioctl;
1153 idev->buffers = RB_ROOT;
1154 mutex_init(&idev->lock);
1155 idev->heaps = RB_ROOT;
1156 idev->user_clients = RB_ROOT;
1157 idev->kernel_clients = RB_ROOT;
1158 return idev;
1159}
1160
1161void ion_device_destroy(struct ion_device *dev)
1162{
1163 misc_deregister(&dev->dev);
1164 /* XXX need to free the heaps and clients ? */
1165 kfree(dev);
1166}