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