blob: 5e325e9e21bcac219be948cd46af1f3d785eb4d8 [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.
Olav Haugan0a852512012-01-09 10:20:55 -08005 * Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved.
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07006 *
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17
18#include <linux/device.h>
19#include <linux/file.h>
20#include <linux/fs.h>
21#include <linux/anon_inodes.h>
22#include <linux/ion.h>
23#include <linux/list.h>
24#include <linux/miscdevice.h>
25#include <linux/mm.h>
26#include <linux/mm_types.h>
27#include <linux/rbtree.h>
28#include <linux/sched.h>
29#include <linux/slab.h>
30#include <linux/seq_file.h>
31#include <linux/uaccess.h>
32#include <linux/debugfs.h>
33
Laura Abbott8c017362011-09-22 20:59:12 -070034#include <mach/iommu_domains.h>
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070035#include "ion_priv.h"
36#define DEBUG
37
38/**
39 * struct ion_device - the metadata of the ion device node
40 * @dev: the actual misc device
41 * @buffers: an rb tree of all the existing buffers
42 * @lock: lock protecting the buffers & heaps trees
43 * @heaps: list of all the heaps in the system
44 * @user_clients: list of all the clients created from userspace
45 */
46struct ion_device {
47 struct miscdevice dev;
48 struct rb_root buffers;
49 struct mutex lock;
50 struct rb_root heaps;
51 long (*custom_ioctl) (struct ion_client *client, unsigned int cmd,
52 unsigned long arg);
53 struct rb_root user_clients;
54 struct rb_root kernel_clients;
55 struct dentry *debug_root;
56};
57
58/**
59 * struct ion_client - a process/hw block local address space
60 * @ref: for reference counting the client
61 * @node: node in the tree of all clients
62 * @dev: backpointer to ion device
63 * @handles: an rb tree of all the handles in this client
64 * @lock: lock protecting the tree of handles
65 * @heap_mask: mask of all supported heaps
66 * @name: used for debugging
67 * @task: used for debugging
68 *
69 * A client represents a list of buffers this client may access.
70 * The mutex stored here is used to protect both handles tree
71 * as well as the handles themselves, and should be held while modifying either.
72 */
73struct ion_client {
74 struct kref ref;
75 struct rb_node node;
76 struct ion_device *dev;
77 struct rb_root handles;
78 struct mutex lock;
79 unsigned int heap_mask;
Olav Haugan63e5f3b2012-01-11 16:42:37 -080080 char *name;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070081 struct task_struct *task;
82 pid_t pid;
83 struct dentry *debug_root;
84};
85
86/**
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -070087 * ion_handle - a client local reference to a buffer
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070088 * @ref: reference count
89 * @client: back pointer to the client the buffer resides in
90 * @buffer: pointer to the buffer
91 * @node: node in the client's handle rbtree
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -070092 * @kmap_cnt: count of times this client has mapped to kernel
93 * @dmap_cnt: count of times this client has mapped for dma
94 * @usermap_cnt: count of times this client has mapped for userspace
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070095 *
96 * Modifications to node, map_cnt or mapping should be protected by the
97 * lock in the client. Other fields are never changed after initialization.
98 */
99struct ion_handle {
100 struct kref ref;
101 struct ion_client *client;
102 struct ion_buffer *buffer;
103 struct rb_node node;
104 unsigned int kmap_cnt;
105 unsigned int dmap_cnt;
106 unsigned int usermap_cnt;
Laura Abbott8c017362011-09-22 20:59:12 -0700107 unsigned int iommu_map_cnt;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700108};
109
Laura Abbott8c017362011-09-22 20:59:12 -0700110static int ion_validate_buffer_flags(struct ion_buffer *buffer,
111 unsigned long flags)
112{
113 if (buffer->kmap_cnt || buffer->dmap_cnt || buffer->umap_cnt ||
114 buffer->iommu_map_cnt) {
115 if (buffer->flags != flags) {
116 pr_err("%s: buffer was already mapped with flags %lx,"
117 " cannot map with flags %lx\n", __func__,
118 buffer->flags, flags);
119 return 1;
120 }
121
122 } else {
123 buffer->flags = flags;
124 }
125 return 0;
126}
127
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700128/* this function should only be called while dev->lock is held */
129static void ion_buffer_add(struct ion_device *dev,
130 struct ion_buffer *buffer)
131{
132 struct rb_node **p = &dev->buffers.rb_node;
133 struct rb_node *parent = NULL;
134 struct ion_buffer *entry;
135
136 while (*p) {
137 parent = *p;
138 entry = rb_entry(parent, struct ion_buffer, node);
139
Rebecca Schultz Zavinf9fb95e2011-06-30 18:09:05 -0700140 if (buffer < entry) {
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700141 p = &(*p)->rb_left;
Rebecca Schultz Zavinf9fb95e2011-06-30 18:09:05 -0700142 } else if (buffer > entry) {
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700143 p = &(*p)->rb_right;
Rebecca Schultz Zavinf9fb95e2011-06-30 18:09:05 -0700144 } else {
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700145 pr_err("%s: buffer already found.", __func__);
146 BUG();
Rebecca Schultz Zavinf9fb95e2011-06-30 18:09:05 -0700147 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700148 }
149
150 rb_link_node(&buffer->node, parent, p);
151 rb_insert_color(&buffer->node, &dev->buffers);
152}
153
Laura Abbott8c017362011-09-22 20:59:12 -0700154void ion_iommu_add(struct ion_buffer *buffer,
155 struct ion_iommu_map *iommu)
156{
157 struct rb_node **p = &buffer->iommu_maps.rb_node;
158 struct rb_node *parent = NULL;
159 struct ion_iommu_map *entry;
160
161 while (*p) {
162 parent = *p;
163 entry = rb_entry(parent, struct ion_iommu_map, node);
164
165 if (iommu->key < entry->key) {
166 p = &(*p)->rb_left;
167 } else if (iommu->key > entry->key) {
168 p = &(*p)->rb_right;
169 } else {
170 pr_err("%s: buffer %p already has mapping for domain %d"
171 " and partition %d\n", __func__,
172 buffer,
173 iommu_map_domain(iommu),
174 iommu_map_partition(iommu));
175 BUG();
176 }
177 }
178
179 rb_link_node(&iommu->node, parent, p);
180 rb_insert_color(&iommu->node, &buffer->iommu_maps);
181
182}
183
184static struct ion_iommu_map *ion_iommu_lookup(struct ion_buffer *buffer,
185 unsigned int domain_no,
186 unsigned int partition_no)
187{
188 struct rb_node **p = &buffer->iommu_maps.rb_node;
189 struct rb_node *parent = NULL;
190 struct ion_iommu_map *entry;
191 uint64_t key = domain_no;
192 key = key << 32 | partition_no;
193
194 while (*p) {
195 parent = *p;
196 entry = rb_entry(parent, struct ion_iommu_map, node);
197
198 if (key < entry->key)
199 p = &(*p)->rb_left;
200 else if (key > entry->key)
201 p = &(*p)->rb_right;
202 else
203 return entry;
204 }
205
206 return NULL;
207}
208
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700209/* this function should only be called while dev->lock is held */
Iliyan Malchev3fe24362011-08-09 14:42:08 -0700210static struct ion_buffer *ion_buffer_create(struct ion_heap *heap,
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700211 struct ion_device *dev,
212 unsigned long len,
213 unsigned long align,
214 unsigned long flags)
215{
216 struct ion_buffer *buffer;
217 int ret;
218
219 buffer = kzalloc(sizeof(struct ion_buffer), GFP_KERNEL);
220 if (!buffer)
221 return ERR_PTR(-ENOMEM);
222
223 buffer->heap = heap;
224 kref_init(&buffer->ref);
225
226 ret = heap->ops->allocate(heap, buffer, len, align, flags);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700227 if (ret) {
228 kfree(buffer);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700229 return ERR_PTR(ret);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700230 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700231 buffer->dev = dev;
232 buffer->size = len;
233 mutex_init(&buffer->lock);
234 ion_buffer_add(dev, buffer);
235 return buffer;
236}
237
238static void ion_buffer_destroy(struct kref *kref)
239{
240 struct ion_buffer *buffer = container_of(kref, struct ion_buffer, ref);
241 struct ion_device *dev = buffer->dev;
242
243 buffer->heap->ops->free(buffer);
244 mutex_lock(&dev->lock);
245 rb_erase(&buffer->node, &dev->buffers);
246 mutex_unlock(&dev->lock);
247 kfree(buffer);
248}
249
250static void ion_buffer_get(struct ion_buffer *buffer)
251{
252 kref_get(&buffer->ref);
253}
254
255static int ion_buffer_put(struct ion_buffer *buffer)
256{
257 return kref_put(&buffer->ref, ion_buffer_destroy);
258}
259
Iliyan Malchev3fe24362011-08-09 14:42:08 -0700260static struct ion_handle *ion_handle_create(struct ion_client *client,
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700261 struct ion_buffer *buffer)
262{
263 struct ion_handle *handle;
264
265 handle = kzalloc(sizeof(struct ion_handle), GFP_KERNEL);
266 if (!handle)
267 return ERR_PTR(-ENOMEM);
268 kref_init(&handle->ref);
Iliyan Malchev3fe24362011-08-09 14:42:08 -0700269 rb_init_node(&handle->node);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700270 handle->client = client;
271 ion_buffer_get(buffer);
272 handle->buffer = buffer;
273
274 return handle;
275}
276
277static void ion_handle_destroy(struct kref *kref)
278{
279 struct ion_handle *handle = container_of(kref, struct ion_handle, ref);
280 /* XXX Can a handle be destroyed while it's map count is non-zero?:
281 if (handle->map_cnt) unmap
282 */
Laura Abbottd2a87372011-10-20 17:53:49 -0700283 WARN_ON(handle->kmap_cnt || handle->dmap_cnt || handle->usermap_cnt);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700284 ion_buffer_put(handle->buffer);
285 mutex_lock(&handle->client->lock);
Iliyan Malchev3fe24362011-08-09 14:42:08 -0700286 if (!RB_EMPTY_NODE(&handle->node))
287 rb_erase(&handle->node, &handle->client->handles);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700288 mutex_unlock(&handle->client->lock);
289 kfree(handle);
290}
291
292struct ion_buffer *ion_handle_buffer(struct ion_handle *handle)
293{
294 return handle->buffer;
295}
296
297static void ion_handle_get(struct ion_handle *handle)
298{
299 kref_get(&handle->ref);
300}
301
302static int ion_handle_put(struct ion_handle *handle)
303{
304 return kref_put(&handle->ref, ion_handle_destroy);
305}
306
307static struct ion_handle *ion_handle_lookup(struct ion_client *client,
308 struct ion_buffer *buffer)
309{
310 struct rb_node *n;
311
312 for (n = rb_first(&client->handles); n; n = rb_next(n)) {
313 struct ion_handle *handle = rb_entry(n, struct ion_handle,
314 node);
315 if (handle->buffer == buffer)
316 return handle;
317 }
318 return NULL;
319}
320
Iliyan Malchev3fe24362011-08-09 14:42:08 -0700321static bool ion_handle_validate(struct ion_client *client, struct ion_handle *handle)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700322{
323 struct rb_node *n = client->handles.rb_node;
324
325 while (n) {
326 struct ion_handle *handle_node = rb_entry(n, struct ion_handle,
327 node);
328 if (handle < handle_node)
329 n = n->rb_left;
330 else if (handle > handle_node)
331 n = n->rb_right;
332 else
333 return true;
334 }
335 return false;
336}
337
338static void ion_handle_add(struct ion_client *client, struct ion_handle *handle)
339{
340 struct rb_node **p = &client->handles.rb_node;
341 struct rb_node *parent = NULL;
342 struct ion_handle *entry;
343
344 while (*p) {
345 parent = *p;
346 entry = rb_entry(parent, struct ion_handle, node);
347
348 if (handle < entry)
349 p = &(*p)->rb_left;
350 else if (handle > entry)
351 p = &(*p)->rb_right;
352 else
353 WARN(1, "%s: buffer already found.", __func__);
354 }
355
356 rb_link_node(&handle->node, parent, p);
357 rb_insert_color(&handle->node, &client->handles);
358}
359
360struct ion_handle *ion_alloc(struct ion_client *client, size_t len,
361 size_t align, unsigned int flags)
362{
363 struct rb_node *n;
364 struct ion_handle *handle;
365 struct ion_device *dev = client->dev;
366 struct ion_buffer *buffer = NULL;
Olav Haugan0a852512012-01-09 10:20:55 -0800367 unsigned long secure_allocation = flags & ION_SECURE;
Olav Haugan35e2f2f2012-01-11 17:31:47 -0800368 const unsigned int MAX_DBG_STR_LEN = 64;
369 char dbg_str[MAX_DBG_STR_LEN];
370 unsigned int dbg_str_idx = 0;
371
372 dbg_str[0] = '\0';
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700373
374 /*
375 * traverse the list of heaps available in this system in priority
376 * order. If the heap type is supported by the client, and matches the
377 * request of the caller allocate from it. Repeat until allocate has
378 * succeeded or all heaps have been tried
379 */
380 mutex_lock(&dev->lock);
381 for (n = rb_first(&dev->heaps); n != NULL; n = rb_next(n)) {
382 struct ion_heap *heap = rb_entry(n, struct ion_heap, node);
383 /* if the client doesn't support this heap type */
384 if (!((1 << heap->type) & client->heap_mask))
385 continue;
386 /* if the caller didn't specify this heap type */
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700387 if (!((1 << heap->id) & flags))
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700388 continue;
Olav Haugan0a852512012-01-09 10:20:55 -0800389 /* Do not allow un-secure heap if secure is specified */
390 if (secure_allocation && (heap->type != ION_HEAP_TYPE_CP))
391 continue;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700392 buffer = ion_buffer_create(heap, dev, len, align, flags);
393 if (!IS_ERR_OR_NULL(buffer))
394 break;
Olav Haugan35e2f2f2012-01-11 17:31:47 -0800395 if (dbg_str_idx < MAX_DBG_STR_LEN) {
396 unsigned int len_left = MAX_DBG_STR_LEN-dbg_str_idx-1;
397 int ret_value = snprintf(&dbg_str[dbg_str_idx],
398 len_left, "%s ", heap->name);
399 if (ret_value >= len_left) {
400 /* overflow */
401 dbg_str[MAX_DBG_STR_LEN-1] = '\0';
402 dbg_str_idx = MAX_DBG_STR_LEN;
403 } else if (ret_value >= 0) {
404 dbg_str_idx += ret_value;
405 } else {
406 /* error */
407 dbg_str[MAX_DBG_STR_LEN-1] = '\0';
408 }
409 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700410 }
411 mutex_unlock(&dev->lock);
412
Olav Haugan35e2f2f2012-01-11 17:31:47 -0800413 if (IS_ERR_OR_NULL(buffer)) {
414 pr_debug("ION is unable to allocate 0x%x bytes (alignment: "
415 "0x%x) from heap(s) %sfor client %s with heap "
416 "mask 0x%x\n",
417 len, align, dbg_str, client->name, client->heap_mask);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700418 return ERR_PTR(PTR_ERR(buffer));
Olav Haugan35e2f2f2012-01-11 17:31:47 -0800419 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700420
421 handle = ion_handle_create(client, buffer);
422
423 if (IS_ERR_OR_NULL(handle))
424 goto end;
425
426 /*
427 * ion_buffer_create will create a buffer with a ref_cnt of 1,
428 * and ion_handle_create will take a second reference, drop one here
429 */
430 ion_buffer_put(buffer);
431
432 mutex_lock(&client->lock);
433 ion_handle_add(client, handle);
434 mutex_unlock(&client->lock);
435 return handle;
436
437end:
438 ion_buffer_put(buffer);
439 return handle;
440}
441
442void ion_free(struct ion_client *client, struct ion_handle *handle)
443{
Rebecca Schultz Zavinc72866d2011-07-07 17:07:56 -0700444 bool valid_handle;
445
446 BUG_ON(client != handle->client);
447
448 mutex_lock(&client->lock);
449 valid_handle = ion_handle_validate(client, handle);
450 mutex_unlock(&client->lock);
451
452 if (!valid_handle) {
453 WARN("%s: invalid handle passed to free.\n", __func__);
454 return;
455 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700456 ion_handle_put(handle);
457}
458
459static void ion_client_get(struct ion_client *client);
460static int ion_client_put(struct ion_client *client);
461
Iliyan Malchev3fe24362011-08-09 14:42:08 -0700462static bool _ion_map(int *buffer_cnt, int *handle_cnt)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700463{
464 bool map;
465
466 BUG_ON(*handle_cnt != 0 && *buffer_cnt == 0);
467
468 if (*buffer_cnt)
469 map = false;
470 else
471 map = true;
472 if (*handle_cnt == 0)
473 (*buffer_cnt)++;
474 (*handle_cnt)++;
475 return map;
476}
477
Iliyan Malchev3fe24362011-08-09 14:42:08 -0700478static bool _ion_unmap(int *buffer_cnt, int *handle_cnt)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700479{
480 BUG_ON(*handle_cnt == 0);
481 (*handle_cnt)--;
482 if (*handle_cnt != 0)
483 return false;
484 BUG_ON(*buffer_cnt == 0);
485 (*buffer_cnt)--;
486 if (*buffer_cnt == 0)
487 return true;
488 return false;
489}
490
491int ion_phys(struct ion_client *client, struct ion_handle *handle,
492 ion_phys_addr_t *addr, size_t *len)
493{
494 struct ion_buffer *buffer;
495 int ret;
496
497 mutex_lock(&client->lock);
498 if (!ion_handle_validate(client, handle)) {
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700499 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700500 return -EINVAL;
501 }
502
503 buffer = handle->buffer;
504
505 if (!buffer->heap->ops->phys) {
506 pr_err("%s: ion_phys is not implemented by this heap.\n",
507 __func__);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700508 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700509 return -ENODEV;
510 }
511 mutex_unlock(&client->lock);
512 ret = buffer->heap->ops->phys(buffer->heap, buffer, addr, len);
513 return ret;
514}
515
Laura Abbott894fd582011-08-19 13:33:56 -0700516void *ion_map_kernel(struct ion_client *client, struct ion_handle *handle,
517 unsigned long flags)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700518{
519 struct ion_buffer *buffer;
520 void *vaddr;
521
522 mutex_lock(&client->lock);
523 if (!ion_handle_validate(client, handle)) {
524 pr_err("%s: invalid handle passed to map_kernel.\n",
525 __func__);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700526 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700527 return ERR_PTR(-EINVAL);
528 }
529
530 buffer = handle->buffer;
531 mutex_lock(&buffer->lock);
532
533 if (!handle->buffer->heap->ops->map_kernel) {
534 pr_err("%s: map_kernel is not implemented by this heap.\n",
535 __func__);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700536 mutex_unlock(&buffer->lock);
537 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700538 return ERR_PTR(-ENODEV);
539 }
540
Laura Abbott8c017362011-09-22 20:59:12 -0700541 if (ion_validate_buffer_flags(buffer, flags)) {
Laura Abbott894fd582011-08-19 13:33:56 -0700542 vaddr = ERR_PTR(-EEXIST);
543 goto out;
Laura Abbott894fd582011-08-19 13:33:56 -0700544 }
545
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700546 if (_ion_map(&buffer->kmap_cnt, &handle->kmap_cnt)) {
Laura Abbott894fd582011-08-19 13:33:56 -0700547 vaddr = buffer->heap->ops->map_kernel(buffer->heap, buffer,
548 flags);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700549 if (IS_ERR_OR_NULL(vaddr))
550 _ion_unmap(&buffer->kmap_cnt, &handle->kmap_cnt);
551 buffer->vaddr = vaddr;
552 } else {
553 vaddr = buffer->vaddr;
554 }
Laura Abbott894fd582011-08-19 13:33:56 -0700555
556out:
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700557 mutex_unlock(&buffer->lock);
558 mutex_unlock(&client->lock);
559 return vaddr;
560}
561
Laura Abbott8c017362011-09-22 20:59:12 -0700562int __ion_iommu_map(struct ion_buffer *buffer,
563 int domain_num, int partition_num, unsigned long align,
564 unsigned long iova_length, unsigned long flags,
565 unsigned long *iova)
566{
567 struct ion_iommu_map *data;
568 int ret;
569
570 data = kmalloc(sizeof(*data), GFP_ATOMIC);
571
572 if (!data)
573 return -ENOMEM;
574
575 data->buffer = buffer;
576 iommu_map_domain(data) = domain_num;
577 iommu_map_partition(data) = partition_num;
578
579 ret = buffer->heap->ops->map_iommu(buffer, data,
580 domain_num,
581 partition_num,
582 align,
583 iova_length,
584 flags);
585
586 if (ret)
587 goto out;
588
589 kref_init(&data->ref);
590 *iova = data->iova_addr;
591
592 ion_iommu_add(buffer, data);
593
594 return 0;
595
596out:
597 msm_free_iova_address(data->iova_addr, domain_num, partition_num,
598 buffer->size);
599 kfree(data);
600 return ret;
601}
602
603int ion_map_iommu(struct ion_client *client, struct ion_handle *handle,
604 int domain_num, int partition_num, unsigned long align,
605 unsigned long iova_length, unsigned long *iova,
606 unsigned long *buffer_size,
607 unsigned long flags)
608{
609 struct ion_buffer *buffer;
610 struct ion_iommu_map *iommu_map;
611 int ret = 0;
612
613 mutex_lock(&client->lock);
614 if (!ion_handle_validate(client, handle)) {
615 pr_err("%s: invalid handle passed to map_kernel.\n",
616 __func__);
617 mutex_unlock(&client->lock);
618 return -EINVAL;
619 }
620
621 buffer = handle->buffer;
622 mutex_lock(&buffer->lock);
623
624 if (!handle->buffer->heap->ops->map_iommu) {
625 pr_err("%s: map_iommu is not implemented by this heap.\n",
626 __func__);
627 ret = -ENODEV;
628 goto out;
629 }
630
631 if (ion_validate_buffer_flags(buffer, flags)) {
632 ret = -EEXIST;
633 goto out;
634 }
635
636 /*
637 * If clients don't want a custom iova length, just use whatever
638 * the buffer size is
639 */
640 if (!iova_length)
641 iova_length = buffer->size;
642
643 if (buffer->size > iova_length) {
644 pr_debug("%s: iova length %lx is not at least buffer size"
645 " %x\n", __func__, iova_length, buffer->size);
646 ret = -EINVAL;
647 goto out;
648 }
649
650 if (buffer->size & ~PAGE_MASK) {
651 pr_debug("%s: buffer size %x is not aligned to %lx", __func__,
652 buffer->size, PAGE_SIZE);
653 ret = -EINVAL;
654 goto out;
655 }
656
657 if (iova_length & ~PAGE_MASK) {
658 pr_debug("%s: iova_length %lx is not aligned to %lx", __func__,
659 iova_length, PAGE_SIZE);
660 ret = -EINVAL;
661 goto out;
662 }
663
664 iommu_map = ion_iommu_lookup(buffer, domain_num, partition_num);
665 if (_ion_map(&buffer->iommu_map_cnt, &handle->iommu_map_cnt) ||
666 !iommu_map) {
667 ret = __ion_iommu_map(buffer, domain_num, partition_num, align,
668 iova_length, flags, iova);
669 if (ret < 0)
670 _ion_unmap(&buffer->iommu_map_cnt,
671 &handle->iommu_map_cnt);
672 } else {
673 if (iommu_map->mapped_size != iova_length) {
674 pr_err("%s: handle %p is already mapped with length"
675 " %x, trying to map with length %lx\n",
676 __func__, handle, iommu_map->mapped_size,
677 iova_length);
678 _ion_unmap(&buffer->iommu_map_cnt,
679 &handle->iommu_map_cnt);
680 ret = -EINVAL;
681 } else {
682 kref_get(&iommu_map->ref);
683 *iova = iommu_map->iova_addr;
684 }
685 }
686 *buffer_size = buffer->size;
687out:
688 mutex_unlock(&buffer->lock);
689 mutex_unlock(&client->lock);
690 return ret;
691}
692EXPORT_SYMBOL(ion_map_iommu);
693
694static void ion_iommu_release(struct kref *kref)
695{
696 struct ion_iommu_map *map = container_of(kref, struct ion_iommu_map,
697 ref);
698 struct ion_buffer *buffer = map->buffer;
699
700 rb_erase(&map->node, &buffer->iommu_maps);
701 buffer->heap->ops->unmap_iommu(map);
702 kfree(map);
703}
704
705void ion_unmap_iommu(struct ion_client *client, struct ion_handle *handle,
706 int domain_num, int partition_num)
707{
708 struct ion_iommu_map *iommu_map;
709 struct ion_buffer *buffer;
710
711 mutex_lock(&client->lock);
712 buffer = handle->buffer;
713
714 mutex_lock(&buffer->lock);
715
716 iommu_map = ion_iommu_lookup(buffer, domain_num, partition_num);
717
718 if (!iommu_map) {
719 WARN(1, "%s: (%d,%d) was never mapped for %p\n", __func__,
720 domain_num, partition_num, buffer);
721 goto out;
722 }
723
724 _ion_unmap(&buffer->iommu_map_cnt, &handle->iommu_map_cnt);
725 kref_put(&iommu_map->ref, ion_iommu_release);
726
727out:
728 mutex_unlock(&buffer->lock);
729
730 mutex_unlock(&client->lock);
731
732}
733EXPORT_SYMBOL(ion_unmap_iommu);
734
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700735struct scatterlist *ion_map_dma(struct ion_client *client,
Laura Abbott894fd582011-08-19 13:33:56 -0700736 struct ion_handle *handle,
737 unsigned long flags)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700738{
739 struct ion_buffer *buffer;
740 struct scatterlist *sglist;
741
742 mutex_lock(&client->lock);
743 if (!ion_handle_validate(client, handle)) {
744 pr_err("%s: invalid handle passed to map_dma.\n",
745 __func__);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700746 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700747 return ERR_PTR(-EINVAL);
748 }
749 buffer = handle->buffer;
750 mutex_lock(&buffer->lock);
751
752 if (!handle->buffer->heap->ops->map_dma) {
753 pr_err("%s: map_kernel is not implemented by this heap.\n",
754 __func__);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700755 mutex_unlock(&buffer->lock);
756 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700757 return ERR_PTR(-ENODEV);
758 }
Laura Abbott894fd582011-08-19 13:33:56 -0700759
Laura Abbott8c017362011-09-22 20:59:12 -0700760 if (ion_validate_buffer_flags(buffer, flags)) {
761 sglist = ERR_PTR(-EEXIST);
762 goto out;
Laura Abbott894fd582011-08-19 13:33:56 -0700763 }
764
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700765 if (_ion_map(&buffer->dmap_cnt, &handle->dmap_cnt)) {
766 sglist = buffer->heap->ops->map_dma(buffer->heap, buffer);
767 if (IS_ERR_OR_NULL(sglist))
768 _ion_unmap(&buffer->dmap_cnt, &handle->dmap_cnt);
769 buffer->sglist = sglist;
770 } else {
771 sglist = buffer->sglist;
772 }
Laura Abbott894fd582011-08-19 13:33:56 -0700773
774out:
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700775 mutex_unlock(&buffer->lock);
776 mutex_unlock(&client->lock);
777 return sglist;
778}
779
780void ion_unmap_kernel(struct ion_client *client, struct ion_handle *handle)
781{
782 struct ion_buffer *buffer;
783
784 mutex_lock(&client->lock);
785 buffer = handle->buffer;
786 mutex_lock(&buffer->lock);
787 if (_ion_unmap(&buffer->kmap_cnt, &handle->kmap_cnt)) {
788 buffer->heap->ops->unmap_kernel(buffer->heap, buffer);
789 buffer->vaddr = NULL;
790 }
791 mutex_unlock(&buffer->lock);
792 mutex_unlock(&client->lock);
793}
794
795void ion_unmap_dma(struct ion_client *client, struct ion_handle *handle)
796{
797 struct ion_buffer *buffer;
798
799 mutex_lock(&client->lock);
800 buffer = handle->buffer;
801 mutex_lock(&buffer->lock);
802 if (_ion_unmap(&buffer->dmap_cnt, &handle->dmap_cnt)) {
803 buffer->heap->ops->unmap_dma(buffer->heap, buffer);
804 buffer->sglist = NULL;
805 }
806 mutex_unlock(&buffer->lock);
807 mutex_unlock(&client->lock);
808}
809
810
811struct ion_buffer *ion_share(struct ion_client *client,
812 struct ion_handle *handle)
813{
Rebecca Schultz Zavinc72866d2011-07-07 17:07:56 -0700814 bool valid_handle;
815
816 mutex_lock(&client->lock);
817 valid_handle = ion_handle_validate(client, handle);
818 mutex_unlock(&client->lock);
819 if (!valid_handle) {
820 WARN("%s: invalid handle passed to share.\n", __func__);
821 return ERR_PTR(-EINVAL);
822 }
823
Iliyan Malchev3fe24362011-08-09 14:42:08 -0700824 /* do not take an extra reference here, the burden is on the caller
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700825 * to make sure the buffer doesn't go away while it's passing it
826 * to another client -- ion_free should not be called on this handle
827 * until the buffer has been imported into the other client
828 */
829 return handle->buffer;
830}
831
832struct ion_handle *ion_import(struct ion_client *client,
833 struct ion_buffer *buffer)
834{
835 struct ion_handle *handle = NULL;
836
837 mutex_lock(&client->lock);
838 /* if a handle exists for this buffer just take a reference to it */
839 handle = ion_handle_lookup(client, buffer);
840 if (!IS_ERR_OR_NULL(handle)) {
841 ion_handle_get(handle);
842 goto end;
843 }
844 handle = ion_handle_create(client, buffer);
845 if (IS_ERR_OR_NULL(handle))
846 goto end;
847 ion_handle_add(client, handle);
848end:
849 mutex_unlock(&client->lock);
850 return handle;
851}
852
Laura Abbottabcb6f72011-10-04 16:26:49 -0700853static int check_vaddr_bounds(unsigned long start, unsigned long end)
854{
855 struct mm_struct *mm = current->active_mm;
856 struct vm_area_struct *vma;
857 int ret = 1;
858
859 if (end < start)
860 goto out;
861
862 down_read(&mm->mmap_sem);
863 vma = find_vma(mm, start);
864 if (vma && vma->vm_start < end) {
865 if (start < vma->vm_start)
866 goto out_up;
867 if (end > vma->vm_end)
868 goto out_up;
869 ret = 0;
870 }
871
872out_up:
873 up_read(&mm->mmap_sem);
874out:
875 return ret;
876}
877
878int ion_do_cache_op(struct ion_client *client, struct ion_handle *handle,
879 void *uaddr, unsigned long offset, unsigned long len,
880 unsigned int cmd)
881{
882 struct ion_buffer *buffer;
Laura Abbottabcb6f72011-10-04 16:26:49 -0700883 int ret = -EINVAL;
884
885 mutex_lock(&client->lock);
886 if (!ion_handle_validate(client, handle)) {
887 pr_err("%s: invalid handle passed to do_cache_op.\n",
888 __func__);
889 mutex_unlock(&client->lock);
890 return -EINVAL;
891 }
892 buffer = handle->buffer;
893 mutex_lock(&buffer->lock);
894
Laura Abbottcbaa6682011-10-19 12:14:14 -0700895 if (!ION_IS_CACHED(buffer->flags)) {
Laura Abbottabcb6f72011-10-04 16:26:49 -0700896 ret = 0;
897 goto out;
898 }
899
900 if (!handle->buffer->heap->ops->cache_op) {
901 pr_err("%s: cache_op is not implemented by this heap.\n",
902 __func__);
903 ret = -ENODEV;
904 goto out;
905 }
906
Laura Abbottabcb6f72011-10-04 16:26:49 -0700907
908 ret = buffer->heap->ops->cache_op(buffer->heap, buffer, uaddr,
909 offset, len, cmd);
910
911out:
912 mutex_unlock(&buffer->lock);
913 mutex_unlock(&client->lock);
914 return ret;
915
916}
917
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700918static const struct file_operations ion_share_fops;
919
920struct ion_handle *ion_import_fd(struct ion_client *client, int fd)
921{
922 struct file *file = fget(fd);
923 struct ion_handle *handle;
924
925 if (!file) {
926 pr_err("%s: imported fd not found in file table.\n", __func__);
927 return ERR_PTR(-EINVAL);
928 }
929 if (file->f_op != &ion_share_fops) {
Laura Abbott084d6eb2011-10-24 19:09:50 -0700930 pr_err("%s: imported file %s is not a shared ion"
931 " file.", __func__, file->f_dentry->d_name.name);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700932 handle = ERR_PTR(-EINVAL);
933 goto end;
934 }
935 handle = ion_import(client, file->private_data);
936end:
937 fput(file);
938 return handle;
939}
940
941static int ion_debug_client_show(struct seq_file *s, void *unused)
942{
943 struct ion_client *client = s->private;
944 struct rb_node *n;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700945
Laura Abbott68c80642011-10-21 17:32:27 -0700946 seq_printf(s, "%16.16s: %16.16s : %16.16s : %16.16s\n", "heap_name",
947 "size_in_bytes", "handle refcount", "buffer");
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700948 mutex_lock(&client->lock);
949 for (n = rb_first(&client->handles); n; n = rb_next(n)) {
950 struct ion_handle *handle = rb_entry(n, struct ion_handle,
951 node);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700952
Laura Abbott8747bbe2011-10-31 14:18:13 -0700953 seq_printf(s, "%16.16s: %16x : %16d : %16p\n",
Laura Abbott68c80642011-10-21 17:32:27 -0700954 handle->buffer->heap->name,
955 handle->buffer->size,
956 atomic_read(&handle->ref.refcount),
957 handle->buffer);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700958 }
Laura Abbott68c80642011-10-21 17:32:27 -0700959
960 seq_printf(s, "%16.16s %d\n", "client refcount:",
961 atomic_read(&client->ref.refcount));
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700962 mutex_unlock(&client->lock);
963
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700964 return 0;
965}
966
967static int ion_debug_client_open(struct inode *inode, struct file *file)
968{
969 return single_open(file, ion_debug_client_show, inode->i_private);
970}
971
972static const struct file_operations debug_client_fops = {
973 .open = ion_debug_client_open,
974 .read = seq_read,
975 .llseek = seq_lseek,
976 .release = single_release,
977};
978
Rebecca Schultz Zavin83e3dab2011-07-01 20:41:25 -0700979static struct ion_client *ion_client_lookup(struct ion_device *dev,
980 struct task_struct *task)
981{
982 struct rb_node *n = dev->user_clients.rb_node;
983 struct ion_client *client;
984
985 mutex_lock(&dev->lock);
986 while (n) {
987 client = rb_entry(n, struct ion_client, node);
988 if (task == client->task) {
989 ion_client_get(client);
990 mutex_unlock(&dev->lock);
991 return client;
992 } else if (task < client->task) {
993 n = n->rb_left;
994 } else if (task > client->task) {
995 n = n->rb_right;
996 }
997 }
998 mutex_unlock(&dev->lock);
999 return NULL;
1000}
1001
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001002struct ion_client *ion_client_create(struct ion_device *dev,
1003 unsigned int heap_mask,
1004 const char *name)
1005{
1006 struct ion_client *client;
1007 struct task_struct *task;
1008 struct rb_node **p;
1009 struct rb_node *parent = NULL;
1010 struct ion_client *entry;
Rebecca Schultz Zavin83e3dab2011-07-01 20:41:25 -07001011 pid_t pid;
Olav Haugan63e5f3b2012-01-11 16:42:37 -08001012 unsigned int name_len = strnlen(name, 64);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001013
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001014 get_task_struct(current->group_leader);
1015 task_lock(current->group_leader);
Rebecca Schultz Zavin83e3dab2011-07-01 20:41:25 -07001016 pid = task_pid_nr(current->group_leader);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001017 /* don't bother to store task struct for kernel threads,
1018 they can't be killed anyway */
1019 if (current->group_leader->flags & PF_KTHREAD) {
1020 put_task_struct(current->group_leader);
1021 task = NULL;
1022 } else {
1023 task = current->group_leader;
1024 }
1025 task_unlock(current->group_leader);
Rebecca Schultz Zavin83e3dab2011-07-01 20:41:25 -07001026
1027 /* if this isn't a kernel thread, see if a client already
1028 exists */
1029 if (task) {
1030 client = ion_client_lookup(dev, task);
1031 if (!IS_ERR_OR_NULL(client)) {
1032 put_task_struct(current->group_leader);
1033 return client;
1034 }
1035 }
1036
1037 client = kzalloc(sizeof(struct ion_client), GFP_KERNEL);
1038 if (!client) {
1039 put_task_struct(current->group_leader);
1040 return ERR_PTR(-ENOMEM);
1041 }
1042
1043 client->dev = dev;
1044 client->handles = RB_ROOT;
1045 mutex_init(&client->lock);
Olav Haugan63e5f3b2012-01-11 16:42:37 -08001046
Olav Haugan6625c7d12012-01-24 13:50:43 -08001047 client->name = kzalloc(name_len+1, GFP_KERNEL);
Olav Haugan63e5f3b2012-01-11 16:42:37 -08001048 if (!client->name) {
1049 put_task_struct(current->group_leader);
1050 kfree(client);
1051 return ERR_PTR(-ENOMEM);
1052 } else {
Olav Haugan6625c7d12012-01-24 13:50:43 -08001053 strlcpy(client->name, name, name_len+1);
Olav Haugan63e5f3b2012-01-11 16:42:37 -08001054 }
1055
Rebecca Schultz Zavin83e3dab2011-07-01 20:41:25 -07001056 client->heap_mask = heap_mask;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001057 client->task = task;
Rebecca Schultz Zavin83e3dab2011-07-01 20:41:25 -07001058 client->pid = pid;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001059 kref_init(&client->ref);
1060
1061 mutex_lock(&dev->lock);
1062 if (task) {
1063 p = &dev->user_clients.rb_node;
1064 while (*p) {
1065 parent = *p;
1066 entry = rb_entry(parent, struct ion_client, node);
1067
1068 if (task < entry->task)
1069 p = &(*p)->rb_left;
1070 else if (task > entry->task)
1071 p = &(*p)->rb_right;
1072 }
1073 rb_link_node(&client->node, parent, p);
1074 rb_insert_color(&client->node, &dev->user_clients);
1075 } else {
1076 p = &dev->kernel_clients.rb_node;
1077 while (*p) {
1078 parent = *p;
1079 entry = rb_entry(parent, struct ion_client, node);
1080
1081 if (client < entry)
1082 p = &(*p)->rb_left;
1083 else if (client > entry)
1084 p = &(*p)->rb_right;
1085 }
1086 rb_link_node(&client->node, parent, p);
1087 rb_insert_color(&client->node, &dev->kernel_clients);
1088 }
1089
Laura Abbotteed86032011-12-05 15:32:36 -08001090
1091 client->debug_root = debugfs_create_file(name, 0664,
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001092 dev->debug_root, client,
1093 &debug_client_fops);
1094 mutex_unlock(&dev->lock);
1095
1096 return client;
1097}
1098
Rebecca Schultz Zavin0b7e8ae2011-07-06 18:07:01 -07001099static void _ion_client_destroy(struct kref *kref)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001100{
Rebecca Schultz Zavin0b7e8ae2011-07-06 18:07:01 -07001101 struct ion_client *client = container_of(kref, struct ion_client, ref);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001102 struct ion_device *dev = client->dev;
1103 struct rb_node *n;
1104
1105 pr_debug("%s: %d\n", __func__, __LINE__);
1106 while ((n = rb_first(&client->handles))) {
1107 struct ion_handle *handle = rb_entry(n, struct ion_handle,
1108 node);
1109 ion_handle_destroy(&handle->ref);
1110 }
1111 mutex_lock(&dev->lock);
1112 if (client->task) {
1113 rb_erase(&client->node, &dev->user_clients);
1114 put_task_struct(client->task);
1115 } else {
1116 rb_erase(&client->node, &dev->kernel_clients);
1117 }
1118 debugfs_remove_recursive(client->debug_root);
1119 mutex_unlock(&dev->lock);
1120
Olav Haugan63e5f3b2012-01-11 16:42:37 -08001121 kfree(client->name);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001122 kfree(client);
1123}
1124
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001125static void ion_client_get(struct ion_client *client)
1126{
1127 kref_get(&client->ref);
1128}
1129
1130static int ion_client_put(struct ion_client *client)
1131{
1132 return kref_put(&client->ref, _ion_client_destroy);
1133}
1134
Rebecca Schultz Zavin0b7e8ae2011-07-06 18:07:01 -07001135void ion_client_destroy(struct ion_client *client)
1136{
Jordan Crousea75022c2011-10-12 16:57:47 -06001137 if (client)
1138 ion_client_put(client);
Rebecca Schultz Zavin0b7e8ae2011-07-06 18:07:01 -07001139}
1140
Laura Abbott273dd8e2011-10-12 14:26:33 -07001141int ion_handle_get_flags(struct ion_client *client, struct ion_handle *handle,
1142 unsigned long *flags)
1143{
1144 struct ion_buffer *buffer;
1145
1146 mutex_lock(&client->lock);
1147 if (!ion_handle_validate(client, handle)) {
1148 pr_err("%s: invalid handle passed to %s.\n",
1149 __func__, __func__);
1150 mutex_unlock(&client->lock);
1151 return -EINVAL;
1152 }
1153 buffer = handle->buffer;
1154 mutex_lock(&buffer->lock);
1155 *flags = buffer->flags;
1156 mutex_unlock(&buffer->lock);
1157 mutex_unlock(&client->lock);
1158
1159 return 0;
1160}
1161EXPORT_SYMBOL(ion_handle_get_flags);
1162
Laura Abbott8c017362011-09-22 20:59:12 -07001163int ion_handle_get_size(struct ion_client *client, struct ion_handle *handle,
1164 unsigned long *size)
1165{
1166 struct ion_buffer *buffer;
1167
1168 mutex_lock(&client->lock);
1169 if (!ion_handle_validate(client, handle)) {
1170 pr_err("%s: invalid handle passed to %s.\n",
1171 __func__, __func__);
1172 mutex_unlock(&client->lock);
1173 return -EINVAL;
1174 }
1175 buffer = handle->buffer;
1176 mutex_lock(&buffer->lock);
1177 *size = buffer->size;
1178 mutex_unlock(&buffer->lock);
1179 mutex_unlock(&client->lock);
1180
1181 return 0;
1182}
1183EXPORT_SYMBOL(ion_handle_get_size);
1184
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001185static int ion_share_release(struct inode *inode, struct file* file)
1186{
1187 struct ion_buffer *buffer = file->private_data;
1188
1189 pr_debug("%s: %d\n", __func__, __LINE__);
1190 /* drop the reference to the buffer -- this prevents the
1191 buffer from going away because the client holding it exited
1192 while it was being passed */
1193 ion_buffer_put(buffer);
1194 return 0;
1195}
1196
1197static void ion_vma_open(struct vm_area_struct *vma)
1198{
1199
1200 struct ion_buffer *buffer = vma->vm_file->private_data;
1201 struct ion_handle *handle = vma->vm_private_data;
1202 struct ion_client *client;
1203
1204 pr_debug("%s: %d\n", __func__, __LINE__);
1205 /* check that the client still exists and take a reference so
1206 it can't go away until this vma is closed */
1207 client = ion_client_lookup(buffer->dev, current->group_leader);
1208 if (IS_ERR_OR_NULL(client)) {
1209 vma->vm_private_data = NULL;
1210 return;
1211 }
Laura Abbott0f2175b2011-12-09 14:26:07 -08001212 ion_handle_get(handle);
Laura Abbott77168502011-12-05 11:06:24 -08001213 mutex_lock(&buffer->lock);
1214 buffer->umap_cnt++;
1215 mutex_unlock(&buffer->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001216 pr_debug("%s: %d client_cnt %d handle_cnt %d alloc_cnt %d\n",
1217 __func__, __LINE__,
1218 atomic_read(&client->ref.refcount),
1219 atomic_read(&handle->ref.refcount),
1220 atomic_read(&buffer->ref.refcount));
1221}
1222
1223static void ion_vma_close(struct vm_area_struct *vma)
1224{
1225 struct ion_handle *handle = vma->vm_private_data;
1226 struct ion_buffer *buffer = vma->vm_file->private_data;
1227 struct ion_client *client;
1228
1229 pr_debug("%s: %d\n", __func__, __LINE__);
1230 /* this indicates the client is gone, nothing to do here */
1231 if (!handle)
1232 return;
1233 client = handle->client;
Laura Abbott77168502011-12-05 11:06:24 -08001234 mutex_lock(&buffer->lock);
1235 buffer->umap_cnt--;
1236 mutex_unlock(&buffer->lock);
Laura Abbotta6835092011-11-14 15:27:02 -08001237
1238 if (buffer->heap->ops->unmap_user)
1239 buffer->heap->ops->unmap_user(buffer->heap, buffer);
1240
1241
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001242 pr_debug("%s: %d client_cnt %d handle_cnt %d alloc_cnt %d\n",
1243 __func__, __LINE__,
1244 atomic_read(&client->ref.refcount),
1245 atomic_read(&handle->ref.refcount),
1246 atomic_read(&buffer->ref.refcount));
1247 ion_handle_put(handle);
1248 ion_client_put(client);
1249 pr_debug("%s: %d client_cnt %d handle_cnt %d alloc_cnt %d\n",
1250 __func__, __LINE__,
1251 atomic_read(&client->ref.refcount),
1252 atomic_read(&handle->ref.refcount),
1253 atomic_read(&buffer->ref.refcount));
1254}
1255
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001256static struct vm_operations_struct ion_vm_ops = {
1257 .open = ion_vma_open,
1258 .close = ion_vma_close,
1259};
1260
1261static int ion_share_mmap(struct file *file, struct vm_area_struct *vma)
1262{
1263 struct ion_buffer *buffer = file->private_data;
1264 unsigned long size = vma->vm_end - vma->vm_start;
1265 struct ion_client *client;
1266 struct ion_handle *handle;
1267 int ret;
Laura Abbott894fd582011-08-19 13:33:56 -07001268 unsigned long flags = file->f_flags & O_DSYNC ?
1269 ION_SET_CACHE(UNCACHED) :
1270 ION_SET_CACHE(CACHED);
1271
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001272
1273 pr_debug("%s: %d\n", __func__, __LINE__);
1274 /* make sure the client still exists, it's possible for the client to
1275 have gone away but the map/share fd still to be around, take
1276 a reference to it so it can't go away while this mapping exists */
1277 client = ion_client_lookup(buffer->dev, current->group_leader);
1278 if (IS_ERR_OR_NULL(client)) {
1279 pr_err("%s: trying to mmap an ion handle in a process with no "
1280 "ion client\n", __func__);
1281 return -EINVAL;
1282 }
1283
1284 if ((size > buffer->size) || (size + (vma->vm_pgoff << PAGE_SHIFT) >
1285 buffer->size)) {
1286 pr_err("%s: trying to map larger area than handle has available"
1287 "\n", __func__);
1288 ret = -EINVAL;
1289 goto err;
1290 }
1291
1292 /* find the handle and take a reference to it */
1293 handle = ion_import(client, buffer);
1294 if (IS_ERR_OR_NULL(handle)) {
1295 ret = -EINVAL;
1296 goto err;
1297 }
1298
1299 if (!handle->buffer->heap->ops->map_user) {
1300 pr_err("%s: this heap does not define a method for mapping "
1301 "to userspace\n", __func__);
1302 ret = -EINVAL;
1303 goto err1;
1304 }
1305
1306 mutex_lock(&buffer->lock);
Laura Abbott894fd582011-08-19 13:33:56 -07001307
Laura Abbott8c017362011-09-22 20:59:12 -07001308 if (ion_validate_buffer_flags(buffer, flags)) {
1309 ret = -EEXIST;
1310 mutex_unlock(&buffer->lock);
1311 goto err1;
Laura Abbott894fd582011-08-19 13:33:56 -07001312 }
Laura Abbott8c017362011-09-22 20:59:12 -07001313
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001314 /* now map it to userspace */
Laura Abbott894fd582011-08-19 13:33:56 -07001315 ret = buffer->heap->ops->map_user(buffer->heap, buffer, vma,
1316 flags);
Laura Abbotte8bc7aa2011-12-09 14:49:33 -08001317
1318 buffer->umap_cnt++;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001319 if (ret) {
1320 pr_err("%s: failure mapping buffer to userspace\n",
1321 __func__);
Laura Abbott894fd582011-08-19 13:33:56 -07001322 goto err2;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001323 }
Laura Abbotte8bc7aa2011-12-09 14:49:33 -08001324 mutex_unlock(&buffer->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001325
1326 vma->vm_ops = &ion_vm_ops;
1327 /* move the handle into the vm_private_data so we can access it from
1328 vma_open/close */
1329 vma->vm_private_data = handle;
1330 pr_debug("%s: %d client_cnt %d handle_cnt %d alloc_cnt %d\n",
1331 __func__, __LINE__,
1332 atomic_read(&client->ref.refcount),
1333 atomic_read(&handle->ref.refcount),
1334 atomic_read(&buffer->ref.refcount));
1335 return 0;
1336
Laura Abbott894fd582011-08-19 13:33:56 -07001337err2:
1338 buffer->umap_cnt--;
Laura Abbotte8bc7aa2011-12-09 14:49:33 -08001339 mutex_unlock(&buffer->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001340 /* drop the reference to the handle */
Laura Abbott894fd582011-08-19 13:33:56 -07001341err1:
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001342 ion_handle_put(handle);
1343err:
Iliyan Malchev3fe24362011-08-09 14:42:08 -07001344 /* drop the reference to the client */
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001345 ion_client_put(client);
1346 return ret;
1347}
1348
1349static const struct file_operations ion_share_fops = {
1350 .owner = THIS_MODULE,
1351 .release = ion_share_release,
1352 .mmap = ion_share_mmap,
1353};
1354
1355static int ion_ioctl_share(struct file *parent, struct ion_client *client,
1356 struct ion_handle *handle)
1357{
1358 int fd = get_unused_fd();
1359 struct file *file;
1360
1361 if (fd < 0)
1362 return -ENFILE;
1363
1364 file = anon_inode_getfile("ion_share_fd", &ion_share_fops,
1365 handle->buffer, O_RDWR);
1366 if (IS_ERR_OR_NULL(file))
1367 goto err;
Laura Abbott4b5d0482011-09-27 18:35:14 -07001368
1369 if (parent->f_flags & O_DSYNC)
1370 file->f_flags |= O_DSYNC;
1371
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001372 ion_buffer_get(handle->buffer);
1373 fd_install(fd, file);
1374
1375 return fd;
1376
1377err:
1378 put_unused_fd(fd);
1379 return -ENFILE;
1380}
1381
1382static long ion_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1383{
1384 struct ion_client *client = filp->private_data;
1385
1386 switch (cmd) {
1387 case ION_IOC_ALLOC:
1388 {
1389 struct ion_allocation_data data;
1390
1391 if (copy_from_user(&data, (void __user *)arg, sizeof(data)))
1392 return -EFAULT;
1393 data.handle = ion_alloc(client, data.len, data.align,
1394 data.flags);
Laura Abbotte1b9ce52011-11-11 18:31:39 -08001395
1396 if (IS_ERR_OR_NULL(data.handle))
Olav Hauganb06ee072011-12-13 15:31:41 -08001397 return -ENOMEM;
Laura Abbotte1b9ce52011-11-11 18:31:39 -08001398
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001399 if (copy_to_user((void __user *)arg, &data, sizeof(data)))
1400 return -EFAULT;
1401 break;
1402 }
1403 case ION_IOC_FREE:
1404 {
1405 struct ion_handle_data data;
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001406 bool valid;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001407
1408 if (copy_from_user(&data, (void __user *)arg,
1409 sizeof(struct ion_handle_data)))
1410 return -EFAULT;
1411 mutex_lock(&client->lock);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001412 valid = ion_handle_validate(client, data.handle);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001413 mutex_unlock(&client->lock);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001414 if (!valid)
1415 return -EINVAL;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001416 ion_free(client, data.handle);
1417 break;
1418 }
1419 case ION_IOC_MAP:
1420 case ION_IOC_SHARE:
1421 {
1422 struct ion_fd_data data;
1423
1424 if (copy_from_user(&data, (void __user *)arg, sizeof(data)))
1425 return -EFAULT;
1426 mutex_lock(&client->lock);
1427 if (!ion_handle_validate(client, data.handle)) {
1428 pr_err("%s: invalid handle passed to share ioctl.\n",
1429 __func__);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001430 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001431 return -EINVAL;
1432 }
1433 data.fd = ion_ioctl_share(filp, client, data.handle);
1434 mutex_unlock(&client->lock);
1435 if (copy_to_user((void __user *)arg, &data, sizeof(data)))
1436 return -EFAULT;
1437 break;
1438 }
1439 case ION_IOC_IMPORT:
1440 {
1441 struct ion_fd_data data;
1442 if (copy_from_user(&data, (void __user *)arg,
1443 sizeof(struct ion_fd_data)))
1444 return -EFAULT;
1445
1446 data.handle = ion_import_fd(client, data.fd);
1447 if (IS_ERR(data.handle))
1448 data.handle = NULL;
1449 if (copy_to_user((void __user *)arg, &data,
1450 sizeof(struct ion_fd_data)))
1451 return -EFAULT;
1452 break;
1453 }
1454 case ION_IOC_CUSTOM:
1455 {
1456 struct ion_device *dev = client->dev;
1457 struct ion_custom_data data;
1458
1459 if (!dev->custom_ioctl)
1460 return -ENOTTY;
1461 if (copy_from_user(&data, (void __user *)arg,
1462 sizeof(struct ion_custom_data)))
1463 return -EFAULT;
1464 return dev->custom_ioctl(client, data.cmd, data.arg);
1465 }
Laura Abbottabcb6f72011-10-04 16:26:49 -07001466 case ION_IOC_CLEAN_CACHES:
1467 case ION_IOC_INV_CACHES:
1468 case ION_IOC_CLEAN_INV_CACHES:
1469 {
1470 struct ion_flush_data data;
Laura Abbott9fa29e82011-11-14 09:42:53 -08001471 unsigned long start, end;
Laura Abbotte80ea012011-11-18 18:36:47 -08001472 struct ion_handle *handle = NULL;
1473 int ret;
Laura Abbottabcb6f72011-10-04 16:26:49 -07001474
1475 if (copy_from_user(&data, (void __user *)arg,
1476 sizeof(struct ion_flush_data)))
1477 return -EFAULT;
1478
Laura Abbott9fa29e82011-11-14 09:42:53 -08001479 start = (unsigned long) data.vaddr;
1480 end = (unsigned long) data.vaddr + data.length;
1481
1482 if (check_vaddr_bounds(start, end)) {
1483 pr_err("%s: virtual address %p is out of bounds\n",
1484 __func__, data.vaddr);
1485 return -EINVAL;
1486 }
1487
Laura Abbotte80ea012011-11-18 18:36:47 -08001488 if (!data.handle) {
1489 handle = ion_import_fd(client, data.fd);
1490 if (IS_ERR_OR_NULL(handle)) {
1491 pr_info("%s: Could not import handle: %d\n",
1492 __func__, (int)handle);
1493 return -EINVAL;
1494 }
1495 }
1496
1497 ret = ion_do_cache_op(client,
1498 data.handle ? data.handle : handle,
1499 data.vaddr, data.offset, data.length,
1500 cmd);
1501
1502 if (!data.handle)
1503 ion_free(client, handle);
1504
1505 break;
Laura Abbottabcb6f72011-10-04 16:26:49 -07001506
1507 }
Laura Abbott273dd8e2011-10-12 14:26:33 -07001508 case ION_IOC_GET_FLAGS:
1509 {
1510 struct ion_flag_data data;
1511 int ret;
1512 if (copy_from_user(&data, (void __user *)arg,
1513 sizeof(struct ion_flag_data)))
1514 return -EFAULT;
1515
1516 ret = ion_handle_get_flags(client, data.handle, &data.flags);
1517 if (ret < 0)
1518 return ret;
1519 if (copy_to_user((void __user *)arg, &data,
1520 sizeof(struct ion_flag_data)))
1521 return -EFAULT;
1522 break;
1523 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001524 default:
1525 return -ENOTTY;
1526 }
1527 return 0;
1528}
1529
1530static int ion_release(struct inode *inode, struct file *file)
1531{
1532 struct ion_client *client = file->private_data;
1533
1534 pr_debug("%s: %d\n", __func__, __LINE__);
1535 ion_client_put(client);
1536 return 0;
1537}
1538
1539static int ion_open(struct inode *inode, struct file *file)
1540{
1541 struct miscdevice *miscdev = file->private_data;
1542 struct ion_device *dev = container_of(miscdev, struct ion_device, dev);
1543 struct ion_client *client;
Laura Abbotteed86032011-12-05 15:32:36 -08001544 char debug_name[64];
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001545
1546 pr_debug("%s: %d\n", __func__, __LINE__);
Laura Abbotteed86032011-12-05 15:32:36 -08001547 snprintf(debug_name, 64, "%u", task_pid_nr(current->group_leader));
1548 client = ion_client_create(dev, -1, debug_name);
Rebecca Schultz Zavin6d3b9582011-07-06 18:07:24 -07001549 if (IS_ERR_OR_NULL(client))
1550 return PTR_ERR(client);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001551 file->private_data = client;
1552
1553 return 0;
1554}
1555
1556static const struct file_operations ion_fops = {
1557 .owner = THIS_MODULE,
1558 .open = ion_open,
1559 .release = ion_release,
1560 .unlocked_ioctl = ion_ioctl,
1561};
1562
1563static size_t ion_debug_heap_total(struct ion_client *client,
Laura Abbott3647ac32011-10-31 14:09:53 -07001564 enum ion_heap_ids id)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001565{
1566 size_t size = 0;
1567 struct rb_node *n;
1568
1569 mutex_lock(&client->lock);
1570 for (n = rb_first(&client->handles); n; n = rb_next(n)) {
1571 struct ion_handle *handle = rb_entry(n,
1572 struct ion_handle,
1573 node);
Laura Abbott3647ac32011-10-31 14:09:53 -07001574 if (handle->buffer->heap->id == id)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001575 size += handle->buffer->size;
1576 }
1577 mutex_unlock(&client->lock);
1578 return size;
1579}
1580
1581static int ion_debug_heap_show(struct seq_file *s, void *unused)
1582{
1583 struct ion_heap *heap = s->private;
1584 struct ion_device *dev = heap->dev;
1585 struct rb_node *n;
1586
1587 seq_printf(s, "%16.s %16.s %16.s\n", "client", "pid", "size");
1588 for (n = rb_first(&dev->user_clients); n; n = rb_next(n)) {
1589 struct ion_client *client = rb_entry(n, struct ion_client,
1590 node);
1591 char task_comm[TASK_COMM_LEN];
Laura Abbott3647ac32011-10-31 14:09:53 -07001592 size_t size = ion_debug_heap_total(client, heap->id);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001593 if (!size)
1594 continue;
1595
1596 get_task_comm(task_comm, client->task);
Laura Abbott8747bbe2011-10-31 14:18:13 -07001597 seq_printf(s, "%16.s %16u %16x\n", task_comm, client->pid,
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001598 size);
1599 }
1600
1601 for (n = rb_first(&dev->kernel_clients); n; n = rb_next(n)) {
1602 struct ion_client *client = rb_entry(n, struct ion_client,
1603 node);
Laura Abbott3647ac32011-10-31 14:09:53 -07001604 size_t size = ion_debug_heap_total(client, heap->id);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001605 if (!size)
1606 continue;
Laura Abbott8747bbe2011-10-31 14:18:13 -07001607 seq_printf(s, "%16.s %16u %16x\n", client->name, client->pid,
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001608 size);
1609 }
Olav Haugan3d4fe1a2012-01-13 11:42:15 -08001610 if (heap->ops->print_debug)
1611 heap->ops->print_debug(heap, s);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001612 return 0;
1613}
1614
1615static int ion_debug_heap_open(struct inode *inode, struct file *file)
1616{
1617 return single_open(file, ion_debug_heap_show, inode->i_private);
1618}
1619
1620static const struct file_operations debug_heap_fops = {
1621 .open = ion_debug_heap_open,
1622 .read = seq_read,
1623 .llseek = seq_lseek,
1624 .release = single_release,
1625};
1626
1627void ion_device_add_heap(struct ion_device *dev, struct ion_heap *heap)
1628{
1629 struct rb_node **p = &dev->heaps.rb_node;
1630 struct rb_node *parent = NULL;
1631 struct ion_heap *entry;
1632
1633 heap->dev = dev;
1634 mutex_lock(&dev->lock);
1635 while (*p) {
1636 parent = *p;
1637 entry = rb_entry(parent, struct ion_heap, node);
1638
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001639 if (heap->id < entry->id) {
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001640 p = &(*p)->rb_left;
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001641 } else if (heap->id > entry->id ) {
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001642 p = &(*p)->rb_right;
1643 } else {
1644 pr_err("%s: can not insert multiple heaps with "
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001645 "id %d\n", __func__, heap->id);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001646 goto end;
1647 }
1648 }
1649
1650 rb_link_node(&heap->node, parent, p);
1651 rb_insert_color(&heap->node, &dev->heaps);
1652 debugfs_create_file(heap->name, 0664, dev->debug_root, heap,
1653 &debug_heap_fops);
1654end:
1655 mutex_unlock(&dev->lock);
1656}
1657
Olav Haugan0a852512012-01-09 10:20:55 -08001658int ion_secure_heap(struct ion_device *dev, int heap_id)
1659{
1660 struct rb_node *n;
1661 int ret_val = 0;
1662
1663 /*
1664 * traverse the list of heaps available in this system
1665 * and find the heap that is specified.
1666 */
1667 mutex_lock(&dev->lock);
1668 for (n = rb_first(&dev->heaps); n != NULL; n = rb_next(n)) {
1669 struct ion_heap *heap = rb_entry(n, struct ion_heap, node);
1670 if (heap->type != ION_HEAP_TYPE_CP)
1671 continue;
1672 if (ION_HEAP(heap->id) != heap_id)
1673 continue;
1674 if (heap->ops->secure_heap)
1675 ret_val = heap->ops->secure_heap(heap);
1676 else
1677 ret_val = -EINVAL;
1678 break;
1679 }
1680 mutex_unlock(&dev->lock);
1681 return ret_val;
1682}
1683EXPORT_SYMBOL(ion_secure_heap);
1684
1685int ion_unsecure_heap(struct ion_device *dev, int heap_id)
1686{
1687 struct rb_node *n;
1688 int ret_val = 0;
1689
1690 /*
1691 * traverse the list of heaps available in this system
1692 * and find the heap that is specified.
1693 */
1694 mutex_lock(&dev->lock);
1695 for (n = rb_first(&dev->heaps); n != NULL; n = rb_next(n)) {
1696 struct ion_heap *heap = rb_entry(n, struct ion_heap, node);
1697 if (heap->type != ION_HEAP_TYPE_CP)
1698 continue;
1699 if (ION_HEAP(heap->id) != heap_id)
1700 continue;
1701 if (heap->ops->secure_heap)
1702 ret_val = heap->ops->unsecure_heap(heap);
1703 else
1704 ret_val = -EINVAL;
1705 break;
1706 }
1707 mutex_unlock(&dev->lock);
1708 return ret_val;
1709}
1710EXPORT_SYMBOL(ion_unsecure_heap);
1711
Laura Abbott404f8242011-10-31 14:22:53 -07001712static int ion_debug_leak_show(struct seq_file *s, void *unused)
1713{
1714 struct ion_device *dev = s->private;
1715 struct rb_node *n;
1716 struct rb_node *n2;
1717
1718 /* mark all buffers as 1 */
1719 seq_printf(s, "%16.s %16.s %16.s %16.s\n", "buffer", "heap", "size",
1720 "ref cnt");
1721 mutex_lock(&dev->lock);
1722 for (n = rb_first(&dev->buffers); n; n = rb_next(n)) {
1723 struct ion_buffer *buf = rb_entry(n, struct ion_buffer,
1724 node);
1725
1726 buf->marked = 1;
1727 }
1728
1729 /* now see which buffers we can access */
1730 for (n = rb_first(&dev->kernel_clients); n; n = rb_next(n)) {
1731 struct ion_client *client = rb_entry(n, struct ion_client,
1732 node);
1733
1734 mutex_lock(&client->lock);
1735 for (n2 = rb_first(&client->handles); n2; n2 = rb_next(n2)) {
1736 struct ion_handle *handle = rb_entry(n2,
1737 struct ion_handle, node);
1738
1739 handle->buffer->marked = 0;
1740
1741 }
1742 mutex_unlock(&client->lock);
1743
1744 }
1745
1746 for (n = rb_first(&dev->user_clients); n; n = rb_next(n)) {
1747 struct ion_client *client = rb_entry(n, struct ion_client,
1748 node);
1749
1750 mutex_lock(&client->lock);
1751 for (n2 = rb_first(&client->handles); n2; n2 = rb_next(n2)) {
1752 struct ion_handle *handle = rb_entry(n2,
1753 struct ion_handle, node);
1754
1755 handle->buffer->marked = 0;
1756
1757 }
1758 mutex_unlock(&client->lock);
1759
1760 }
1761 /* And anyone still marked as a 1 means a leaked handle somewhere */
1762 for (n = rb_first(&dev->buffers); n; n = rb_next(n)) {
1763 struct ion_buffer *buf = rb_entry(n, struct ion_buffer,
1764 node);
1765
1766 if (buf->marked == 1)
1767 seq_printf(s, "%16.x %16.s %16.x %16.d\n",
1768 (int)buf, buf->heap->name, buf->size,
1769 atomic_read(&buf->ref.refcount));
1770 }
1771 mutex_unlock(&dev->lock);
1772 return 0;
1773}
1774
1775static int ion_debug_leak_open(struct inode *inode, struct file *file)
1776{
1777 return single_open(file, ion_debug_leak_show, inode->i_private);
1778}
1779
1780static const struct file_operations debug_leak_fops = {
1781 .open = ion_debug_leak_open,
1782 .read = seq_read,
1783 .llseek = seq_lseek,
1784 .release = single_release,
1785};
1786
1787
1788
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001789struct ion_device *ion_device_create(long (*custom_ioctl)
1790 (struct ion_client *client,
1791 unsigned int cmd,
1792 unsigned long arg))
1793{
1794 struct ion_device *idev;
1795 int ret;
1796
1797 idev = kzalloc(sizeof(struct ion_device), GFP_KERNEL);
1798 if (!idev)
1799 return ERR_PTR(-ENOMEM);
1800
1801 idev->dev.minor = MISC_DYNAMIC_MINOR;
1802 idev->dev.name = "ion";
1803 idev->dev.fops = &ion_fops;
1804 idev->dev.parent = NULL;
1805 ret = misc_register(&idev->dev);
1806 if (ret) {
1807 pr_err("ion: failed to register misc device.\n");
1808 return ERR_PTR(ret);
1809 }
1810
1811 idev->debug_root = debugfs_create_dir("ion", NULL);
1812 if (IS_ERR_OR_NULL(idev->debug_root))
1813 pr_err("ion: failed to create debug files.\n");
1814
1815 idev->custom_ioctl = custom_ioctl;
1816 idev->buffers = RB_ROOT;
1817 mutex_init(&idev->lock);
1818 idev->heaps = RB_ROOT;
1819 idev->user_clients = RB_ROOT;
1820 idev->kernel_clients = RB_ROOT;
Laura Abbott404f8242011-10-31 14:22:53 -07001821 debugfs_create_file("check_leaked_fds", 0664, idev->debug_root, idev,
1822 &debug_leak_fops);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001823 return idev;
1824}
1825
1826void ion_device_destroy(struct ion_device *dev)
1827{
1828 misc_deregister(&dev->dev);
1829 /* XXX need to free the heaps and clients ? */
1830 kfree(dev);
1831}