blob: 2357b684c7893315333d86a83cfd574623eff5d2 [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 */
Iliyan Malchev3fe24362011-08-09 14:42:08 -0700134static struct ion_buffer *ion_buffer_create(struct ion_heap *heap,
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700135 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
Iliyan Malchev3fe24362011-08-09 14:42:08 -0700184static struct ion_handle *ion_handle_create(struct ion_client *client,
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700185 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 Malchev3fe24362011-08-09 14:42:08 -0700193 rb_init_node(&handle->node);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700194 handle->client = client;
195 ion_buffer_get(buffer);
196 handle->buffer = buffer;
197
198 return handle;
199}
200
201static 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 */
Laura Abbottd2a87372011-10-20 17:53:49 -0700207 WARN_ON(handle->kmap_cnt || handle->dmap_cnt || handle->usermap_cnt);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700208 ion_buffer_put(handle->buffer);
209 mutex_lock(&handle->client->lock);
Iliyan Malchev3fe24362011-08-09 14:42:08 -0700210 if (!RB_EMPTY_NODE(&handle->node))
211 rb_erase(&handle->node, &handle->client->handles);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700212 mutex_unlock(&handle->client->lock);
213 kfree(handle);
214}
215
216struct ion_buffer *ion_handle_buffer(struct ion_handle *handle)
217{
218 return handle->buffer;
219}
220
221static void ion_handle_get(struct ion_handle *handle)
222{
223 kref_get(&handle->ref);
224}
225
226static int ion_handle_put(struct ion_handle *handle)
227{
228 return kref_put(&handle->ref, ion_handle_destroy);
229}
230
231static struct ion_handle *ion_handle_lookup(struct ion_client *client,
232 struct ion_buffer *buffer)
233{
234 struct rb_node *n;
235
236 for (n = rb_first(&client->handles); n; n = rb_next(n)) {
237 struct ion_handle *handle = rb_entry(n, struct ion_handle,
238 node);
239 if (handle->buffer == buffer)
240 return handle;
241 }
242 return NULL;
243}
244
Iliyan Malchev3fe24362011-08-09 14:42:08 -0700245static bool ion_handle_validate(struct ion_client *client, struct ion_handle *handle)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700246{
247 struct rb_node *n = client->handles.rb_node;
248
249 while (n) {
250 struct ion_handle *handle_node = rb_entry(n, struct ion_handle,
251 node);
252 if (handle < handle_node)
253 n = n->rb_left;
254 else if (handle > handle_node)
255 n = n->rb_right;
256 else
257 return true;
258 }
259 return false;
260}
261
262static void ion_handle_add(struct ion_client *client, struct ion_handle *handle)
263{
264 struct rb_node **p = &client->handles.rb_node;
265 struct rb_node *parent = NULL;
266 struct ion_handle *entry;
267
268 while (*p) {
269 parent = *p;
270 entry = rb_entry(parent, struct ion_handle, node);
271
272 if (handle < entry)
273 p = &(*p)->rb_left;
274 else if (handle > entry)
275 p = &(*p)->rb_right;
276 else
277 WARN(1, "%s: buffer already found.", __func__);
278 }
279
280 rb_link_node(&handle->node, parent, p);
281 rb_insert_color(&handle->node, &client->handles);
282}
283
284struct ion_handle *ion_alloc(struct ion_client *client, size_t len,
285 size_t align, unsigned int flags)
286{
287 struct rb_node *n;
288 struct ion_handle *handle;
289 struct ion_device *dev = client->dev;
290 struct ion_buffer *buffer = NULL;
291
292 /*
293 * traverse the list of heaps available in this system in priority
294 * order. If the heap type is supported by the client, and matches the
295 * request of the caller allocate from it. Repeat until allocate has
296 * succeeded or all heaps have been tried
297 */
298 mutex_lock(&dev->lock);
299 for (n = rb_first(&dev->heaps); n != NULL; n = rb_next(n)) {
300 struct ion_heap *heap = rb_entry(n, struct ion_heap, node);
301 /* if the client doesn't support this heap type */
302 if (!((1 << heap->type) & client->heap_mask))
303 continue;
304 /* if the caller didn't specify this heap type */
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700305 if (!((1 << heap->id) & flags))
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700306 continue;
307 buffer = ion_buffer_create(heap, dev, len, align, flags);
308 if (!IS_ERR_OR_NULL(buffer))
309 break;
310 }
311 mutex_unlock(&dev->lock);
312
313 if (IS_ERR_OR_NULL(buffer))
314 return ERR_PTR(PTR_ERR(buffer));
315
316 handle = ion_handle_create(client, buffer);
317
318 if (IS_ERR_OR_NULL(handle))
319 goto end;
320
321 /*
322 * ion_buffer_create will create a buffer with a ref_cnt of 1,
323 * and ion_handle_create will take a second reference, drop one here
324 */
325 ion_buffer_put(buffer);
326
327 mutex_lock(&client->lock);
328 ion_handle_add(client, handle);
329 mutex_unlock(&client->lock);
330 return handle;
331
332end:
333 ion_buffer_put(buffer);
334 return handle;
335}
336
337void ion_free(struct ion_client *client, struct ion_handle *handle)
338{
Rebecca Schultz Zavinc72866d2011-07-07 17:07:56 -0700339 bool valid_handle;
340
341 BUG_ON(client != handle->client);
342
343 mutex_lock(&client->lock);
344 valid_handle = ion_handle_validate(client, handle);
345 mutex_unlock(&client->lock);
346
347 if (!valid_handle) {
348 WARN("%s: invalid handle passed to free.\n", __func__);
349 return;
350 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700351 ion_handle_put(handle);
352}
353
354static void ion_client_get(struct ion_client *client);
355static int ion_client_put(struct ion_client *client);
356
Iliyan Malchev3fe24362011-08-09 14:42:08 -0700357static bool _ion_map(int *buffer_cnt, int *handle_cnt)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700358{
359 bool map;
360
361 BUG_ON(*handle_cnt != 0 && *buffer_cnt == 0);
362
363 if (*buffer_cnt)
364 map = false;
365 else
366 map = true;
367 if (*handle_cnt == 0)
368 (*buffer_cnt)++;
369 (*handle_cnt)++;
370 return map;
371}
372
Iliyan Malchev3fe24362011-08-09 14:42:08 -0700373static bool _ion_unmap(int *buffer_cnt, int *handle_cnt)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700374{
375 BUG_ON(*handle_cnt == 0);
376 (*handle_cnt)--;
377 if (*handle_cnt != 0)
378 return false;
379 BUG_ON(*buffer_cnt == 0);
380 (*buffer_cnt)--;
381 if (*buffer_cnt == 0)
382 return true;
383 return false;
384}
385
386int ion_phys(struct ion_client *client, struct ion_handle *handle,
387 ion_phys_addr_t *addr, size_t *len)
388{
389 struct ion_buffer *buffer;
390 int ret;
391
392 mutex_lock(&client->lock);
393 if (!ion_handle_validate(client, handle)) {
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700394 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700395 return -EINVAL;
396 }
397
398 buffer = handle->buffer;
399
400 if (!buffer->heap->ops->phys) {
401 pr_err("%s: ion_phys is not implemented by this heap.\n",
402 __func__);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700403 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700404 return -ENODEV;
405 }
406 mutex_unlock(&client->lock);
407 ret = buffer->heap->ops->phys(buffer->heap, buffer, addr, len);
408 return ret;
409}
410
Laura Abbott894fd582011-08-19 13:33:56 -0700411void *ion_map_kernel(struct ion_client *client, struct ion_handle *handle,
412 unsigned long flags)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700413{
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__);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700421 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700422 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__);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700431 mutex_unlock(&buffer->lock);
432 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700433 return ERR_PTR(-ENODEV);
434 }
435
Laura Abbott894fd582011-08-19 13:33:56 -0700436 if (buffer->kmap_cnt || buffer->dmap_cnt || buffer->umap_cnt) {
437 if (buffer->flags != flags) {
438 pr_err("%s: buffer was already mapped with flags %lx,"
439 " cannot map with flags %lx\n", __func__,
440 buffer->flags, flags);
441 vaddr = ERR_PTR(-EEXIST);
442 goto out;
443 }
444
445 } else {
446 buffer->flags = flags;
447 }
448
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700449 if (_ion_map(&buffer->kmap_cnt, &handle->kmap_cnt)) {
Laura Abbott894fd582011-08-19 13:33:56 -0700450 vaddr = buffer->heap->ops->map_kernel(buffer->heap, buffer,
451 flags);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700452 if (IS_ERR_OR_NULL(vaddr))
453 _ion_unmap(&buffer->kmap_cnt, &handle->kmap_cnt);
454 buffer->vaddr = vaddr;
455 } else {
456 vaddr = buffer->vaddr;
457 }
Laura Abbott894fd582011-08-19 13:33:56 -0700458
459out:
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700460 mutex_unlock(&buffer->lock);
461 mutex_unlock(&client->lock);
462 return vaddr;
463}
464
465struct scatterlist *ion_map_dma(struct ion_client *client,
Laura Abbott894fd582011-08-19 13:33:56 -0700466 struct ion_handle *handle,
467 unsigned long flags)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700468{
469 struct ion_buffer *buffer;
470 struct scatterlist *sglist;
471
472 mutex_lock(&client->lock);
473 if (!ion_handle_validate(client, handle)) {
474 pr_err("%s: invalid handle passed to map_dma.\n",
475 __func__);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700476 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700477 return ERR_PTR(-EINVAL);
478 }
479 buffer = handle->buffer;
480 mutex_lock(&buffer->lock);
481
482 if (!handle->buffer->heap->ops->map_dma) {
483 pr_err("%s: map_kernel is not implemented by this heap.\n",
484 __func__);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700485 mutex_unlock(&buffer->lock);
486 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700487 return ERR_PTR(-ENODEV);
488 }
Laura Abbott894fd582011-08-19 13:33:56 -0700489
490 if (buffer->kmap_cnt || buffer->dmap_cnt || buffer->umap_cnt) {
491 if (buffer->flags != flags) {
492 pr_err("%s: buffer was already mapped with flags %lx,"
493 " cannot map with flags %lx\n", __func__,
494 buffer->flags, flags);
495 sglist = ERR_PTR(-EEXIST);
496 goto out;
497 }
498
499 } else {
500 buffer->flags = flags;
501 }
502
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700503 if (_ion_map(&buffer->dmap_cnt, &handle->dmap_cnt)) {
504 sglist = buffer->heap->ops->map_dma(buffer->heap, buffer);
505 if (IS_ERR_OR_NULL(sglist))
506 _ion_unmap(&buffer->dmap_cnt, &handle->dmap_cnt);
507 buffer->sglist = sglist;
508 } else {
509 sglist = buffer->sglist;
510 }
Laura Abbott894fd582011-08-19 13:33:56 -0700511
512out:
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700513 mutex_unlock(&buffer->lock);
514 mutex_unlock(&client->lock);
515 return sglist;
516}
517
518void ion_unmap_kernel(struct ion_client *client, struct ion_handle *handle)
519{
520 struct ion_buffer *buffer;
521
522 mutex_lock(&client->lock);
523 buffer = handle->buffer;
524 mutex_lock(&buffer->lock);
525 if (_ion_unmap(&buffer->kmap_cnt, &handle->kmap_cnt)) {
526 buffer->heap->ops->unmap_kernel(buffer->heap, buffer);
527 buffer->vaddr = NULL;
528 }
529 mutex_unlock(&buffer->lock);
530 mutex_unlock(&client->lock);
531}
532
533void ion_unmap_dma(struct ion_client *client, struct ion_handle *handle)
534{
535 struct ion_buffer *buffer;
536
537 mutex_lock(&client->lock);
538 buffer = handle->buffer;
539 mutex_lock(&buffer->lock);
540 if (_ion_unmap(&buffer->dmap_cnt, &handle->dmap_cnt)) {
541 buffer->heap->ops->unmap_dma(buffer->heap, buffer);
542 buffer->sglist = NULL;
543 }
544 mutex_unlock(&buffer->lock);
545 mutex_unlock(&client->lock);
546}
547
548
549struct ion_buffer *ion_share(struct ion_client *client,
550 struct ion_handle *handle)
551{
Rebecca Schultz Zavinc72866d2011-07-07 17:07:56 -0700552 bool valid_handle;
553
554 mutex_lock(&client->lock);
555 valid_handle = ion_handle_validate(client, handle);
556 mutex_unlock(&client->lock);
557 if (!valid_handle) {
558 WARN("%s: invalid handle passed to share.\n", __func__);
559 return ERR_PTR(-EINVAL);
560 }
561
Iliyan Malchev3fe24362011-08-09 14:42:08 -0700562 /* do not take an extra reference here, the burden is on the caller
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700563 * to make sure the buffer doesn't go away while it's passing it
564 * to another client -- ion_free should not be called on this handle
565 * until the buffer has been imported into the other client
566 */
567 return handle->buffer;
568}
569
570struct ion_handle *ion_import(struct ion_client *client,
571 struct ion_buffer *buffer)
572{
573 struct ion_handle *handle = NULL;
574
575 mutex_lock(&client->lock);
576 /* if a handle exists for this buffer just take a reference to it */
577 handle = ion_handle_lookup(client, buffer);
578 if (!IS_ERR_OR_NULL(handle)) {
579 ion_handle_get(handle);
580 goto end;
581 }
582 handle = ion_handle_create(client, buffer);
583 if (IS_ERR_OR_NULL(handle))
584 goto end;
585 ion_handle_add(client, handle);
586end:
587 mutex_unlock(&client->lock);
588 return handle;
589}
590
Laura Abbottabcb6f72011-10-04 16:26:49 -0700591static int check_vaddr_bounds(unsigned long start, unsigned long end)
592{
593 struct mm_struct *mm = current->active_mm;
594 struct vm_area_struct *vma;
595 int ret = 1;
596
597 if (end < start)
598 goto out;
599
600 down_read(&mm->mmap_sem);
601 vma = find_vma(mm, start);
602 if (vma && vma->vm_start < end) {
603 if (start < vma->vm_start)
604 goto out_up;
605 if (end > vma->vm_end)
606 goto out_up;
607 ret = 0;
608 }
609
610out_up:
611 up_read(&mm->mmap_sem);
612out:
613 return ret;
614}
615
616int ion_do_cache_op(struct ion_client *client, struct ion_handle *handle,
617 void *uaddr, unsigned long offset, unsigned long len,
618 unsigned int cmd)
619{
620 struct ion_buffer *buffer;
621 unsigned long start, end;
622 int ret = -EINVAL;
623
624 mutex_lock(&client->lock);
625 if (!ion_handle_validate(client, handle)) {
626 pr_err("%s: invalid handle passed to do_cache_op.\n",
627 __func__);
628 mutex_unlock(&client->lock);
629 return -EINVAL;
630 }
631 buffer = handle->buffer;
632 mutex_lock(&buffer->lock);
633
Laura Abbottcbaa6682011-10-19 12:14:14 -0700634 if (!ION_IS_CACHED(buffer->flags)) {
Laura Abbottabcb6f72011-10-04 16:26:49 -0700635 ret = 0;
636 goto out;
637 }
638
639 if (!handle->buffer->heap->ops->cache_op) {
640 pr_err("%s: cache_op is not implemented by this heap.\n",
641 __func__);
642 ret = -ENODEV;
643 goto out;
644 }
645
646 start = (unsigned long) uaddr;
647 end = (unsigned long) uaddr + len;
648
649 if (check_vaddr_bounds(start, end)) {
650 pr_err("%s: virtual address %p is out of bounds\n",
651 __func__, uaddr);
652 goto out;
653 }
654
655 ret = buffer->heap->ops->cache_op(buffer->heap, buffer, uaddr,
656 offset, len, cmd);
657
658out:
659 mutex_unlock(&buffer->lock);
660 mutex_unlock(&client->lock);
661 return ret;
662
663}
664
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700665static const struct file_operations ion_share_fops;
666
667struct ion_handle *ion_import_fd(struct ion_client *client, int fd)
668{
669 struct file *file = fget(fd);
670 struct ion_handle *handle;
671
672 if (!file) {
673 pr_err("%s: imported fd not found in file table.\n", __func__);
674 return ERR_PTR(-EINVAL);
675 }
676 if (file->f_op != &ion_share_fops) {
Laura Abbott084d6eb2011-10-24 19:09:50 -0700677 pr_err("%s: imported file %s is not a shared ion"
678 " file.", __func__, file->f_dentry->d_name.name);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700679 handle = ERR_PTR(-EINVAL);
680 goto end;
681 }
682 handle = ion_import(client, file->private_data);
683end:
684 fput(file);
685 return handle;
686}
687
688static int ion_debug_client_show(struct seq_file *s, void *unused)
689{
690 struct ion_client *client = s->private;
691 struct rb_node *n;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700692
Laura Abbott68c80642011-10-21 17:32:27 -0700693 seq_printf(s, "%16.16s: %16.16s : %16.16s : %16.16s\n", "heap_name",
694 "size_in_bytes", "handle refcount", "buffer");
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700695 mutex_lock(&client->lock);
696 for (n = rb_first(&client->handles); n; n = rb_next(n)) {
697 struct ion_handle *handle = rb_entry(n, struct ion_handle,
698 node);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700699
Laura Abbott68c80642011-10-21 17:32:27 -0700700 seq_printf(s, "%16.16s: %16u : %16d : %16p\n",
701 handle->buffer->heap->name,
702 handle->buffer->size,
703 atomic_read(&handle->ref.refcount),
704 handle->buffer);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700705 }
Laura Abbott68c80642011-10-21 17:32:27 -0700706
707 seq_printf(s, "%16.16s %d\n", "client refcount:",
708 atomic_read(&client->ref.refcount));
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700709 mutex_unlock(&client->lock);
710
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700711 return 0;
712}
713
714static int ion_debug_client_open(struct inode *inode, struct file *file)
715{
716 return single_open(file, ion_debug_client_show, inode->i_private);
717}
718
719static const struct file_operations debug_client_fops = {
720 .open = ion_debug_client_open,
721 .read = seq_read,
722 .llseek = seq_lseek,
723 .release = single_release,
724};
725
Rebecca Schultz Zavin83e3dab2011-07-01 20:41:25 -0700726static struct ion_client *ion_client_lookup(struct ion_device *dev,
727 struct task_struct *task)
728{
729 struct rb_node *n = dev->user_clients.rb_node;
730 struct ion_client *client;
731
732 mutex_lock(&dev->lock);
733 while (n) {
734 client = rb_entry(n, struct ion_client, node);
735 if (task == client->task) {
736 ion_client_get(client);
737 mutex_unlock(&dev->lock);
738 return client;
739 } else if (task < client->task) {
740 n = n->rb_left;
741 } else if (task > client->task) {
742 n = n->rb_right;
743 }
744 }
745 mutex_unlock(&dev->lock);
746 return NULL;
747}
748
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700749struct ion_client *ion_client_create(struct ion_device *dev,
750 unsigned int heap_mask,
751 const char *name)
752{
753 struct ion_client *client;
754 struct task_struct *task;
755 struct rb_node **p;
756 struct rb_node *parent = NULL;
757 struct ion_client *entry;
758 char debug_name[64];
Rebecca Schultz Zavin83e3dab2011-07-01 20:41:25 -0700759 pid_t pid;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700760
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700761 get_task_struct(current->group_leader);
762 task_lock(current->group_leader);
Rebecca Schultz Zavin83e3dab2011-07-01 20:41:25 -0700763 pid = task_pid_nr(current->group_leader);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700764 /* don't bother to store task struct for kernel threads,
765 they can't be killed anyway */
766 if (current->group_leader->flags & PF_KTHREAD) {
767 put_task_struct(current->group_leader);
768 task = NULL;
769 } else {
770 task = current->group_leader;
771 }
772 task_unlock(current->group_leader);
Rebecca Schultz Zavin83e3dab2011-07-01 20:41:25 -0700773
774 /* if this isn't a kernel thread, see if a client already
775 exists */
776 if (task) {
777 client = ion_client_lookup(dev, task);
778 if (!IS_ERR_OR_NULL(client)) {
779 put_task_struct(current->group_leader);
780 return client;
781 }
782 }
783
784 client = kzalloc(sizeof(struct ion_client), GFP_KERNEL);
785 if (!client) {
786 put_task_struct(current->group_leader);
787 return ERR_PTR(-ENOMEM);
788 }
789
790 client->dev = dev;
791 client->handles = RB_ROOT;
792 mutex_init(&client->lock);
793 client->name = name;
794 client->heap_mask = heap_mask;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700795 client->task = task;
Rebecca Schultz Zavin83e3dab2011-07-01 20:41:25 -0700796 client->pid = pid;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700797 kref_init(&client->ref);
798
799 mutex_lock(&dev->lock);
800 if (task) {
801 p = &dev->user_clients.rb_node;
802 while (*p) {
803 parent = *p;
804 entry = rb_entry(parent, struct ion_client, node);
805
806 if (task < entry->task)
807 p = &(*p)->rb_left;
808 else if (task > entry->task)
809 p = &(*p)->rb_right;
810 }
811 rb_link_node(&client->node, parent, p);
812 rb_insert_color(&client->node, &dev->user_clients);
813 } else {
814 p = &dev->kernel_clients.rb_node;
815 while (*p) {
816 parent = *p;
817 entry = rb_entry(parent, struct ion_client, node);
818
819 if (client < entry)
820 p = &(*p)->rb_left;
821 else if (client > entry)
822 p = &(*p)->rb_right;
823 }
824 rb_link_node(&client->node, parent, p);
825 rb_insert_color(&client->node, &dev->kernel_clients);
826 }
827
828 snprintf(debug_name, 64, "%u", client->pid);
829 client->debug_root = debugfs_create_file(debug_name, 0664,
830 dev->debug_root, client,
831 &debug_client_fops);
832 mutex_unlock(&dev->lock);
833
834 return client;
835}
836
Rebecca Schultz Zavin0b7e8ae2011-07-06 18:07:01 -0700837static void _ion_client_destroy(struct kref *kref)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700838{
Rebecca Schultz Zavin0b7e8ae2011-07-06 18:07:01 -0700839 struct ion_client *client = container_of(kref, struct ion_client, ref);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700840 struct ion_device *dev = client->dev;
841 struct rb_node *n;
842
843 pr_debug("%s: %d\n", __func__, __LINE__);
844 while ((n = rb_first(&client->handles))) {
845 struct ion_handle *handle = rb_entry(n, struct ion_handle,
846 node);
847 ion_handle_destroy(&handle->ref);
848 }
849 mutex_lock(&dev->lock);
850 if (client->task) {
851 rb_erase(&client->node, &dev->user_clients);
852 put_task_struct(client->task);
853 } else {
854 rb_erase(&client->node, &dev->kernel_clients);
855 }
856 debugfs_remove_recursive(client->debug_root);
857 mutex_unlock(&dev->lock);
858
859 kfree(client);
860}
861
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700862static void ion_client_get(struct ion_client *client)
863{
864 kref_get(&client->ref);
865}
866
867static int ion_client_put(struct ion_client *client)
868{
869 return kref_put(&client->ref, _ion_client_destroy);
870}
871
Rebecca Schultz Zavin0b7e8ae2011-07-06 18:07:01 -0700872void ion_client_destroy(struct ion_client *client)
873{
Jordan Crousea75022c2011-10-12 16:57:47 -0600874 if (client)
875 ion_client_put(client);
Rebecca Schultz Zavin0b7e8ae2011-07-06 18:07:01 -0700876}
877
Laura Abbott273dd8e2011-10-12 14:26:33 -0700878int ion_handle_get_flags(struct ion_client *client, struct ion_handle *handle,
879 unsigned long *flags)
880{
881 struct ion_buffer *buffer;
882
883 mutex_lock(&client->lock);
884 if (!ion_handle_validate(client, handle)) {
885 pr_err("%s: invalid handle passed to %s.\n",
886 __func__, __func__);
887 mutex_unlock(&client->lock);
888 return -EINVAL;
889 }
890 buffer = handle->buffer;
891 mutex_lock(&buffer->lock);
892 *flags = buffer->flags;
893 mutex_unlock(&buffer->lock);
894 mutex_unlock(&client->lock);
895
896 return 0;
897}
898EXPORT_SYMBOL(ion_handle_get_flags);
899
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700900static int ion_share_release(struct inode *inode, struct file* file)
901{
902 struct ion_buffer *buffer = file->private_data;
903
904 pr_debug("%s: %d\n", __func__, __LINE__);
Laura Abbott894fd582011-08-19 13:33:56 -0700905 mutex_lock(&buffer->lock);
906 buffer->umap_cnt--;
907 mutex_unlock(&buffer->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700908 /* drop the reference to the buffer -- this prevents the
909 buffer from going away because the client holding it exited
910 while it was being passed */
911 ion_buffer_put(buffer);
912 return 0;
913}
914
915static void ion_vma_open(struct vm_area_struct *vma)
916{
917
918 struct ion_buffer *buffer = vma->vm_file->private_data;
919 struct ion_handle *handle = vma->vm_private_data;
920 struct ion_client *client;
921
922 pr_debug("%s: %d\n", __func__, __LINE__);
923 /* check that the client still exists and take a reference so
924 it can't go away until this vma is closed */
925 client = ion_client_lookup(buffer->dev, current->group_leader);
926 if (IS_ERR_OR_NULL(client)) {
927 vma->vm_private_data = NULL;
928 return;
929 }
930 pr_debug("%s: %d client_cnt %d handle_cnt %d alloc_cnt %d\n",
931 __func__, __LINE__,
932 atomic_read(&client->ref.refcount),
933 atomic_read(&handle->ref.refcount),
934 atomic_read(&buffer->ref.refcount));
935}
936
937static void ion_vma_close(struct vm_area_struct *vma)
938{
939 struct ion_handle *handle = vma->vm_private_data;
940 struct ion_buffer *buffer = vma->vm_file->private_data;
941 struct ion_client *client;
942
943 pr_debug("%s: %d\n", __func__, __LINE__);
944 /* this indicates the client is gone, nothing to do here */
945 if (!handle)
946 return;
947 client = handle->client;
948 pr_debug("%s: %d client_cnt %d handle_cnt %d alloc_cnt %d\n",
949 __func__, __LINE__,
950 atomic_read(&client->ref.refcount),
951 atomic_read(&handle->ref.refcount),
952 atomic_read(&buffer->ref.refcount));
953 ion_handle_put(handle);
954 ion_client_put(client);
955 pr_debug("%s: %d client_cnt %d handle_cnt %d alloc_cnt %d\n",
956 __func__, __LINE__,
957 atomic_read(&client->ref.refcount),
958 atomic_read(&handle->ref.refcount),
959 atomic_read(&buffer->ref.refcount));
960}
961
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700962static struct vm_operations_struct ion_vm_ops = {
963 .open = ion_vma_open,
964 .close = ion_vma_close,
965};
966
967static int ion_share_mmap(struct file *file, struct vm_area_struct *vma)
968{
969 struct ion_buffer *buffer = file->private_data;
970 unsigned long size = vma->vm_end - vma->vm_start;
971 struct ion_client *client;
972 struct ion_handle *handle;
973 int ret;
Laura Abbott894fd582011-08-19 13:33:56 -0700974 unsigned long flags = file->f_flags & O_DSYNC ?
975 ION_SET_CACHE(UNCACHED) :
976 ION_SET_CACHE(CACHED);
977
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700978
979 pr_debug("%s: %d\n", __func__, __LINE__);
980 /* make sure the client still exists, it's possible for the client to
981 have gone away but the map/share fd still to be around, take
982 a reference to it so it can't go away while this mapping exists */
983 client = ion_client_lookup(buffer->dev, current->group_leader);
984 if (IS_ERR_OR_NULL(client)) {
985 pr_err("%s: trying to mmap an ion handle in a process with no "
986 "ion client\n", __func__);
987 return -EINVAL;
988 }
989
990 if ((size > buffer->size) || (size + (vma->vm_pgoff << PAGE_SHIFT) >
991 buffer->size)) {
992 pr_err("%s: trying to map larger area than handle has available"
993 "\n", __func__);
994 ret = -EINVAL;
995 goto err;
996 }
997
998 /* find the handle and take a reference to it */
999 handle = ion_import(client, buffer);
1000 if (IS_ERR_OR_NULL(handle)) {
1001 ret = -EINVAL;
1002 goto err;
1003 }
1004
1005 if (!handle->buffer->heap->ops->map_user) {
1006 pr_err("%s: this heap does not define a method for mapping "
1007 "to userspace\n", __func__);
1008 ret = -EINVAL;
1009 goto err1;
1010 }
1011
1012 mutex_lock(&buffer->lock);
Laura Abbott894fd582011-08-19 13:33:56 -07001013 if (buffer->kmap_cnt || buffer->dmap_cnt || buffer->umap_cnt) {
1014 if (buffer->flags != flags) {
1015 pr_err("%s: buffer was already mapped with flags %lx,"
1016 " cannot map with flags %lx\n", __func__,
1017 buffer->flags, flags);
1018 ret = -EEXIST;
1019 mutex_unlock(&buffer->lock);
1020 goto err1;
1021 }
1022
1023 } else {
1024 buffer->flags = flags;
1025 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001026 /* now map it to userspace */
Laura Abbott894fd582011-08-19 13:33:56 -07001027 ret = buffer->heap->ops->map_user(buffer->heap, buffer, vma,
1028 flags);
1029 buffer->umap_cnt++;
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001030 mutex_unlock(&buffer->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001031 if (ret) {
1032 pr_err("%s: failure mapping buffer to userspace\n",
1033 __func__);
Laura Abbott894fd582011-08-19 13:33:56 -07001034 goto err2;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001035 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001036
1037 vma->vm_ops = &ion_vm_ops;
1038 /* move the handle into the vm_private_data so we can access it from
1039 vma_open/close */
1040 vma->vm_private_data = handle;
1041 pr_debug("%s: %d client_cnt %d handle_cnt %d alloc_cnt %d\n",
1042 __func__, __LINE__,
1043 atomic_read(&client->ref.refcount),
1044 atomic_read(&handle->ref.refcount),
1045 atomic_read(&buffer->ref.refcount));
1046 return 0;
1047
Laura Abbott894fd582011-08-19 13:33:56 -07001048err2:
1049 buffer->umap_cnt--;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001050 /* drop the reference to the handle */
Laura Abbott894fd582011-08-19 13:33:56 -07001051err1:
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001052 ion_handle_put(handle);
1053err:
Iliyan Malchev3fe24362011-08-09 14:42:08 -07001054 /* drop the reference to the client */
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001055 ion_client_put(client);
1056 return ret;
1057}
1058
1059static const struct file_operations ion_share_fops = {
1060 .owner = THIS_MODULE,
1061 .release = ion_share_release,
1062 .mmap = ion_share_mmap,
1063};
1064
1065static int ion_ioctl_share(struct file *parent, struct ion_client *client,
1066 struct ion_handle *handle)
1067{
1068 int fd = get_unused_fd();
1069 struct file *file;
1070
1071 if (fd < 0)
1072 return -ENFILE;
1073
1074 file = anon_inode_getfile("ion_share_fd", &ion_share_fops,
1075 handle->buffer, O_RDWR);
1076 if (IS_ERR_OR_NULL(file))
1077 goto err;
Laura Abbott4b5d0482011-09-27 18:35:14 -07001078
1079 if (parent->f_flags & O_DSYNC)
1080 file->f_flags |= O_DSYNC;
1081
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001082 ion_buffer_get(handle->buffer);
1083 fd_install(fd, file);
1084
1085 return fd;
1086
1087err:
1088 put_unused_fd(fd);
1089 return -ENFILE;
1090}
1091
1092static long ion_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1093{
1094 struct ion_client *client = filp->private_data;
1095
1096 switch (cmd) {
1097 case ION_IOC_ALLOC:
1098 {
1099 struct ion_allocation_data data;
1100
1101 if (copy_from_user(&data, (void __user *)arg, sizeof(data)))
1102 return -EFAULT;
1103 data.handle = ion_alloc(client, data.len, data.align,
1104 data.flags);
1105 if (copy_to_user((void __user *)arg, &data, sizeof(data)))
1106 return -EFAULT;
1107 break;
1108 }
1109 case ION_IOC_FREE:
1110 {
1111 struct ion_handle_data data;
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001112 bool valid;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001113
1114 if (copy_from_user(&data, (void __user *)arg,
1115 sizeof(struct ion_handle_data)))
1116 return -EFAULT;
1117 mutex_lock(&client->lock);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001118 valid = ion_handle_validate(client, data.handle);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001119 mutex_unlock(&client->lock);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001120 if (!valid)
1121 return -EINVAL;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001122 ion_free(client, data.handle);
1123 break;
1124 }
1125 case ION_IOC_MAP:
1126 case ION_IOC_SHARE:
1127 {
1128 struct ion_fd_data data;
1129
1130 if (copy_from_user(&data, (void __user *)arg, sizeof(data)))
1131 return -EFAULT;
1132 mutex_lock(&client->lock);
1133 if (!ion_handle_validate(client, data.handle)) {
1134 pr_err("%s: invalid handle passed to share ioctl.\n",
1135 __func__);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001136 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001137 return -EINVAL;
1138 }
1139 data.fd = ion_ioctl_share(filp, client, data.handle);
1140 mutex_unlock(&client->lock);
1141 if (copy_to_user((void __user *)arg, &data, sizeof(data)))
1142 return -EFAULT;
1143 break;
1144 }
1145 case ION_IOC_IMPORT:
1146 {
1147 struct ion_fd_data data;
1148 if (copy_from_user(&data, (void __user *)arg,
1149 sizeof(struct ion_fd_data)))
1150 return -EFAULT;
1151
1152 data.handle = ion_import_fd(client, data.fd);
1153 if (IS_ERR(data.handle))
1154 data.handle = NULL;
1155 if (copy_to_user((void __user *)arg, &data,
1156 sizeof(struct ion_fd_data)))
1157 return -EFAULT;
1158 break;
1159 }
1160 case ION_IOC_CUSTOM:
1161 {
1162 struct ion_device *dev = client->dev;
1163 struct ion_custom_data data;
1164
1165 if (!dev->custom_ioctl)
1166 return -ENOTTY;
1167 if (copy_from_user(&data, (void __user *)arg,
1168 sizeof(struct ion_custom_data)))
1169 return -EFAULT;
1170 return dev->custom_ioctl(client, data.cmd, data.arg);
1171 }
Laura Abbottabcb6f72011-10-04 16:26:49 -07001172 case ION_IOC_CLEAN_CACHES:
1173 case ION_IOC_INV_CACHES:
1174 case ION_IOC_CLEAN_INV_CACHES:
1175 {
1176 struct ion_flush_data data;
1177
1178 if (copy_from_user(&data, (void __user *)arg,
1179 sizeof(struct ion_flush_data)))
1180 return -EFAULT;
1181
1182 return ion_do_cache_op(client, data.handle, data.vaddr,
1183 data.offset, data.length, cmd);
1184
1185 }
Laura Abbott273dd8e2011-10-12 14:26:33 -07001186 case ION_IOC_GET_FLAGS:
1187 {
1188 struct ion_flag_data data;
1189 int ret;
1190 if (copy_from_user(&data, (void __user *)arg,
1191 sizeof(struct ion_flag_data)))
1192 return -EFAULT;
1193
1194 ret = ion_handle_get_flags(client, data.handle, &data.flags);
1195 if (ret < 0)
1196 return ret;
1197 if (copy_to_user((void __user *)arg, &data,
1198 sizeof(struct ion_flag_data)))
1199 return -EFAULT;
1200 break;
1201 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001202 default:
1203 return -ENOTTY;
1204 }
1205 return 0;
1206}
1207
1208static int ion_release(struct inode *inode, struct file *file)
1209{
1210 struct ion_client *client = file->private_data;
1211
1212 pr_debug("%s: %d\n", __func__, __LINE__);
1213 ion_client_put(client);
1214 return 0;
1215}
1216
1217static int ion_open(struct inode *inode, struct file *file)
1218{
1219 struct miscdevice *miscdev = file->private_data;
1220 struct ion_device *dev = container_of(miscdev, struct ion_device, dev);
1221 struct ion_client *client;
1222
1223 pr_debug("%s: %d\n", __func__, __LINE__);
Rebecca Schultz Zavin6d3b9582011-07-06 18:07:24 -07001224 client = ion_client_create(dev, -1, "user");
1225 if (IS_ERR_OR_NULL(client))
1226 return PTR_ERR(client);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001227 file->private_data = client;
1228
1229 return 0;
1230}
1231
1232static const struct file_operations ion_fops = {
1233 .owner = THIS_MODULE,
1234 .open = ion_open,
1235 .release = ion_release,
1236 .unlocked_ioctl = ion_ioctl,
1237};
1238
1239static size_t ion_debug_heap_total(struct ion_client *client,
1240 enum ion_heap_type type)
1241{
1242 size_t size = 0;
1243 struct rb_node *n;
1244
1245 mutex_lock(&client->lock);
1246 for (n = rb_first(&client->handles); n; n = rb_next(n)) {
1247 struct ion_handle *handle = rb_entry(n,
1248 struct ion_handle,
1249 node);
1250 if (handle->buffer->heap->type == type)
1251 size += handle->buffer->size;
1252 }
1253 mutex_unlock(&client->lock);
1254 return size;
1255}
1256
1257static int ion_debug_heap_show(struct seq_file *s, void *unused)
1258{
1259 struct ion_heap *heap = s->private;
1260 struct ion_device *dev = heap->dev;
1261 struct rb_node *n;
1262
1263 seq_printf(s, "%16.s %16.s %16.s\n", "client", "pid", "size");
1264 for (n = rb_first(&dev->user_clients); n; n = rb_next(n)) {
1265 struct ion_client *client = rb_entry(n, struct ion_client,
1266 node);
1267 char task_comm[TASK_COMM_LEN];
1268 size_t size = ion_debug_heap_total(client, heap->type);
1269 if (!size)
1270 continue;
1271
1272 get_task_comm(task_comm, client->task);
1273 seq_printf(s, "%16.s %16u %16u\n", task_comm, client->pid,
1274 size);
1275 }
1276
1277 for (n = rb_first(&dev->kernel_clients); n; n = rb_next(n)) {
1278 struct ion_client *client = rb_entry(n, struct ion_client,
1279 node);
1280 size_t size = ion_debug_heap_total(client, heap->type);
1281 if (!size)
1282 continue;
1283 seq_printf(s, "%16.s %16u %16u\n", client->name, client->pid,
1284 size);
1285 }
Laura Abbott68c80642011-10-21 17:32:27 -07001286 if (heap->ops->get_allocated) {
1287 seq_printf(s, "total bytes currently allocated: %lx\n",
1288 heap->ops->get_allocated(heap));
1289 }
1290 if (heap->ops->get_total) {
1291 seq_printf(s, "total heap size: %lx\n",
1292 heap->ops->get_total(heap));
1293 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001294 return 0;
1295}
1296
1297static int ion_debug_heap_open(struct inode *inode, struct file *file)
1298{
1299 return single_open(file, ion_debug_heap_show, inode->i_private);
1300}
1301
1302static const struct file_operations debug_heap_fops = {
1303 .open = ion_debug_heap_open,
1304 .read = seq_read,
1305 .llseek = seq_lseek,
1306 .release = single_release,
1307};
1308
1309void ion_device_add_heap(struct ion_device *dev, struct ion_heap *heap)
1310{
1311 struct rb_node **p = &dev->heaps.rb_node;
1312 struct rb_node *parent = NULL;
1313 struct ion_heap *entry;
1314
1315 heap->dev = dev;
1316 mutex_lock(&dev->lock);
1317 while (*p) {
1318 parent = *p;
1319 entry = rb_entry(parent, struct ion_heap, node);
1320
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001321 if (heap->id < entry->id) {
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001322 p = &(*p)->rb_left;
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001323 } else if (heap->id > entry->id ) {
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001324 p = &(*p)->rb_right;
1325 } else {
1326 pr_err("%s: can not insert multiple heaps with "
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001327 "id %d\n", __func__, heap->id);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001328 goto end;
1329 }
1330 }
1331
1332 rb_link_node(&heap->node, parent, p);
1333 rb_insert_color(&heap->node, &dev->heaps);
1334 debugfs_create_file(heap->name, 0664, dev->debug_root, heap,
1335 &debug_heap_fops);
1336end:
1337 mutex_unlock(&dev->lock);
1338}
1339
1340struct ion_device *ion_device_create(long (*custom_ioctl)
1341 (struct ion_client *client,
1342 unsigned int cmd,
1343 unsigned long arg))
1344{
1345 struct ion_device *idev;
1346 int ret;
1347
1348 idev = kzalloc(sizeof(struct ion_device), GFP_KERNEL);
1349 if (!idev)
1350 return ERR_PTR(-ENOMEM);
1351
1352 idev->dev.minor = MISC_DYNAMIC_MINOR;
1353 idev->dev.name = "ion";
1354 idev->dev.fops = &ion_fops;
1355 idev->dev.parent = NULL;
1356 ret = misc_register(&idev->dev);
1357 if (ret) {
1358 pr_err("ion: failed to register misc device.\n");
1359 return ERR_PTR(ret);
1360 }
1361
1362 idev->debug_root = debugfs_create_dir("ion", NULL);
1363 if (IS_ERR_OR_NULL(idev->debug_root))
1364 pr_err("ion: failed to create debug files.\n");
1365
1366 idev->custom_ioctl = custom_ioctl;
1367 idev->buffers = RB_ROOT;
1368 mutex_init(&idev->lock);
1369 idev->heaps = RB_ROOT;
1370 idev->user_clients = RB_ROOT;
1371 idev->kernel_clients = RB_ROOT;
1372 return idev;
1373}
1374
1375void ion_device_destroy(struct ion_device *dev)
1376{
1377 misc_deregister(&dev->dev);
1378 /* XXX need to free the heaps and clients ? */
1379 kfree(dev);
1380}