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