blob: 142831543e49beac5ef01e44fe1a72af0975c03f [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
Laura Abbott8c017362011-09-22 20:59:12 -070033#include <mach/iommu_domains.h>
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070034#include "ion_priv.h"
35#define DEBUG
36
37/**
38 * struct ion_device - the metadata of the ion device node
39 * @dev: the actual misc device
40 * @buffers: an rb tree of all the existing buffers
41 * @lock: lock protecting the buffers & heaps trees
42 * @heaps: list of all the heaps in the system
43 * @user_clients: list of all the clients created from userspace
44 */
45struct ion_device {
46 struct miscdevice dev;
47 struct rb_root buffers;
48 struct mutex lock;
49 struct rb_root heaps;
50 long (*custom_ioctl) (struct ion_client *client, unsigned int cmd,
51 unsigned long arg);
52 struct rb_root user_clients;
53 struct rb_root kernel_clients;
54 struct dentry *debug_root;
55};
56
57/**
58 * struct ion_client - a process/hw block local address space
59 * @ref: for reference counting the client
60 * @node: node in the tree of all clients
61 * @dev: backpointer to ion device
62 * @handles: an rb tree of all the handles in this client
63 * @lock: lock protecting the tree of handles
64 * @heap_mask: mask of all supported heaps
65 * @name: used for debugging
66 * @task: used for debugging
67 *
68 * A client represents a list of buffers this client may access.
69 * The mutex stored here is used to protect both handles tree
70 * as well as the handles themselves, and should be held while modifying either.
71 */
72struct ion_client {
73 struct kref ref;
74 struct rb_node node;
75 struct ion_device *dev;
76 struct rb_root handles;
77 struct mutex lock;
78 unsigned int heap_mask;
79 const char *name;
80 struct task_struct *task;
81 pid_t pid;
82 struct dentry *debug_root;
83};
84
85/**
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -070086 * ion_handle - a client local reference to a buffer
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070087 * @ref: reference count
88 * @client: back pointer to the client the buffer resides in
89 * @buffer: pointer to the buffer
90 * @node: node in the client's handle rbtree
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -070091 * @kmap_cnt: count of times this client has mapped to kernel
92 * @dmap_cnt: count of times this client has mapped for dma
93 * @usermap_cnt: count of times this client has mapped for userspace
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070094 *
95 * Modifications to node, map_cnt or mapping should be protected by the
96 * lock in the client. Other fields are never changed after initialization.
97 */
98struct ion_handle {
99 struct kref ref;
100 struct ion_client *client;
101 struct ion_buffer *buffer;
102 struct rb_node node;
103 unsigned int kmap_cnt;
104 unsigned int dmap_cnt;
105 unsigned int usermap_cnt;
Laura Abbott8c017362011-09-22 20:59:12 -0700106 unsigned int iommu_map_cnt;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700107};
108
Laura Abbott8c017362011-09-22 20:59:12 -0700109static int ion_validate_buffer_flags(struct ion_buffer *buffer,
110 unsigned long flags)
111{
112 if (buffer->kmap_cnt || buffer->dmap_cnt || buffer->umap_cnt ||
113 buffer->iommu_map_cnt) {
114 if (buffer->flags != flags) {
115 pr_err("%s: buffer was already mapped with flags %lx,"
116 " cannot map with flags %lx\n", __func__,
117 buffer->flags, flags);
118 return 1;
119 }
120
121 } else {
122 buffer->flags = flags;
123 }
124 return 0;
125}
126
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700127/* this function should only be called while dev->lock is held */
128static void ion_buffer_add(struct ion_device *dev,
129 struct ion_buffer *buffer)
130{
131 struct rb_node **p = &dev->buffers.rb_node;
132 struct rb_node *parent = NULL;
133 struct ion_buffer *entry;
134
135 while (*p) {
136 parent = *p;
137 entry = rb_entry(parent, struct ion_buffer, node);
138
Rebecca Schultz Zavinf9fb95e2011-06-30 18:09:05 -0700139 if (buffer < entry) {
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700140 p = &(*p)->rb_left;
Rebecca Schultz Zavinf9fb95e2011-06-30 18:09:05 -0700141 } else if (buffer > entry) {
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700142 p = &(*p)->rb_right;
Rebecca Schultz Zavinf9fb95e2011-06-30 18:09:05 -0700143 } else {
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700144 pr_err("%s: buffer already found.", __func__);
145 BUG();
Rebecca Schultz Zavinf9fb95e2011-06-30 18:09:05 -0700146 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700147 }
148
149 rb_link_node(&buffer->node, parent, p);
150 rb_insert_color(&buffer->node, &dev->buffers);
151}
152
Laura Abbott8c017362011-09-22 20:59:12 -0700153void ion_iommu_add(struct ion_buffer *buffer,
154 struct ion_iommu_map *iommu)
155{
156 struct rb_node **p = &buffer->iommu_maps.rb_node;
157 struct rb_node *parent = NULL;
158 struct ion_iommu_map *entry;
159
160 while (*p) {
161 parent = *p;
162 entry = rb_entry(parent, struct ion_iommu_map, node);
163
164 if (iommu->key < entry->key) {
165 p = &(*p)->rb_left;
166 } else if (iommu->key > entry->key) {
167 p = &(*p)->rb_right;
168 } else {
169 pr_err("%s: buffer %p already has mapping for domain %d"
170 " and partition %d\n", __func__,
171 buffer,
172 iommu_map_domain(iommu),
173 iommu_map_partition(iommu));
174 BUG();
175 }
176 }
177
178 rb_link_node(&iommu->node, parent, p);
179 rb_insert_color(&iommu->node, &buffer->iommu_maps);
180
181}
182
183static struct ion_iommu_map *ion_iommu_lookup(struct ion_buffer *buffer,
184 unsigned int domain_no,
185 unsigned int partition_no)
186{
187 struct rb_node **p = &buffer->iommu_maps.rb_node;
188 struct rb_node *parent = NULL;
189 struct ion_iommu_map *entry;
190 uint64_t key = domain_no;
191 key = key << 32 | partition_no;
192
193 while (*p) {
194 parent = *p;
195 entry = rb_entry(parent, struct ion_iommu_map, node);
196
197 if (key < entry->key)
198 p = &(*p)->rb_left;
199 else if (key > entry->key)
200 p = &(*p)->rb_right;
201 else
202 return entry;
203 }
204
205 return NULL;
206}
207
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700208/* this function should only be called while dev->lock is held */
Iliyan Malchev3fe24362011-08-09 14:42:08 -0700209static struct ion_buffer *ion_buffer_create(struct ion_heap *heap,
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700210 struct ion_device *dev,
211 unsigned long len,
212 unsigned long align,
213 unsigned long flags)
214{
215 struct ion_buffer *buffer;
216 int ret;
217
218 buffer = kzalloc(sizeof(struct ion_buffer), GFP_KERNEL);
219 if (!buffer)
220 return ERR_PTR(-ENOMEM);
221
222 buffer->heap = heap;
223 kref_init(&buffer->ref);
224
225 ret = heap->ops->allocate(heap, buffer, len, align, flags);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700226 if (ret) {
227 kfree(buffer);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700228 return ERR_PTR(ret);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700229 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700230 buffer->dev = dev;
231 buffer->size = len;
232 mutex_init(&buffer->lock);
233 ion_buffer_add(dev, buffer);
234 return buffer;
235}
236
237static void ion_buffer_destroy(struct kref *kref)
238{
239 struct ion_buffer *buffer = container_of(kref, struct ion_buffer, ref);
240 struct ion_device *dev = buffer->dev;
241
242 buffer->heap->ops->free(buffer);
243 mutex_lock(&dev->lock);
244 rb_erase(&buffer->node, &dev->buffers);
245 mutex_unlock(&dev->lock);
246 kfree(buffer);
247}
248
249static void ion_buffer_get(struct ion_buffer *buffer)
250{
251 kref_get(&buffer->ref);
252}
253
254static int ion_buffer_put(struct ion_buffer *buffer)
255{
256 return kref_put(&buffer->ref, ion_buffer_destroy);
257}
258
Iliyan Malchev3fe24362011-08-09 14:42:08 -0700259static struct ion_handle *ion_handle_create(struct ion_client *client,
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700260 struct ion_buffer *buffer)
261{
262 struct ion_handle *handle;
263
264 handle = kzalloc(sizeof(struct ion_handle), GFP_KERNEL);
265 if (!handle)
266 return ERR_PTR(-ENOMEM);
267 kref_init(&handle->ref);
Iliyan Malchev3fe24362011-08-09 14:42:08 -0700268 rb_init_node(&handle->node);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700269 handle->client = client;
270 ion_buffer_get(buffer);
271 handle->buffer = buffer;
272
273 return handle;
274}
275
276static void ion_handle_destroy(struct kref *kref)
277{
278 struct ion_handle *handle = container_of(kref, struct ion_handle, ref);
279 /* XXX Can a handle be destroyed while it's map count is non-zero?:
280 if (handle->map_cnt) unmap
281 */
Laura Abbottd2a87372011-10-20 17:53:49 -0700282 WARN_ON(handle->kmap_cnt || handle->dmap_cnt || handle->usermap_cnt);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700283 ion_buffer_put(handle->buffer);
284 mutex_lock(&handle->client->lock);
Iliyan Malchev3fe24362011-08-09 14:42:08 -0700285 if (!RB_EMPTY_NODE(&handle->node))
286 rb_erase(&handle->node, &handle->client->handles);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700287 mutex_unlock(&handle->client->lock);
288 kfree(handle);
289}
290
291struct ion_buffer *ion_handle_buffer(struct ion_handle *handle)
292{
293 return handle->buffer;
294}
295
296static void ion_handle_get(struct ion_handle *handle)
297{
298 kref_get(&handle->ref);
299}
300
301static int ion_handle_put(struct ion_handle *handle)
302{
303 return kref_put(&handle->ref, ion_handle_destroy);
304}
305
306static struct ion_handle *ion_handle_lookup(struct ion_client *client,
307 struct ion_buffer *buffer)
308{
309 struct rb_node *n;
310
311 for (n = rb_first(&client->handles); n; n = rb_next(n)) {
312 struct ion_handle *handle = rb_entry(n, struct ion_handle,
313 node);
314 if (handle->buffer == buffer)
315 return handle;
316 }
317 return NULL;
318}
319
Iliyan Malchev3fe24362011-08-09 14:42:08 -0700320static bool ion_handle_validate(struct ion_client *client, struct ion_handle *handle)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700321{
322 struct rb_node *n = client->handles.rb_node;
323
324 while (n) {
325 struct ion_handle *handle_node = rb_entry(n, struct ion_handle,
326 node);
327 if (handle < handle_node)
328 n = n->rb_left;
329 else if (handle > handle_node)
330 n = n->rb_right;
331 else
332 return true;
333 }
334 return false;
335}
336
337static void ion_handle_add(struct ion_client *client, struct ion_handle *handle)
338{
339 struct rb_node **p = &client->handles.rb_node;
340 struct rb_node *parent = NULL;
341 struct ion_handle *entry;
342
343 while (*p) {
344 parent = *p;
345 entry = rb_entry(parent, struct ion_handle, node);
346
347 if (handle < entry)
348 p = &(*p)->rb_left;
349 else if (handle > entry)
350 p = &(*p)->rb_right;
351 else
352 WARN(1, "%s: buffer already found.", __func__);
353 }
354
355 rb_link_node(&handle->node, parent, p);
356 rb_insert_color(&handle->node, &client->handles);
357}
358
359struct ion_handle *ion_alloc(struct ion_client *client, size_t len,
360 size_t align, unsigned int flags)
361{
362 struct rb_node *n;
363 struct ion_handle *handle;
364 struct ion_device *dev = client->dev;
365 struct ion_buffer *buffer = NULL;
366
367 /*
368 * traverse the list of heaps available in this system in priority
369 * order. If the heap type is supported by the client, and matches the
370 * request of the caller allocate from it. Repeat until allocate has
371 * succeeded or all heaps have been tried
372 */
373 mutex_lock(&dev->lock);
374 for (n = rb_first(&dev->heaps); n != NULL; n = rb_next(n)) {
375 struct ion_heap *heap = rb_entry(n, struct ion_heap, node);
376 /* if the client doesn't support this heap type */
377 if (!((1 << heap->type) & client->heap_mask))
378 continue;
379 /* if the caller didn't specify this heap type */
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700380 if (!((1 << heap->id) & flags))
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700381 continue;
382 buffer = ion_buffer_create(heap, dev, len, align, flags);
383 if (!IS_ERR_OR_NULL(buffer))
384 break;
385 }
386 mutex_unlock(&dev->lock);
387
388 if (IS_ERR_OR_NULL(buffer))
389 return ERR_PTR(PTR_ERR(buffer));
390
391 handle = ion_handle_create(client, buffer);
392
393 if (IS_ERR_OR_NULL(handle))
394 goto end;
395
396 /*
397 * ion_buffer_create will create a buffer with a ref_cnt of 1,
398 * and ion_handle_create will take a second reference, drop one here
399 */
400 ion_buffer_put(buffer);
401
402 mutex_lock(&client->lock);
403 ion_handle_add(client, handle);
404 mutex_unlock(&client->lock);
405 return handle;
406
407end:
408 ion_buffer_put(buffer);
409 return handle;
410}
411
412void ion_free(struct ion_client *client, struct ion_handle *handle)
413{
Rebecca Schultz Zavinc72866d2011-07-07 17:07:56 -0700414 bool valid_handle;
415
416 BUG_ON(client != handle->client);
417
418 mutex_lock(&client->lock);
419 valid_handle = ion_handle_validate(client, handle);
420 mutex_unlock(&client->lock);
421
422 if (!valid_handle) {
423 WARN("%s: invalid handle passed to free.\n", __func__);
424 return;
425 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700426 ion_handle_put(handle);
427}
428
429static void ion_client_get(struct ion_client *client);
430static int ion_client_put(struct ion_client *client);
431
Iliyan Malchev3fe24362011-08-09 14:42:08 -0700432static bool _ion_map(int *buffer_cnt, int *handle_cnt)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700433{
434 bool map;
435
436 BUG_ON(*handle_cnt != 0 && *buffer_cnt == 0);
437
438 if (*buffer_cnt)
439 map = false;
440 else
441 map = true;
442 if (*handle_cnt == 0)
443 (*buffer_cnt)++;
444 (*handle_cnt)++;
445 return map;
446}
447
Iliyan Malchev3fe24362011-08-09 14:42:08 -0700448static bool _ion_unmap(int *buffer_cnt, int *handle_cnt)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700449{
450 BUG_ON(*handle_cnt == 0);
451 (*handle_cnt)--;
452 if (*handle_cnt != 0)
453 return false;
454 BUG_ON(*buffer_cnt == 0);
455 (*buffer_cnt)--;
456 if (*buffer_cnt == 0)
457 return true;
458 return false;
459}
460
461int ion_phys(struct ion_client *client, struct ion_handle *handle,
462 ion_phys_addr_t *addr, size_t *len)
463{
464 struct ion_buffer *buffer;
465 int ret;
466
467 mutex_lock(&client->lock);
468 if (!ion_handle_validate(client, handle)) {
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700469 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700470 return -EINVAL;
471 }
472
473 buffer = handle->buffer;
474
475 if (!buffer->heap->ops->phys) {
476 pr_err("%s: ion_phys is not implemented by this heap.\n",
477 __func__);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700478 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700479 return -ENODEV;
480 }
481 mutex_unlock(&client->lock);
482 ret = buffer->heap->ops->phys(buffer->heap, buffer, addr, len);
483 return ret;
484}
485
Laura Abbott894fd582011-08-19 13:33:56 -0700486void *ion_map_kernel(struct ion_client *client, struct ion_handle *handle,
487 unsigned long flags)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700488{
489 struct ion_buffer *buffer;
490 void *vaddr;
491
492 mutex_lock(&client->lock);
493 if (!ion_handle_validate(client, handle)) {
494 pr_err("%s: invalid handle passed to map_kernel.\n",
495 __func__);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700496 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700497 return ERR_PTR(-EINVAL);
498 }
499
500 buffer = handle->buffer;
501 mutex_lock(&buffer->lock);
502
503 if (!handle->buffer->heap->ops->map_kernel) {
504 pr_err("%s: map_kernel is not implemented by this heap.\n",
505 __func__);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700506 mutex_unlock(&buffer->lock);
507 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700508 return ERR_PTR(-ENODEV);
509 }
510
Laura Abbott8c017362011-09-22 20:59:12 -0700511 if (ion_validate_buffer_flags(buffer, flags)) {
Laura Abbott894fd582011-08-19 13:33:56 -0700512 vaddr = ERR_PTR(-EEXIST);
513 goto out;
Laura Abbott894fd582011-08-19 13:33:56 -0700514 }
515
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700516 if (_ion_map(&buffer->kmap_cnt, &handle->kmap_cnt)) {
Laura Abbott894fd582011-08-19 13:33:56 -0700517 vaddr = buffer->heap->ops->map_kernel(buffer->heap, buffer,
518 flags);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700519 if (IS_ERR_OR_NULL(vaddr))
520 _ion_unmap(&buffer->kmap_cnt, &handle->kmap_cnt);
521 buffer->vaddr = vaddr;
522 } else {
523 vaddr = buffer->vaddr;
524 }
Laura Abbott894fd582011-08-19 13:33:56 -0700525
526out:
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700527 mutex_unlock(&buffer->lock);
528 mutex_unlock(&client->lock);
529 return vaddr;
530}
531
Laura Abbott8c017362011-09-22 20:59:12 -0700532int __ion_iommu_map(struct ion_buffer *buffer,
533 int domain_num, int partition_num, unsigned long align,
534 unsigned long iova_length, unsigned long flags,
535 unsigned long *iova)
536{
537 struct ion_iommu_map *data;
538 int ret;
539
540 data = kmalloc(sizeof(*data), GFP_ATOMIC);
541
542 if (!data)
543 return -ENOMEM;
544
545 data->buffer = buffer;
546 iommu_map_domain(data) = domain_num;
547 iommu_map_partition(data) = partition_num;
548
549 ret = buffer->heap->ops->map_iommu(buffer, data,
550 domain_num,
551 partition_num,
552 align,
553 iova_length,
554 flags);
555
556 if (ret)
557 goto out;
558
559 kref_init(&data->ref);
560 *iova = data->iova_addr;
561
562 ion_iommu_add(buffer, data);
563
564 return 0;
565
566out:
567 msm_free_iova_address(data->iova_addr, domain_num, partition_num,
568 buffer->size);
569 kfree(data);
570 return ret;
571}
572
573int ion_map_iommu(struct ion_client *client, struct ion_handle *handle,
574 int domain_num, int partition_num, unsigned long align,
575 unsigned long iova_length, unsigned long *iova,
576 unsigned long *buffer_size,
577 unsigned long flags)
578{
579 struct ion_buffer *buffer;
580 struct ion_iommu_map *iommu_map;
581 int ret = 0;
582
583 mutex_lock(&client->lock);
584 if (!ion_handle_validate(client, handle)) {
585 pr_err("%s: invalid handle passed to map_kernel.\n",
586 __func__);
587 mutex_unlock(&client->lock);
588 return -EINVAL;
589 }
590
591 buffer = handle->buffer;
592 mutex_lock(&buffer->lock);
593
594 if (!handle->buffer->heap->ops->map_iommu) {
595 pr_err("%s: map_iommu is not implemented by this heap.\n",
596 __func__);
597 ret = -ENODEV;
598 goto out;
599 }
600
601 if (ion_validate_buffer_flags(buffer, flags)) {
602 ret = -EEXIST;
603 goto out;
604 }
605
606 /*
607 * If clients don't want a custom iova length, just use whatever
608 * the buffer size is
609 */
610 if (!iova_length)
611 iova_length = buffer->size;
612
613 if (buffer->size > iova_length) {
614 pr_debug("%s: iova length %lx is not at least buffer size"
615 " %x\n", __func__, iova_length, buffer->size);
616 ret = -EINVAL;
617 goto out;
618 }
619
620 if (buffer->size & ~PAGE_MASK) {
621 pr_debug("%s: buffer size %x is not aligned to %lx", __func__,
622 buffer->size, PAGE_SIZE);
623 ret = -EINVAL;
624 goto out;
625 }
626
627 if (iova_length & ~PAGE_MASK) {
628 pr_debug("%s: iova_length %lx is not aligned to %lx", __func__,
629 iova_length, PAGE_SIZE);
630 ret = -EINVAL;
631 goto out;
632 }
633
634 iommu_map = ion_iommu_lookup(buffer, domain_num, partition_num);
635 if (_ion_map(&buffer->iommu_map_cnt, &handle->iommu_map_cnt) ||
636 !iommu_map) {
637 ret = __ion_iommu_map(buffer, domain_num, partition_num, align,
638 iova_length, flags, iova);
639 if (ret < 0)
640 _ion_unmap(&buffer->iommu_map_cnt,
641 &handle->iommu_map_cnt);
642 } else {
643 if (iommu_map->mapped_size != iova_length) {
644 pr_err("%s: handle %p is already mapped with length"
645 " %x, trying to map with length %lx\n",
646 __func__, handle, iommu_map->mapped_size,
647 iova_length);
648 _ion_unmap(&buffer->iommu_map_cnt,
649 &handle->iommu_map_cnt);
650 ret = -EINVAL;
651 } else {
652 kref_get(&iommu_map->ref);
653 *iova = iommu_map->iova_addr;
654 }
655 }
656 *buffer_size = buffer->size;
657out:
658 mutex_unlock(&buffer->lock);
659 mutex_unlock(&client->lock);
660 return ret;
661}
662EXPORT_SYMBOL(ion_map_iommu);
663
664static void ion_iommu_release(struct kref *kref)
665{
666 struct ion_iommu_map *map = container_of(kref, struct ion_iommu_map,
667 ref);
668 struct ion_buffer *buffer = map->buffer;
669
670 rb_erase(&map->node, &buffer->iommu_maps);
671 buffer->heap->ops->unmap_iommu(map);
672 kfree(map);
673}
674
675void ion_unmap_iommu(struct ion_client *client, struct ion_handle *handle,
676 int domain_num, int partition_num)
677{
678 struct ion_iommu_map *iommu_map;
679 struct ion_buffer *buffer;
680
681 mutex_lock(&client->lock);
682 buffer = handle->buffer;
683
684 mutex_lock(&buffer->lock);
685
686 iommu_map = ion_iommu_lookup(buffer, domain_num, partition_num);
687
688 if (!iommu_map) {
689 WARN(1, "%s: (%d,%d) was never mapped for %p\n", __func__,
690 domain_num, partition_num, buffer);
691 goto out;
692 }
693
694 _ion_unmap(&buffer->iommu_map_cnt, &handle->iommu_map_cnt);
695 kref_put(&iommu_map->ref, ion_iommu_release);
696
697out:
698 mutex_unlock(&buffer->lock);
699
700 mutex_unlock(&client->lock);
701
702}
703EXPORT_SYMBOL(ion_unmap_iommu);
704
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700705struct scatterlist *ion_map_dma(struct ion_client *client,
Laura Abbott894fd582011-08-19 13:33:56 -0700706 struct ion_handle *handle,
707 unsigned long flags)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700708{
709 struct ion_buffer *buffer;
710 struct scatterlist *sglist;
711
712 mutex_lock(&client->lock);
713 if (!ion_handle_validate(client, handle)) {
714 pr_err("%s: invalid handle passed to map_dma.\n",
715 __func__);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700716 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700717 return ERR_PTR(-EINVAL);
718 }
719 buffer = handle->buffer;
720 mutex_lock(&buffer->lock);
721
722 if (!handle->buffer->heap->ops->map_dma) {
723 pr_err("%s: map_kernel is not implemented by this heap.\n",
724 __func__);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700725 mutex_unlock(&buffer->lock);
726 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700727 return ERR_PTR(-ENODEV);
728 }
Laura Abbott894fd582011-08-19 13:33:56 -0700729
Laura Abbott8c017362011-09-22 20:59:12 -0700730 if (ion_validate_buffer_flags(buffer, flags)) {
731 sglist = ERR_PTR(-EEXIST);
732 goto out;
Laura Abbott894fd582011-08-19 13:33:56 -0700733 }
734
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700735 if (_ion_map(&buffer->dmap_cnt, &handle->dmap_cnt)) {
736 sglist = buffer->heap->ops->map_dma(buffer->heap, buffer);
737 if (IS_ERR_OR_NULL(sglist))
738 _ion_unmap(&buffer->dmap_cnt, &handle->dmap_cnt);
739 buffer->sglist = sglist;
740 } else {
741 sglist = buffer->sglist;
742 }
Laura Abbott894fd582011-08-19 13:33:56 -0700743
744out:
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700745 mutex_unlock(&buffer->lock);
746 mutex_unlock(&client->lock);
747 return sglist;
748}
749
750void ion_unmap_kernel(struct ion_client *client, struct ion_handle *handle)
751{
752 struct ion_buffer *buffer;
753
754 mutex_lock(&client->lock);
755 buffer = handle->buffer;
756 mutex_lock(&buffer->lock);
757 if (_ion_unmap(&buffer->kmap_cnt, &handle->kmap_cnt)) {
758 buffer->heap->ops->unmap_kernel(buffer->heap, buffer);
759 buffer->vaddr = NULL;
760 }
761 mutex_unlock(&buffer->lock);
762 mutex_unlock(&client->lock);
763}
764
765void ion_unmap_dma(struct ion_client *client, struct ion_handle *handle)
766{
767 struct ion_buffer *buffer;
768
769 mutex_lock(&client->lock);
770 buffer = handle->buffer;
771 mutex_lock(&buffer->lock);
772 if (_ion_unmap(&buffer->dmap_cnt, &handle->dmap_cnt)) {
773 buffer->heap->ops->unmap_dma(buffer->heap, buffer);
774 buffer->sglist = NULL;
775 }
776 mutex_unlock(&buffer->lock);
777 mutex_unlock(&client->lock);
778}
779
780
781struct ion_buffer *ion_share(struct ion_client *client,
782 struct ion_handle *handle)
783{
Rebecca Schultz Zavinc72866d2011-07-07 17:07:56 -0700784 bool valid_handle;
785
786 mutex_lock(&client->lock);
787 valid_handle = ion_handle_validate(client, handle);
788 mutex_unlock(&client->lock);
789 if (!valid_handle) {
790 WARN("%s: invalid handle passed to share.\n", __func__);
791 return ERR_PTR(-EINVAL);
792 }
793
Iliyan Malchev3fe24362011-08-09 14:42:08 -0700794 /* do not take an extra reference here, the burden is on the caller
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700795 * to make sure the buffer doesn't go away while it's passing it
796 * to another client -- ion_free should not be called on this handle
797 * until the buffer has been imported into the other client
798 */
799 return handle->buffer;
800}
801
802struct ion_handle *ion_import(struct ion_client *client,
803 struct ion_buffer *buffer)
804{
805 struct ion_handle *handle = NULL;
806
807 mutex_lock(&client->lock);
808 /* if a handle exists for this buffer just take a reference to it */
809 handle = ion_handle_lookup(client, buffer);
810 if (!IS_ERR_OR_NULL(handle)) {
811 ion_handle_get(handle);
812 goto end;
813 }
814 handle = ion_handle_create(client, buffer);
815 if (IS_ERR_OR_NULL(handle))
816 goto end;
817 ion_handle_add(client, handle);
818end:
819 mutex_unlock(&client->lock);
820 return handle;
821}
822
Laura Abbottabcb6f72011-10-04 16:26:49 -0700823static int check_vaddr_bounds(unsigned long start, unsigned long end)
824{
825 struct mm_struct *mm = current->active_mm;
826 struct vm_area_struct *vma;
827 int ret = 1;
828
829 if (end < start)
830 goto out;
831
832 down_read(&mm->mmap_sem);
833 vma = find_vma(mm, start);
834 if (vma && vma->vm_start < end) {
835 if (start < vma->vm_start)
836 goto out_up;
837 if (end > vma->vm_end)
838 goto out_up;
839 ret = 0;
840 }
841
842out_up:
843 up_read(&mm->mmap_sem);
844out:
845 return ret;
846}
847
848int ion_do_cache_op(struct ion_client *client, struct ion_handle *handle,
849 void *uaddr, unsigned long offset, unsigned long len,
850 unsigned int cmd)
851{
852 struct ion_buffer *buffer;
Laura Abbottabcb6f72011-10-04 16:26:49 -0700853 int ret = -EINVAL;
854
855 mutex_lock(&client->lock);
856 if (!ion_handle_validate(client, handle)) {
857 pr_err("%s: invalid handle passed to do_cache_op.\n",
858 __func__);
859 mutex_unlock(&client->lock);
860 return -EINVAL;
861 }
862 buffer = handle->buffer;
863 mutex_lock(&buffer->lock);
864
Laura Abbottcbaa6682011-10-19 12:14:14 -0700865 if (!ION_IS_CACHED(buffer->flags)) {
Laura Abbottabcb6f72011-10-04 16:26:49 -0700866 ret = 0;
867 goto out;
868 }
869
870 if (!handle->buffer->heap->ops->cache_op) {
871 pr_err("%s: cache_op is not implemented by this heap.\n",
872 __func__);
873 ret = -ENODEV;
874 goto out;
875 }
876
Laura Abbottabcb6f72011-10-04 16:26:49 -0700877
878 ret = buffer->heap->ops->cache_op(buffer->heap, buffer, uaddr,
879 offset, len, cmd);
880
881out:
882 mutex_unlock(&buffer->lock);
883 mutex_unlock(&client->lock);
884 return ret;
885
886}
887
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700888static const struct file_operations ion_share_fops;
889
890struct ion_handle *ion_import_fd(struct ion_client *client, int fd)
891{
892 struct file *file = fget(fd);
893 struct ion_handle *handle;
894
895 if (!file) {
896 pr_err("%s: imported fd not found in file table.\n", __func__);
897 return ERR_PTR(-EINVAL);
898 }
899 if (file->f_op != &ion_share_fops) {
Laura Abbott084d6eb2011-10-24 19:09:50 -0700900 pr_err("%s: imported file %s is not a shared ion"
901 " file.", __func__, file->f_dentry->d_name.name);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700902 handle = ERR_PTR(-EINVAL);
903 goto end;
904 }
905 handle = ion_import(client, file->private_data);
906end:
907 fput(file);
908 return handle;
909}
910
911static int ion_debug_client_show(struct seq_file *s, void *unused)
912{
913 struct ion_client *client = s->private;
914 struct rb_node *n;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700915
Laura Abbott68c80642011-10-21 17:32:27 -0700916 seq_printf(s, "%16.16s: %16.16s : %16.16s : %16.16s\n", "heap_name",
917 "size_in_bytes", "handle refcount", "buffer");
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700918 mutex_lock(&client->lock);
919 for (n = rb_first(&client->handles); n; n = rb_next(n)) {
920 struct ion_handle *handle = rb_entry(n, struct ion_handle,
921 node);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700922
Laura Abbott8747bbe2011-10-31 14:18:13 -0700923 seq_printf(s, "%16.16s: %16x : %16d : %16p\n",
Laura Abbott68c80642011-10-21 17:32:27 -0700924 handle->buffer->heap->name,
925 handle->buffer->size,
926 atomic_read(&handle->ref.refcount),
927 handle->buffer);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700928 }
Laura Abbott68c80642011-10-21 17:32:27 -0700929
930 seq_printf(s, "%16.16s %d\n", "client refcount:",
931 atomic_read(&client->ref.refcount));
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700932 mutex_unlock(&client->lock);
933
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700934 return 0;
935}
936
937static int ion_debug_client_open(struct inode *inode, struct file *file)
938{
939 return single_open(file, ion_debug_client_show, inode->i_private);
940}
941
942static const struct file_operations debug_client_fops = {
943 .open = ion_debug_client_open,
944 .read = seq_read,
945 .llseek = seq_lseek,
946 .release = single_release,
947};
948
Rebecca Schultz Zavin83e3dab2011-07-01 20:41:25 -0700949static struct ion_client *ion_client_lookup(struct ion_device *dev,
950 struct task_struct *task)
951{
952 struct rb_node *n = dev->user_clients.rb_node;
953 struct ion_client *client;
954
955 mutex_lock(&dev->lock);
956 while (n) {
957 client = rb_entry(n, struct ion_client, node);
958 if (task == client->task) {
959 ion_client_get(client);
960 mutex_unlock(&dev->lock);
961 return client;
962 } else if (task < client->task) {
963 n = n->rb_left;
964 } else if (task > client->task) {
965 n = n->rb_right;
966 }
967 }
968 mutex_unlock(&dev->lock);
969 return NULL;
970}
971
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700972struct ion_client *ion_client_create(struct ion_device *dev,
973 unsigned int heap_mask,
974 const char *name)
975{
976 struct ion_client *client;
977 struct task_struct *task;
978 struct rb_node **p;
979 struct rb_node *parent = NULL;
980 struct ion_client *entry;
Rebecca Schultz Zavin83e3dab2011-07-01 20:41:25 -0700981 pid_t pid;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700982
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700983 get_task_struct(current->group_leader);
984 task_lock(current->group_leader);
Rebecca Schultz Zavin83e3dab2011-07-01 20:41:25 -0700985 pid = task_pid_nr(current->group_leader);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700986 /* don't bother to store task struct for kernel threads,
987 they can't be killed anyway */
988 if (current->group_leader->flags & PF_KTHREAD) {
989 put_task_struct(current->group_leader);
990 task = NULL;
991 } else {
992 task = current->group_leader;
993 }
994 task_unlock(current->group_leader);
Rebecca Schultz Zavin83e3dab2011-07-01 20:41:25 -0700995
996 /* if this isn't a kernel thread, see if a client already
997 exists */
998 if (task) {
999 client = ion_client_lookup(dev, task);
1000 if (!IS_ERR_OR_NULL(client)) {
1001 put_task_struct(current->group_leader);
1002 return client;
1003 }
1004 }
1005
1006 client = kzalloc(sizeof(struct ion_client), GFP_KERNEL);
1007 if (!client) {
1008 put_task_struct(current->group_leader);
1009 return ERR_PTR(-ENOMEM);
1010 }
1011
1012 client->dev = dev;
1013 client->handles = RB_ROOT;
1014 mutex_init(&client->lock);
1015 client->name = name;
1016 client->heap_mask = heap_mask;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001017 client->task = task;
Rebecca Schultz Zavin83e3dab2011-07-01 20:41:25 -07001018 client->pid = pid;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001019 kref_init(&client->ref);
1020
1021 mutex_lock(&dev->lock);
1022 if (task) {
1023 p = &dev->user_clients.rb_node;
1024 while (*p) {
1025 parent = *p;
1026 entry = rb_entry(parent, struct ion_client, node);
1027
1028 if (task < entry->task)
1029 p = &(*p)->rb_left;
1030 else if (task > entry->task)
1031 p = &(*p)->rb_right;
1032 }
1033 rb_link_node(&client->node, parent, p);
1034 rb_insert_color(&client->node, &dev->user_clients);
1035 } else {
1036 p = &dev->kernel_clients.rb_node;
1037 while (*p) {
1038 parent = *p;
1039 entry = rb_entry(parent, struct ion_client, node);
1040
1041 if (client < entry)
1042 p = &(*p)->rb_left;
1043 else if (client > entry)
1044 p = &(*p)->rb_right;
1045 }
1046 rb_link_node(&client->node, parent, p);
1047 rb_insert_color(&client->node, &dev->kernel_clients);
1048 }
1049
Laura Abbotteed86032011-12-05 15:32:36 -08001050
1051 client->debug_root = debugfs_create_file(name, 0664,
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001052 dev->debug_root, client,
1053 &debug_client_fops);
1054 mutex_unlock(&dev->lock);
1055
1056 return client;
1057}
1058
Rebecca Schultz Zavin0b7e8ae2011-07-06 18:07:01 -07001059static void _ion_client_destroy(struct kref *kref)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001060{
Rebecca Schultz Zavin0b7e8ae2011-07-06 18:07:01 -07001061 struct ion_client *client = container_of(kref, struct ion_client, ref);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001062 struct ion_device *dev = client->dev;
1063 struct rb_node *n;
1064
1065 pr_debug("%s: %d\n", __func__, __LINE__);
1066 while ((n = rb_first(&client->handles))) {
1067 struct ion_handle *handle = rb_entry(n, struct ion_handle,
1068 node);
1069 ion_handle_destroy(&handle->ref);
1070 }
1071 mutex_lock(&dev->lock);
1072 if (client->task) {
1073 rb_erase(&client->node, &dev->user_clients);
1074 put_task_struct(client->task);
1075 } else {
1076 rb_erase(&client->node, &dev->kernel_clients);
1077 }
1078 debugfs_remove_recursive(client->debug_root);
1079 mutex_unlock(&dev->lock);
1080
1081 kfree(client);
1082}
1083
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001084static void ion_client_get(struct ion_client *client)
1085{
1086 kref_get(&client->ref);
1087}
1088
1089static int ion_client_put(struct ion_client *client)
1090{
1091 return kref_put(&client->ref, _ion_client_destroy);
1092}
1093
Rebecca Schultz Zavin0b7e8ae2011-07-06 18:07:01 -07001094void ion_client_destroy(struct ion_client *client)
1095{
Jordan Crousea75022c2011-10-12 16:57:47 -06001096 if (client)
1097 ion_client_put(client);
Rebecca Schultz Zavin0b7e8ae2011-07-06 18:07:01 -07001098}
1099
Laura Abbott273dd8e2011-10-12 14:26:33 -07001100int ion_handle_get_flags(struct ion_client *client, struct ion_handle *handle,
1101 unsigned long *flags)
1102{
1103 struct ion_buffer *buffer;
1104
1105 mutex_lock(&client->lock);
1106 if (!ion_handle_validate(client, handle)) {
1107 pr_err("%s: invalid handle passed to %s.\n",
1108 __func__, __func__);
1109 mutex_unlock(&client->lock);
1110 return -EINVAL;
1111 }
1112 buffer = handle->buffer;
1113 mutex_lock(&buffer->lock);
1114 *flags = buffer->flags;
1115 mutex_unlock(&buffer->lock);
1116 mutex_unlock(&client->lock);
1117
1118 return 0;
1119}
1120EXPORT_SYMBOL(ion_handle_get_flags);
1121
Laura Abbott8c017362011-09-22 20:59:12 -07001122int ion_handle_get_size(struct ion_client *client, struct ion_handle *handle,
1123 unsigned long *size)
1124{
1125 struct ion_buffer *buffer;
1126
1127 mutex_lock(&client->lock);
1128 if (!ion_handle_validate(client, handle)) {
1129 pr_err("%s: invalid handle passed to %s.\n",
1130 __func__, __func__);
1131 mutex_unlock(&client->lock);
1132 return -EINVAL;
1133 }
1134 buffer = handle->buffer;
1135 mutex_lock(&buffer->lock);
1136 *size = buffer->size;
1137 mutex_unlock(&buffer->lock);
1138 mutex_unlock(&client->lock);
1139
1140 return 0;
1141}
1142EXPORT_SYMBOL(ion_handle_get_size);
1143
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001144static int ion_share_release(struct inode *inode, struct file* file)
1145{
1146 struct ion_buffer *buffer = file->private_data;
1147
1148 pr_debug("%s: %d\n", __func__, __LINE__);
1149 /* drop the reference to the buffer -- this prevents the
1150 buffer from going away because the client holding it exited
1151 while it was being passed */
1152 ion_buffer_put(buffer);
1153 return 0;
1154}
1155
1156static void ion_vma_open(struct vm_area_struct *vma)
1157{
1158
1159 struct ion_buffer *buffer = vma->vm_file->private_data;
1160 struct ion_handle *handle = vma->vm_private_data;
1161 struct ion_client *client;
1162
1163 pr_debug("%s: %d\n", __func__, __LINE__);
1164 /* check that the client still exists and take a reference so
1165 it can't go away until this vma is closed */
1166 client = ion_client_lookup(buffer->dev, current->group_leader);
1167 if (IS_ERR_OR_NULL(client)) {
1168 vma->vm_private_data = NULL;
1169 return;
1170 }
Laura Abbott0f2175b2011-12-09 14:26:07 -08001171 ion_handle_get(handle);
Laura Abbott77168502011-12-05 11:06:24 -08001172 mutex_lock(&buffer->lock);
1173 buffer->umap_cnt++;
1174 mutex_unlock(&buffer->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001175 pr_debug("%s: %d client_cnt %d handle_cnt %d alloc_cnt %d\n",
1176 __func__, __LINE__,
1177 atomic_read(&client->ref.refcount),
1178 atomic_read(&handle->ref.refcount),
1179 atomic_read(&buffer->ref.refcount));
1180}
1181
1182static void ion_vma_close(struct vm_area_struct *vma)
1183{
1184 struct ion_handle *handle = vma->vm_private_data;
1185 struct ion_buffer *buffer = vma->vm_file->private_data;
1186 struct ion_client *client;
1187
1188 pr_debug("%s: %d\n", __func__, __LINE__);
1189 /* this indicates the client is gone, nothing to do here */
1190 if (!handle)
1191 return;
1192 client = handle->client;
Laura Abbott77168502011-12-05 11:06:24 -08001193 mutex_lock(&buffer->lock);
1194 buffer->umap_cnt--;
1195 mutex_unlock(&buffer->lock);
Laura Abbotta6835092011-11-14 15:27:02 -08001196
1197 if (buffer->heap->ops->unmap_user)
1198 buffer->heap->ops->unmap_user(buffer->heap, buffer);
1199
1200
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001201 pr_debug("%s: %d client_cnt %d handle_cnt %d alloc_cnt %d\n",
1202 __func__, __LINE__,
1203 atomic_read(&client->ref.refcount),
1204 atomic_read(&handle->ref.refcount),
1205 atomic_read(&buffer->ref.refcount));
1206 ion_handle_put(handle);
1207 ion_client_put(client);
1208 pr_debug("%s: %d client_cnt %d handle_cnt %d alloc_cnt %d\n",
1209 __func__, __LINE__,
1210 atomic_read(&client->ref.refcount),
1211 atomic_read(&handle->ref.refcount),
1212 atomic_read(&buffer->ref.refcount));
1213}
1214
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001215static struct vm_operations_struct ion_vm_ops = {
1216 .open = ion_vma_open,
1217 .close = ion_vma_close,
1218};
1219
1220static int ion_share_mmap(struct file *file, struct vm_area_struct *vma)
1221{
1222 struct ion_buffer *buffer = file->private_data;
1223 unsigned long size = vma->vm_end - vma->vm_start;
1224 struct ion_client *client;
1225 struct ion_handle *handle;
1226 int ret;
Laura Abbott894fd582011-08-19 13:33:56 -07001227 unsigned long flags = file->f_flags & O_DSYNC ?
1228 ION_SET_CACHE(UNCACHED) :
1229 ION_SET_CACHE(CACHED);
1230
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001231
1232 pr_debug("%s: %d\n", __func__, __LINE__);
1233 /* make sure the client still exists, it's possible for the client to
1234 have gone away but the map/share fd still to be around, take
1235 a reference to it so it can't go away while this mapping exists */
1236 client = ion_client_lookup(buffer->dev, current->group_leader);
1237 if (IS_ERR_OR_NULL(client)) {
1238 pr_err("%s: trying to mmap an ion handle in a process with no "
1239 "ion client\n", __func__);
1240 return -EINVAL;
1241 }
1242
1243 if ((size > buffer->size) || (size + (vma->vm_pgoff << PAGE_SHIFT) >
1244 buffer->size)) {
1245 pr_err("%s: trying to map larger area than handle has available"
1246 "\n", __func__);
1247 ret = -EINVAL;
1248 goto err;
1249 }
1250
1251 /* find the handle and take a reference to it */
1252 handle = ion_import(client, buffer);
1253 if (IS_ERR_OR_NULL(handle)) {
1254 ret = -EINVAL;
1255 goto err;
1256 }
1257
1258 if (!handle->buffer->heap->ops->map_user) {
1259 pr_err("%s: this heap does not define a method for mapping "
1260 "to userspace\n", __func__);
1261 ret = -EINVAL;
1262 goto err1;
1263 }
1264
1265 mutex_lock(&buffer->lock);
Laura Abbott894fd582011-08-19 13:33:56 -07001266
Laura Abbott8c017362011-09-22 20:59:12 -07001267 if (ion_validate_buffer_flags(buffer, flags)) {
1268 ret = -EEXIST;
1269 mutex_unlock(&buffer->lock);
1270 goto err1;
Laura Abbott894fd582011-08-19 13:33:56 -07001271 }
Laura Abbott8c017362011-09-22 20:59:12 -07001272
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001273 /* now map it to userspace */
Laura Abbott894fd582011-08-19 13:33:56 -07001274 ret = buffer->heap->ops->map_user(buffer->heap, buffer, vma,
1275 flags);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001276 mutex_unlock(&buffer->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001277 if (ret) {
1278 pr_err("%s: failure mapping buffer to userspace\n",
1279 __func__);
Laura Abbott894fd582011-08-19 13:33:56 -07001280 goto err2;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001281 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001282
1283 vma->vm_ops = &ion_vm_ops;
1284 /* move the handle into the vm_private_data so we can access it from
1285 vma_open/close */
1286 vma->vm_private_data = handle;
1287 pr_debug("%s: %d client_cnt %d handle_cnt %d alloc_cnt %d\n",
1288 __func__, __LINE__,
1289 atomic_read(&client->ref.refcount),
1290 atomic_read(&handle->ref.refcount),
1291 atomic_read(&buffer->ref.refcount));
1292 return 0;
1293
Laura Abbott894fd582011-08-19 13:33:56 -07001294err2:
1295 buffer->umap_cnt--;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001296 /* drop the reference to the handle */
Laura Abbott894fd582011-08-19 13:33:56 -07001297err1:
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001298 ion_handle_put(handle);
1299err:
Iliyan Malchev3fe24362011-08-09 14:42:08 -07001300 /* drop the reference to the client */
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001301 ion_client_put(client);
1302 return ret;
1303}
1304
1305static const struct file_operations ion_share_fops = {
1306 .owner = THIS_MODULE,
1307 .release = ion_share_release,
1308 .mmap = ion_share_mmap,
1309};
1310
1311static int ion_ioctl_share(struct file *parent, struct ion_client *client,
1312 struct ion_handle *handle)
1313{
1314 int fd = get_unused_fd();
1315 struct file *file;
1316
1317 if (fd < 0)
1318 return -ENFILE;
1319
1320 file = anon_inode_getfile("ion_share_fd", &ion_share_fops,
1321 handle->buffer, O_RDWR);
1322 if (IS_ERR_OR_NULL(file))
1323 goto err;
Laura Abbott4b5d0482011-09-27 18:35:14 -07001324
1325 if (parent->f_flags & O_DSYNC)
1326 file->f_flags |= O_DSYNC;
1327
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001328 ion_buffer_get(handle->buffer);
1329 fd_install(fd, file);
1330
1331 return fd;
1332
1333err:
1334 put_unused_fd(fd);
1335 return -ENFILE;
1336}
1337
1338static long ion_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1339{
1340 struct ion_client *client = filp->private_data;
1341
1342 switch (cmd) {
1343 case ION_IOC_ALLOC:
1344 {
1345 struct ion_allocation_data data;
1346
1347 if (copy_from_user(&data, (void __user *)arg, sizeof(data)))
1348 return -EFAULT;
1349 data.handle = ion_alloc(client, data.len, data.align,
1350 data.flags);
Laura Abbotte1b9ce52011-11-11 18:31:39 -08001351
1352 if (IS_ERR_OR_NULL(data.handle))
1353 return PTR_ERR(data.handle);
1354
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001355 if (copy_to_user((void __user *)arg, &data, sizeof(data)))
1356 return -EFAULT;
1357 break;
1358 }
1359 case ION_IOC_FREE:
1360 {
1361 struct ion_handle_data data;
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001362 bool valid;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001363
1364 if (copy_from_user(&data, (void __user *)arg,
1365 sizeof(struct ion_handle_data)))
1366 return -EFAULT;
1367 mutex_lock(&client->lock);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001368 valid = ion_handle_validate(client, data.handle);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001369 mutex_unlock(&client->lock);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001370 if (!valid)
1371 return -EINVAL;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001372 ion_free(client, data.handle);
1373 break;
1374 }
1375 case ION_IOC_MAP:
1376 case ION_IOC_SHARE:
1377 {
1378 struct ion_fd_data data;
1379
1380 if (copy_from_user(&data, (void __user *)arg, sizeof(data)))
1381 return -EFAULT;
1382 mutex_lock(&client->lock);
1383 if (!ion_handle_validate(client, data.handle)) {
1384 pr_err("%s: invalid handle passed to share ioctl.\n",
1385 __func__);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001386 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001387 return -EINVAL;
1388 }
1389 data.fd = ion_ioctl_share(filp, client, data.handle);
1390 mutex_unlock(&client->lock);
1391 if (copy_to_user((void __user *)arg, &data, sizeof(data)))
1392 return -EFAULT;
1393 break;
1394 }
1395 case ION_IOC_IMPORT:
1396 {
1397 struct ion_fd_data data;
1398 if (copy_from_user(&data, (void __user *)arg,
1399 sizeof(struct ion_fd_data)))
1400 return -EFAULT;
1401
1402 data.handle = ion_import_fd(client, data.fd);
1403 if (IS_ERR(data.handle))
1404 data.handle = NULL;
1405 if (copy_to_user((void __user *)arg, &data,
1406 sizeof(struct ion_fd_data)))
1407 return -EFAULT;
1408 break;
1409 }
1410 case ION_IOC_CUSTOM:
1411 {
1412 struct ion_device *dev = client->dev;
1413 struct ion_custom_data data;
1414
1415 if (!dev->custom_ioctl)
1416 return -ENOTTY;
1417 if (copy_from_user(&data, (void __user *)arg,
1418 sizeof(struct ion_custom_data)))
1419 return -EFAULT;
1420 return dev->custom_ioctl(client, data.cmd, data.arg);
1421 }
Laura Abbottabcb6f72011-10-04 16:26:49 -07001422 case ION_IOC_CLEAN_CACHES:
1423 case ION_IOC_INV_CACHES:
1424 case ION_IOC_CLEAN_INV_CACHES:
1425 {
1426 struct ion_flush_data data;
Laura Abbott9fa29e82011-11-14 09:42:53 -08001427 unsigned long start, end;
Laura Abbotte80ea012011-11-18 18:36:47 -08001428 struct ion_handle *handle = NULL;
1429 int ret;
Laura Abbottabcb6f72011-10-04 16:26:49 -07001430
1431 if (copy_from_user(&data, (void __user *)arg,
1432 sizeof(struct ion_flush_data)))
1433 return -EFAULT;
1434
Laura Abbott9fa29e82011-11-14 09:42:53 -08001435 start = (unsigned long) data.vaddr;
1436 end = (unsigned long) data.vaddr + data.length;
1437
1438 if (check_vaddr_bounds(start, end)) {
1439 pr_err("%s: virtual address %p is out of bounds\n",
1440 __func__, data.vaddr);
1441 return -EINVAL;
1442 }
1443
Laura Abbotte80ea012011-11-18 18:36:47 -08001444 if (!data.handle) {
1445 handle = ion_import_fd(client, data.fd);
1446 if (IS_ERR_OR_NULL(handle)) {
1447 pr_info("%s: Could not import handle: %d\n",
1448 __func__, (int)handle);
1449 return -EINVAL;
1450 }
1451 }
1452
1453 ret = ion_do_cache_op(client,
1454 data.handle ? data.handle : handle,
1455 data.vaddr, data.offset, data.length,
1456 cmd);
1457
1458 if (!data.handle)
1459 ion_free(client, handle);
1460
1461 break;
Laura Abbottabcb6f72011-10-04 16:26:49 -07001462
1463 }
Laura Abbott273dd8e2011-10-12 14:26:33 -07001464 case ION_IOC_GET_FLAGS:
1465 {
1466 struct ion_flag_data data;
1467 int ret;
1468 if (copy_from_user(&data, (void __user *)arg,
1469 sizeof(struct ion_flag_data)))
1470 return -EFAULT;
1471
1472 ret = ion_handle_get_flags(client, data.handle, &data.flags);
1473 if (ret < 0)
1474 return ret;
1475 if (copy_to_user((void __user *)arg, &data,
1476 sizeof(struct ion_flag_data)))
1477 return -EFAULT;
1478 break;
1479 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001480 default:
1481 return -ENOTTY;
1482 }
1483 return 0;
1484}
1485
1486static int ion_release(struct inode *inode, struct file *file)
1487{
1488 struct ion_client *client = file->private_data;
1489
1490 pr_debug("%s: %d\n", __func__, __LINE__);
1491 ion_client_put(client);
1492 return 0;
1493}
1494
1495static int ion_open(struct inode *inode, struct file *file)
1496{
1497 struct miscdevice *miscdev = file->private_data;
1498 struct ion_device *dev = container_of(miscdev, struct ion_device, dev);
1499 struct ion_client *client;
Laura Abbotteed86032011-12-05 15:32:36 -08001500 char debug_name[64];
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001501
1502 pr_debug("%s: %d\n", __func__, __LINE__);
Laura Abbotteed86032011-12-05 15:32:36 -08001503 snprintf(debug_name, 64, "%u", task_pid_nr(current->group_leader));
1504 client = ion_client_create(dev, -1, debug_name);
Rebecca Schultz Zavin6d3b9582011-07-06 18:07:24 -07001505 if (IS_ERR_OR_NULL(client))
1506 return PTR_ERR(client);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001507 file->private_data = client;
1508
1509 return 0;
1510}
1511
1512static const struct file_operations ion_fops = {
1513 .owner = THIS_MODULE,
1514 .open = ion_open,
1515 .release = ion_release,
1516 .unlocked_ioctl = ion_ioctl,
1517};
1518
1519static size_t ion_debug_heap_total(struct ion_client *client,
Laura Abbott3647ac32011-10-31 14:09:53 -07001520 enum ion_heap_ids id)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001521{
1522 size_t size = 0;
1523 struct rb_node *n;
1524
1525 mutex_lock(&client->lock);
1526 for (n = rb_first(&client->handles); n; n = rb_next(n)) {
1527 struct ion_handle *handle = rb_entry(n,
1528 struct ion_handle,
1529 node);
Laura Abbott3647ac32011-10-31 14:09:53 -07001530 if (handle->buffer->heap->id == id)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001531 size += handle->buffer->size;
1532 }
1533 mutex_unlock(&client->lock);
1534 return size;
1535}
1536
1537static int ion_debug_heap_show(struct seq_file *s, void *unused)
1538{
1539 struct ion_heap *heap = s->private;
1540 struct ion_device *dev = heap->dev;
1541 struct rb_node *n;
1542
1543 seq_printf(s, "%16.s %16.s %16.s\n", "client", "pid", "size");
1544 for (n = rb_first(&dev->user_clients); n; n = rb_next(n)) {
1545 struct ion_client *client = rb_entry(n, struct ion_client,
1546 node);
1547 char task_comm[TASK_COMM_LEN];
Laura Abbott3647ac32011-10-31 14:09:53 -07001548 size_t size = ion_debug_heap_total(client, heap->id);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001549 if (!size)
1550 continue;
1551
1552 get_task_comm(task_comm, client->task);
Laura Abbott8747bbe2011-10-31 14:18:13 -07001553 seq_printf(s, "%16.s %16u %16x\n", task_comm, client->pid,
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001554 size);
1555 }
1556
1557 for (n = rb_first(&dev->kernel_clients); n; n = rb_next(n)) {
1558 struct ion_client *client = rb_entry(n, struct ion_client,
1559 node);
Laura Abbott3647ac32011-10-31 14:09:53 -07001560 size_t size = ion_debug_heap_total(client, heap->id);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001561 if (!size)
1562 continue;
Laura Abbott8747bbe2011-10-31 14:18:13 -07001563 seq_printf(s, "%16.s %16u %16x\n", client->name, client->pid,
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001564 size);
1565 }
Laura Abbott68c80642011-10-21 17:32:27 -07001566 if (heap->ops->get_allocated) {
1567 seq_printf(s, "total bytes currently allocated: %lx\n",
1568 heap->ops->get_allocated(heap));
1569 }
1570 if (heap->ops->get_total) {
1571 seq_printf(s, "total heap size: %lx\n",
1572 heap->ops->get_total(heap));
1573 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001574 return 0;
1575}
1576
1577static int ion_debug_heap_open(struct inode *inode, struct file *file)
1578{
1579 return single_open(file, ion_debug_heap_show, inode->i_private);
1580}
1581
1582static const struct file_operations debug_heap_fops = {
1583 .open = ion_debug_heap_open,
1584 .read = seq_read,
1585 .llseek = seq_lseek,
1586 .release = single_release,
1587};
1588
1589void ion_device_add_heap(struct ion_device *dev, struct ion_heap *heap)
1590{
1591 struct rb_node **p = &dev->heaps.rb_node;
1592 struct rb_node *parent = NULL;
1593 struct ion_heap *entry;
1594
1595 heap->dev = dev;
1596 mutex_lock(&dev->lock);
1597 while (*p) {
1598 parent = *p;
1599 entry = rb_entry(parent, struct ion_heap, node);
1600
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001601 if (heap->id < entry->id) {
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001602 p = &(*p)->rb_left;
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001603 } else if (heap->id > entry->id ) {
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001604 p = &(*p)->rb_right;
1605 } else {
1606 pr_err("%s: can not insert multiple heaps with "
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001607 "id %d\n", __func__, heap->id);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001608 goto end;
1609 }
1610 }
1611
1612 rb_link_node(&heap->node, parent, p);
1613 rb_insert_color(&heap->node, &dev->heaps);
1614 debugfs_create_file(heap->name, 0664, dev->debug_root, heap,
1615 &debug_heap_fops);
1616end:
1617 mutex_unlock(&dev->lock);
1618}
1619
Laura Abbott404f8242011-10-31 14:22:53 -07001620static int ion_debug_leak_show(struct seq_file *s, void *unused)
1621{
1622 struct ion_device *dev = s->private;
1623 struct rb_node *n;
1624 struct rb_node *n2;
1625
1626 /* mark all buffers as 1 */
1627 seq_printf(s, "%16.s %16.s %16.s %16.s\n", "buffer", "heap", "size",
1628 "ref cnt");
1629 mutex_lock(&dev->lock);
1630 for (n = rb_first(&dev->buffers); n; n = rb_next(n)) {
1631 struct ion_buffer *buf = rb_entry(n, struct ion_buffer,
1632 node);
1633
1634 buf->marked = 1;
1635 }
1636
1637 /* now see which buffers we can access */
1638 for (n = rb_first(&dev->kernel_clients); n; n = rb_next(n)) {
1639 struct ion_client *client = rb_entry(n, struct ion_client,
1640 node);
1641
1642 mutex_lock(&client->lock);
1643 for (n2 = rb_first(&client->handles); n2; n2 = rb_next(n2)) {
1644 struct ion_handle *handle = rb_entry(n2,
1645 struct ion_handle, node);
1646
1647 handle->buffer->marked = 0;
1648
1649 }
1650 mutex_unlock(&client->lock);
1651
1652 }
1653
1654 for (n = rb_first(&dev->user_clients); n; n = rb_next(n)) {
1655 struct ion_client *client = rb_entry(n, struct ion_client,
1656 node);
1657
1658 mutex_lock(&client->lock);
1659 for (n2 = rb_first(&client->handles); n2; n2 = rb_next(n2)) {
1660 struct ion_handle *handle = rb_entry(n2,
1661 struct ion_handle, node);
1662
1663 handle->buffer->marked = 0;
1664
1665 }
1666 mutex_unlock(&client->lock);
1667
1668 }
1669 /* And anyone still marked as a 1 means a leaked handle somewhere */
1670 for (n = rb_first(&dev->buffers); n; n = rb_next(n)) {
1671 struct ion_buffer *buf = rb_entry(n, struct ion_buffer,
1672 node);
1673
1674 if (buf->marked == 1)
1675 seq_printf(s, "%16.x %16.s %16.x %16.d\n",
1676 (int)buf, buf->heap->name, buf->size,
1677 atomic_read(&buf->ref.refcount));
1678 }
1679 mutex_unlock(&dev->lock);
1680 return 0;
1681}
1682
1683static int ion_debug_leak_open(struct inode *inode, struct file *file)
1684{
1685 return single_open(file, ion_debug_leak_show, inode->i_private);
1686}
1687
1688static const struct file_operations debug_leak_fops = {
1689 .open = ion_debug_leak_open,
1690 .read = seq_read,
1691 .llseek = seq_lseek,
1692 .release = single_release,
1693};
1694
1695
1696
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001697struct ion_device *ion_device_create(long (*custom_ioctl)
1698 (struct ion_client *client,
1699 unsigned int cmd,
1700 unsigned long arg))
1701{
1702 struct ion_device *idev;
1703 int ret;
1704
1705 idev = kzalloc(sizeof(struct ion_device), GFP_KERNEL);
1706 if (!idev)
1707 return ERR_PTR(-ENOMEM);
1708
1709 idev->dev.minor = MISC_DYNAMIC_MINOR;
1710 idev->dev.name = "ion";
1711 idev->dev.fops = &ion_fops;
1712 idev->dev.parent = NULL;
1713 ret = misc_register(&idev->dev);
1714 if (ret) {
1715 pr_err("ion: failed to register misc device.\n");
1716 return ERR_PTR(ret);
1717 }
1718
1719 idev->debug_root = debugfs_create_dir("ion", NULL);
1720 if (IS_ERR_OR_NULL(idev->debug_root))
1721 pr_err("ion: failed to create debug files.\n");
1722
1723 idev->custom_ioctl = custom_ioctl;
1724 idev->buffers = RB_ROOT;
1725 mutex_init(&idev->lock);
1726 idev->heaps = RB_ROOT;
1727 idev->user_clients = RB_ROOT;
1728 idev->kernel_clients = RB_ROOT;
Laura Abbott404f8242011-10-31 14:22:53 -07001729 debugfs_create_file("check_leaked_fds", 0664, idev->debug_root, idev,
1730 &debug_leak_fops);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001731 return idev;
1732}
1733
1734void ion_device_destroy(struct ion_device *dev)
1735{
1736 misc_deregister(&dev->dev);
1737 /* XXX need to free the heaps and clients ? */
1738 kfree(dev);
1739}