blob: 386593f5005052404753648d544c7b559f3092a8 [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
1047 client->name = kzalloc(sizeof(name_len+1), GFP_KERNEL);
1048 if (!client->name) {
1049 put_task_struct(current->group_leader);
1050 kfree(client);
1051 return ERR_PTR(-ENOMEM);
1052 } else {
1053 strncpy(client->name, name, name_len);
1054 client->name[name_len] = '\0';
1055 }
1056
Rebecca Schultz Zavin83e3dab2011-07-01 20:41:25 -07001057 client->heap_mask = heap_mask;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001058 client->task = task;
Rebecca Schultz Zavin83e3dab2011-07-01 20:41:25 -07001059 client->pid = pid;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001060 kref_init(&client->ref);
1061
1062 mutex_lock(&dev->lock);
1063 if (task) {
1064 p = &dev->user_clients.rb_node;
1065 while (*p) {
1066 parent = *p;
1067 entry = rb_entry(parent, struct ion_client, node);
1068
1069 if (task < entry->task)
1070 p = &(*p)->rb_left;
1071 else if (task > entry->task)
1072 p = &(*p)->rb_right;
1073 }
1074 rb_link_node(&client->node, parent, p);
1075 rb_insert_color(&client->node, &dev->user_clients);
1076 } else {
1077 p = &dev->kernel_clients.rb_node;
1078 while (*p) {
1079 parent = *p;
1080 entry = rb_entry(parent, struct ion_client, node);
1081
1082 if (client < entry)
1083 p = &(*p)->rb_left;
1084 else if (client > entry)
1085 p = &(*p)->rb_right;
1086 }
1087 rb_link_node(&client->node, parent, p);
1088 rb_insert_color(&client->node, &dev->kernel_clients);
1089 }
1090
Laura Abbotteed86032011-12-05 15:32:36 -08001091
1092 client->debug_root = debugfs_create_file(name, 0664,
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001093 dev->debug_root, client,
1094 &debug_client_fops);
1095 mutex_unlock(&dev->lock);
1096
1097 return client;
1098}
1099
Rebecca Schultz Zavin0b7e8ae2011-07-06 18:07:01 -07001100static void _ion_client_destroy(struct kref *kref)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001101{
Rebecca Schultz Zavin0b7e8ae2011-07-06 18:07:01 -07001102 struct ion_client *client = container_of(kref, struct ion_client, ref);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001103 struct ion_device *dev = client->dev;
1104 struct rb_node *n;
1105
1106 pr_debug("%s: %d\n", __func__, __LINE__);
1107 while ((n = rb_first(&client->handles))) {
1108 struct ion_handle *handle = rb_entry(n, struct ion_handle,
1109 node);
1110 ion_handle_destroy(&handle->ref);
1111 }
1112 mutex_lock(&dev->lock);
1113 if (client->task) {
1114 rb_erase(&client->node, &dev->user_clients);
1115 put_task_struct(client->task);
1116 } else {
1117 rb_erase(&client->node, &dev->kernel_clients);
1118 }
1119 debugfs_remove_recursive(client->debug_root);
1120 mutex_unlock(&dev->lock);
1121
Olav Haugan63e5f3b2012-01-11 16:42:37 -08001122 kfree(client->name);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001123 kfree(client);
1124}
1125
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001126static void ion_client_get(struct ion_client *client)
1127{
1128 kref_get(&client->ref);
1129}
1130
1131static int ion_client_put(struct ion_client *client)
1132{
1133 return kref_put(&client->ref, _ion_client_destroy);
1134}
1135
Rebecca Schultz Zavin0b7e8ae2011-07-06 18:07:01 -07001136void ion_client_destroy(struct ion_client *client)
1137{
Jordan Crousea75022c2011-10-12 16:57:47 -06001138 if (client)
1139 ion_client_put(client);
Rebecca Schultz Zavin0b7e8ae2011-07-06 18:07:01 -07001140}
1141
Laura Abbott273dd8e2011-10-12 14:26:33 -07001142int ion_handle_get_flags(struct ion_client *client, struct ion_handle *handle,
1143 unsigned long *flags)
1144{
1145 struct ion_buffer *buffer;
1146
1147 mutex_lock(&client->lock);
1148 if (!ion_handle_validate(client, handle)) {
1149 pr_err("%s: invalid handle passed to %s.\n",
1150 __func__, __func__);
1151 mutex_unlock(&client->lock);
1152 return -EINVAL;
1153 }
1154 buffer = handle->buffer;
1155 mutex_lock(&buffer->lock);
1156 *flags = buffer->flags;
1157 mutex_unlock(&buffer->lock);
1158 mutex_unlock(&client->lock);
1159
1160 return 0;
1161}
1162EXPORT_SYMBOL(ion_handle_get_flags);
1163
Laura Abbott8c017362011-09-22 20:59:12 -07001164int ion_handle_get_size(struct ion_client *client, struct ion_handle *handle,
1165 unsigned long *size)
1166{
1167 struct ion_buffer *buffer;
1168
1169 mutex_lock(&client->lock);
1170 if (!ion_handle_validate(client, handle)) {
1171 pr_err("%s: invalid handle passed to %s.\n",
1172 __func__, __func__);
1173 mutex_unlock(&client->lock);
1174 return -EINVAL;
1175 }
1176 buffer = handle->buffer;
1177 mutex_lock(&buffer->lock);
1178 *size = buffer->size;
1179 mutex_unlock(&buffer->lock);
1180 mutex_unlock(&client->lock);
1181
1182 return 0;
1183}
1184EXPORT_SYMBOL(ion_handle_get_size);
1185
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001186static int ion_share_release(struct inode *inode, struct file* file)
1187{
1188 struct ion_buffer *buffer = file->private_data;
1189
1190 pr_debug("%s: %d\n", __func__, __LINE__);
1191 /* drop the reference to the buffer -- this prevents the
1192 buffer from going away because the client holding it exited
1193 while it was being passed */
1194 ion_buffer_put(buffer);
1195 return 0;
1196}
1197
1198static void ion_vma_open(struct vm_area_struct *vma)
1199{
1200
1201 struct ion_buffer *buffer = vma->vm_file->private_data;
1202 struct ion_handle *handle = vma->vm_private_data;
1203 struct ion_client *client;
1204
1205 pr_debug("%s: %d\n", __func__, __LINE__);
1206 /* check that the client still exists and take a reference so
1207 it can't go away until this vma is closed */
1208 client = ion_client_lookup(buffer->dev, current->group_leader);
1209 if (IS_ERR_OR_NULL(client)) {
1210 vma->vm_private_data = NULL;
1211 return;
1212 }
Laura Abbott0f2175b2011-12-09 14:26:07 -08001213 ion_handle_get(handle);
Laura Abbott77168502011-12-05 11:06:24 -08001214 mutex_lock(&buffer->lock);
1215 buffer->umap_cnt++;
1216 mutex_unlock(&buffer->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001217 pr_debug("%s: %d client_cnt %d handle_cnt %d alloc_cnt %d\n",
1218 __func__, __LINE__,
1219 atomic_read(&client->ref.refcount),
1220 atomic_read(&handle->ref.refcount),
1221 atomic_read(&buffer->ref.refcount));
1222}
1223
1224static void ion_vma_close(struct vm_area_struct *vma)
1225{
1226 struct ion_handle *handle = vma->vm_private_data;
1227 struct ion_buffer *buffer = vma->vm_file->private_data;
1228 struct ion_client *client;
1229
1230 pr_debug("%s: %d\n", __func__, __LINE__);
1231 /* this indicates the client is gone, nothing to do here */
1232 if (!handle)
1233 return;
1234 client = handle->client;
Laura Abbott77168502011-12-05 11:06:24 -08001235 mutex_lock(&buffer->lock);
1236 buffer->umap_cnt--;
1237 mutex_unlock(&buffer->lock);
Laura Abbotta6835092011-11-14 15:27:02 -08001238
1239 if (buffer->heap->ops->unmap_user)
1240 buffer->heap->ops->unmap_user(buffer->heap, buffer);
1241
1242
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001243 pr_debug("%s: %d client_cnt %d handle_cnt %d alloc_cnt %d\n",
1244 __func__, __LINE__,
1245 atomic_read(&client->ref.refcount),
1246 atomic_read(&handle->ref.refcount),
1247 atomic_read(&buffer->ref.refcount));
1248 ion_handle_put(handle);
1249 ion_client_put(client);
1250 pr_debug("%s: %d client_cnt %d handle_cnt %d alloc_cnt %d\n",
1251 __func__, __LINE__,
1252 atomic_read(&client->ref.refcount),
1253 atomic_read(&handle->ref.refcount),
1254 atomic_read(&buffer->ref.refcount));
1255}
1256
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001257static struct vm_operations_struct ion_vm_ops = {
1258 .open = ion_vma_open,
1259 .close = ion_vma_close,
1260};
1261
1262static int ion_share_mmap(struct file *file, struct vm_area_struct *vma)
1263{
1264 struct ion_buffer *buffer = file->private_data;
1265 unsigned long size = vma->vm_end - vma->vm_start;
1266 struct ion_client *client;
1267 struct ion_handle *handle;
1268 int ret;
Laura Abbott894fd582011-08-19 13:33:56 -07001269 unsigned long flags = file->f_flags & O_DSYNC ?
1270 ION_SET_CACHE(UNCACHED) :
1271 ION_SET_CACHE(CACHED);
1272
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001273
1274 pr_debug("%s: %d\n", __func__, __LINE__);
1275 /* make sure the client still exists, it's possible for the client to
1276 have gone away but the map/share fd still to be around, take
1277 a reference to it so it can't go away while this mapping exists */
1278 client = ion_client_lookup(buffer->dev, current->group_leader);
1279 if (IS_ERR_OR_NULL(client)) {
1280 pr_err("%s: trying to mmap an ion handle in a process with no "
1281 "ion client\n", __func__);
1282 return -EINVAL;
1283 }
1284
1285 if ((size > buffer->size) || (size + (vma->vm_pgoff << PAGE_SHIFT) >
1286 buffer->size)) {
1287 pr_err("%s: trying to map larger area than handle has available"
1288 "\n", __func__);
1289 ret = -EINVAL;
1290 goto err;
1291 }
1292
1293 /* find the handle and take a reference to it */
1294 handle = ion_import(client, buffer);
1295 if (IS_ERR_OR_NULL(handle)) {
1296 ret = -EINVAL;
1297 goto err;
1298 }
1299
1300 if (!handle->buffer->heap->ops->map_user) {
1301 pr_err("%s: this heap does not define a method for mapping "
1302 "to userspace\n", __func__);
1303 ret = -EINVAL;
1304 goto err1;
1305 }
1306
1307 mutex_lock(&buffer->lock);
Laura Abbott894fd582011-08-19 13:33:56 -07001308
Laura Abbott8c017362011-09-22 20:59:12 -07001309 if (ion_validate_buffer_flags(buffer, flags)) {
1310 ret = -EEXIST;
1311 mutex_unlock(&buffer->lock);
1312 goto err1;
Laura Abbott894fd582011-08-19 13:33:56 -07001313 }
Laura Abbott8c017362011-09-22 20:59:12 -07001314
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001315 /* now map it to userspace */
Laura Abbott894fd582011-08-19 13:33:56 -07001316 ret = buffer->heap->ops->map_user(buffer->heap, buffer, vma,
1317 flags);
Laura Abbotte8bc7aa2011-12-09 14:49:33 -08001318
1319 buffer->umap_cnt++;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001320 if (ret) {
1321 pr_err("%s: failure mapping buffer to userspace\n",
1322 __func__);
Laura Abbott894fd582011-08-19 13:33:56 -07001323 goto err2;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001324 }
Laura Abbotte8bc7aa2011-12-09 14:49:33 -08001325 mutex_unlock(&buffer->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001326
1327 vma->vm_ops = &ion_vm_ops;
1328 /* move the handle into the vm_private_data so we can access it from
1329 vma_open/close */
1330 vma->vm_private_data = handle;
1331 pr_debug("%s: %d client_cnt %d handle_cnt %d alloc_cnt %d\n",
1332 __func__, __LINE__,
1333 atomic_read(&client->ref.refcount),
1334 atomic_read(&handle->ref.refcount),
1335 atomic_read(&buffer->ref.refcount));
1336 return 0;
1337
Laura Abbott894fd582011-08-19 13:33:56 -07001338err2:
1339 buffer->umap_cnt--;
Laura Abbotte8bc7aa2011-12-09 14:49:33 -08001340 mutex_unlock(&buffer->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001341 /* drop the reference to the handle */
Laura Abbott894fd582011-08-19 13:33:56 -07001342err1:
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001343 ion_handle_put(handle);
1344err:
Iliyan Malchev3fe24362011-08-09 14:42:08 -07001345 /* drop the reference to the client */
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001346 ion_client_put(client);
1347 return ret;
1348}
1349
1350static const struct file_operations ion_share_fops = {
1351 .owner = THIS_MODULE,
1352 .release = ion_share_release,
1353 .mmap = ion_share_mmap,
1354};
1355
1356static int ion_ioctl_share(struct file *parent, struct ion_client *client,
1357 struct ion_handle *handle)
1358{
1359 int fd = get_unused_fd();
1360 struct file *file;
1361
1362 if (fd < 0)
1363 return -ENFILE;
1364
1365 file = anon_inode_getfile("ion_share_fd", &ion_share_fops,
1366 handle->buffer, O_RDWR);
1367 if (IS_ERR_OR_NULL(file))
1368 goto err;
Laura Abbott4b5d0482011-09-27 18:35:14 -07001369
1370 if (parent->f_flags & O_DSYNC)
1371 file->f_flags |= O_DSYNC;
1372
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001373 ion_buffer_get(handle->buffer);
1374 fd_install(fd, file);
1375
1376 return fd;
1377
1378err:
1379 put_unused_fd(fd);
1380 return -ENFILE;
1381}
1382
1383static long ion_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1384{
1385 struct ion_client *client = filp->private_data;
1386
1387 switch (cmd) {
1388 case ION_IOC_ALLOC:
1389 {
1390 struct ion_allocation_data data;
1391
1392 if (copy_from_user(&data, (void __user *)arg, sizeof(data)))
1393 return -EFAULT;
1394 data.handle = ion_alloc(client, data.len, data.align,
1395 data.flags);
Laura Abbotte1b9ce52011-11-11 18:31:39 -08001396
1397 if (IS_ERR_OR_NULL(data.handle))
Olav Hauganb06ee072011-12-13 15:31:41 -08001398 return -ENOMEM;
Laura Abbotte1b9ce52011-11-11 18:31:39 -08001399
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001400 if (copy_to_user((void __user *)arg, &data, sizeof(data)))
1401 return -EFAULT;
1402 break;
1403 }
1404 case ION_IOC_FREE:
1405 {
1406 struct ion_handle_data data;
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001407 bool valid;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001408
1409 if (copy_from_user(&data, (void __user *)arg,
1410 sizeof(struct ion_handle_data)))
1411 return -EFAULT;
1412 mutex_lock(&client->lock);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001413 valid = ion_handle_validate(client, data.handle);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001414 mutex_unlock(&client->lock);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001415 if (!valid)
1416 return -EINVAL;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001417 ion_free(client, data.handle);
1418 break;
1419 }
1420 case ION_IOC_MAP:
1421 case ION_IOC_SHARE:
1422 {
1423 struct ion_fd_data data;
1424
1425 if (copy_from_user(&data, (void __user *)arg, sizeof(data)))
1426 return -EFAULT;
1427 mutex_lock(&client->lock);
1428 if (!ion_handle_validate(client, data.handle)) {
1429 pr_err("%s: invalid handle passed to share ioctl.\n",
1430 __func__);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001431 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001432 return -EINVAL;
1433 }
1434 data.fd = ion_ioctl_share(filp, client, data.handle);
1435 mutex_unlock(&client->lock);
1436 if (copy_to_user((void __user *)arg, &data, sizeof(data)))
1437 return -EFAULT;
1438 break;
1439 }
1440 case ION_IOC_IMPORT:
1441 {
1442 struct ion_fd_data data;
1443 if (copy_from_user(&data, (void __user *)arg,
1444 sizeof(struct ion_fd_data)))
1445 return -EFAULT;
1446
1447 data.handle = ion_import_fd(client, data.fd);
1448 if (IS_ERR(data.handle))
1449 data.handle = NULL;
1450 if (copy_to_user((void __user *)arg, &data,
1451 sizeof(struct ion_fd_data)))
1452 return -EFAULT;
1453 break;
1454 }
1455 case ION_IOC_CUSTOM:
1456 {
1457 struct ion_device *dev = client->dev;
1458 struct ion_custom_data data;
1459
1460 if (!dev->custom_ioctl)
1461 return -ENOTTY;
1462 if (copy_from_user(&data, (void __user *)arg,
1463 sizeof(struct ion_custom_data)))
1464 return -EFAULT;
1465 return dev->custom_ioctl(client, data.cmd, data.arg);
1466 }
Laura Abbottabcb6f72011-10-04 16:26:49 -07001467 case ION_IOC_CLEAN_CACHES:
1468 case ION_IOC_INV_CACHES:
1469 case ION_IOC_CLEAN_INV_CACHES:
1470 {
1471 struct ion_flush_data data;
Laura Abbott9fa29e82011-11-14 09:42:53 -08001472 unsigned long start, end;
Laura Abbotte80ea012011-11-18 18:36:47 -08001473 struct ion_handle *handle = NULL;
1474 int ret;
Laura Abbottabcb6f72011-10-04 16:26:49 -07001475
1476 if (copy_from_user(&data, (void __user *)arg,
1477 sizeof(struct ion_flush_data)))
1478 return -EFAULT;
1479
Laura Abbott9fa29e82011-11-14 09:42:53 -08001480 start = (unsigned long) data.vaddr;
1481 end = (unsigned long) data.vaddr + data.length;
1482
1483 if (check_vaddr_bounds(start, end)) {
1484 pr_err("%s: virtual address %p is out of bounds\n",
1485 __func__, data.vaddr);
1486 return -EINVAL;
1487 }
1488
Laura Abbotte80ea012011-11-18 18:36:47 -08001489 if (!data.handle) {
1490 handle = ion_import_fd(client, data.fd);
1491 if (IS_ERR_OR_NULL(handle)) {
1492 pr_info("%s: Could not import handle: %d\n",
1493 __func__, (int)handle);
1494 return -EINVAL;
1495 }
1496 }
1497
1498 ret = ion_do_cache_op(client,
1499 data.handle ? data.handle : handle,
1500 data.vaddr, data.offset, data.length,
1501 cmd);
1502
1503 if (!data.handle)
1504 ion_free(client, handle);
1505
1506 break;
Laura Abbottabcb6f72011-10-04 16:26:49 -07001507
1508 }
Laura Abbott273dd8e2011-10-12 14:26:33 -07001509 case ION_IOC_GET_FLAGS:
1510 {
1511 struct ion_flag_data data;
1512 int ret;
1513 if (copy_from_user(&data, (void __user *)arg,
1514 sizeof(struct ion_flag_data)))
1515 return -EFAULT;
1516
1517 ret = ion_handle_get_flags(client, data.handle, &data.flags);
1518 if (ret < 0)
1519 return ret;
1520 if (copy_to_user((void __user *)arg, &data,
1521 sizeof(struct ion_flag_data)))
1522 return -EFAULT;
1523 break;
1524 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001525 default:
1526 return -ENOTTY;
1527 }
1528 return 0;
1529}
1530
1531static int ion_release(struct inode *inode, struct file *file)
1532{
1533 struct ion_client *client = file->private_data;
1534
1535 pr_debug("%s: %d\n", __func__, __LINE__);
1536 ion_client_put(client);
1537 return 0;
1538}
1539
1540static int ion_open(struct inode *inode, struct file *file)
1541{
1542 struct miscdevice *miscdev = file->private_data;
1543 struct ion_device *dev = container_of(miscdev, struct ion_device, dev);
1544 struct ion_client *client;
Laura Abbotteed86032011-12-05 15:32:36 -08001545 char debug_name[64];
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001546
1547 pr_debug("%s: %d\n", __func__, __LINE__);
Laura Abbotteed86032011-12-05 15:32:36 -08001548 snprintf(debug_name, 64, "%u", task_pid_nr(current->group_leader));
1549 client = ion_client_create(dev, -1, debug_name);
Rebecca Schultz Zavin6d3b9582011-07-06 18:07:24 -07001550 if (IS_ERR_OR_NULL(client))
1551 return PTR_ERR(client);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001552 file->private_data = client;
1553
1554 return 0;
1555}
1556
1557static const struct file_operations ion_fops = {
1558 .owner = THIS_MODULE,
1559 .open = ion_open,
1560 .release = ion_release,
1561 .unlocked_ioctl = ion_ioctl,
1562};
1563
1564static size_t ion_debug_heap_total(struct ion_client *client,
Laura Abbott3647ac32011-10-31 14:09:53 -07001565 enum ion_heap_ids id)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001566{
1567 size_t size = 0;
1568 struct rb_node *n;
1569
1570 mutex_lock(&client->lock);
1571 for (n = rb_first(&client->handles); n; n = rb_next(n)) {
1572 struct ion_handle *handle = rb_entry(n,
1573 struct ion_handle,
1574 node);
Laura Abbott3647ac32011-10-31 14:09:53 -07001575 if (handle->buffer->heap->id == id)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001576 size += handle->buffer->size;
1577 }
1578 mutex_unlock(&client->lock);
1579 return size;
1580}
1581
1582static int ion_debug_heap_show(struct seq_file *s, void *unused)
1583{
1584 struct ion_heap *heap = s->private;
1585 struct ion_device *dev = heap->dev;
1586 struct rb_node *n;
1587
1588 seq_printf(s, "%16.s %16.s %16.s\n", "client", "pid", "size");
1589 for (n = rb_first(&dev->user_clients); n; n = rb_next(n)) {
1590 struct ion_client *client = rb_entry(n, struct ion_client,
1591 node);
1592 char task_comm[TASK_COMM_LEN];
Laura Abbott3647ac32011-10-31 14:09:53 -07001593 size_t size = ion_debug_heap_total(client, heap->id);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001594 if (!size)
1595 continue;
1596
1597 get_task_comm(task_comm, client->task);
Laura Abbott8747bbe2011-10-31 14:18:13 -07001598 seq_printf(s, "%16.s %16u %16x\n", task_comm, client->pid,
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001599 size);
1600 }
1601
1602 for (n = rb_first(&dev->kernel_clients); n; n = rb_next(n)) {
1603 struct ion_client *client = rb_entry(n, struct ion_client,
1604 node);
Laura Abbott3647ac32011-10-31 14:09:53 -07001605 size_t size = ion_debug_heap_total(client, heap->id);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001606 if (!size)
1607 continue;
Laura Abbott8747bbe2011-10-31 14:18:13 -07001608 seq_printf(s, "%16.s %16u %16x\n", client->name, client->pid,
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001609 size);
1610 }
Olav Haugan3d4fe1a2012-01-13 11:42:15 -08001611 if (heap->ops->print_debug)
1612 heap->ops->print_debug(heap, s);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001613 return 0;
1614}
1615
1616static int ion_debug_heap_open(struct inode *inode, struct file *file)
1617{
1618 return single_open(file, ion_debug_heap_show, inode->i_private);
1619}
1620
1621static const struct file_operations debug_heap_fops = {
1622 .open = ion_debug_heap_open,
1623 .read = seq_read,
1624 .llseek = seq_lseek,
1625 .release = single_release,
1626};
1627
1628void ion_device_add_heap(struct ion_device *dev, struct ion_heap *heap)
1629{
1630 struct rb_node **p = &dev->heaps.rb_node;
1631 struct rb_node *parent = NULL;
1632 struct ion_heap *entry;
1633
1634 heap->dev = dev;
1635 mutex_lock(&dev->lock);
1636 while (*p) {
1637 parent = *p;
1638 entry = rb_entry(parent, struct ion_heap, node);
1639
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001640 if (heap->id < entry->id) {
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001641 p = &(*p)->rb_left;
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001642 } else if (heap->id > entry->id ) {
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001643 p = &(*p)->rb_right;
1644 } else {
1645 pr_err("%s: can not insert multiple heaps with "
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001646 "id %d\n", __func__, heap->id);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001647 goto end;
1648 }
1649 }
1650
1651 rb_link_node(&heap->node, parent, p);
1652 rb_insert_color(&heap->node, &dev->heaps);
1653 debugfs_create_file(heap->name, 0664, dev->debug_root, heap,
1654 &debug_heap_fops);
1655end:
1656 mutex_unlock(&dev->lock);
1657}
1658
Olav Haugan0a852512012-01-09 10:20:55 -08001659int ion_secure_heap(struct ion_device *dev, int heap_id)
1660{
1661 struct rb_node *n;
1662 int ret_val = 0;
1663
1664 /*
1665 * traverse the list of heaps available in this system
1666 * and find the heap that is specified.
1667 */
1668 mutex_lock(&dev->lock);
1669 for (n = rb_first(&dev->heaps); n != NULL; n = rb_next(n)) {
1670 struct ion_heap *heap = rb_entry(n, struct ion_heap, node);
1671 if (heap->type != ION_HEAP_TYPE_CP)
1672 continue;
1673 if (ION_HEAP(heap->id) != heap_id)
1674 continue;
1675 if (heap->ops->secure_heap)
1676 ret_val = heap->ops->secure_heap(heap);
1677 else
1678 ret_val = -EINVAL;
1679 break;
1680 }
1681 mutex_unlock(&dev->lock);
1682 return ret_val;
1683}
1684EXPORT_SYMBOL(ion_secure_heap);
1685
1686int ion_unsecure_heap(struct ion_device *dev, int heap_id)
1687{
1688 struct rb_node *n;
1689 int ret_val = 0;
1690
1691 /*
1692 * traverse the list of heaps available in this system
1693 * and find the heap that is specified.
1694 */
1695 mutex_lock(&dev->lock);
1696 for (n = rb_first(&dev->heaps); n != NULL; n = rb_next(n)) {
1697 struct ion_heap *heap = rb_entry(n, struct ion_heap, node);
1698 if (heap->type != ION_HEAP_TYPE_CP)
1699 continue;
1700 if (ION_HEAP(heap->id) != heap_id)
1701 continue;
1702 if (heap->ops->secure_heap)
1703 ret_val = heap->ops->unsecure_heap(heap);
1704 else
1705 ret_val = -EINVAL;
1706 break;
1707 }
1708 mutex_unlock(&dev->lock);
1709 return ret_val;
1710}
1711EXPORT_SYMBOL(ion_unsecure_heap);
1712
Laura Abbott404f8242011-10-31 14:22:53 -07001713static int ion_debug_leak_show(struct seq_file *s, void *unused)
1714{
1715 struct ion_device *dev = s->private;
1716 struct rb_node *n;
1717 struct rb_node *n2;
1718
1719 /* mark all buffers as 1 */
1720 seq_printf(s, "%16.s %16.s %16.s %16.s\n", "buffer", "heap", "size",
1721 "ref cnt");
1722 mutex_lock(&dev->lock);
1723 for (n = rb_first(&dev->buffers); n; n = rb_next(n)) {
1724 struct ion_buffer *buf = rb_entry(n, struct ion_buffer,
1725 node);
1726
1727 buf->marked = 1;
1728 }
1729
1730 /* now see which buffers we can access */
1731 for (n = rb_first(&dev->kernel_clients); n; n = rb_next(n)) {
1732 struct ion_client *client = rb_entry(n, struct ion_client,
1733 node);
1734
1735 mutex_lock(&client->lock);
1736 for (n2 = rb_first(&client->handles); n2; n2 = rb_next(n2)) {
1737 struct ion_handle *handle = rb_entry(n2,
1738 struct ion_handle, node);
1739
1740 handle->buffer->marked = 0;
1741
1742 }
1743 mutex_unlock(&client->lock);
1744
1745 }
1746
1747 for (n = rb_first(&dev->user_clients); n; n = rb_next(n)) {
1748 struct ion_client *client = rb_entry(n, struct ion_client,
1749 node);
1750
1751 mutex_lock(&client->lock);
1752 for (n2 = rb_first(&client->handles); n2; n2 = rb_next(n2)) {
1753 struct ion_handle *handle = rb_entry(n2,
1754 struct ion_handle, node);
1755
1756 handle->buffer->marked = 0;
1757
1758 }
1759 mutex_unlock(&client->lock);
1760
1761 }
1762 /* And anyone still marked as a 1 means a leaked handle somewhere */
1763 for (n = rb_first(&dev->buffers); n; n = rb_next(n)) {
1764 struct ion_buffer *buf = rb_entry(n, struct ion_buffer,
1765 node);
1766
1767 if (buf->marked == 1)
1768 seq_printf(s, "%16.x %16.s %16.x %16.d\n",
1769 (int)buf, buf->heap->name, buf->size,
1770 atomic_read(&buf->ref.refcount));
1771 }
1772 mutex_unlock(&dev->lock);
1773 return 0;
1774}
1775
1776static int ion_debug_leak_open(struct inode *inode, struct file *file)
1777{
1778 return single_open(file, ion_debug_leak_show, inode->i_private);
1779}
1780
1781static const struct file_operations debug_leak_fops = {
1782 .open = ion_debug_leak_open,
1783 .read = seq_read,
1784 .llseek = seq_lseek,
1785 .release = single_release,
1786};
1787
1788
1789
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001790struct ion_device *ion_device_create(long (*custom_ioctl)
1791 (struct ion_client *client,
1792 unsigned int cmd,
1793 unsigned long arg))
1794{
1795 struct ion_device *idev;
1796 int ret;
1797
1798 idev = kzalloc(sizeof(struct ion_device), GFP_KERNEL);
1799 if (!idev)
1800 return ERR_PTR(-ENOMEM);
1801
1802 idev->dev.minor = MISC_DYNAMIC_MINOR;
1803 idev->dev.name = "ion";
1804 idev->dev.fops = &ion_fops;
1805 idev->dev.parent = NULL;
1806 ret = misc_register(&idev->dev);
1807 if (ret) {
1808 pr_err("ion: failed to register misc device.\n");
1809 return ERR_PTR(ret);
1810 }
1811
1812 idev->debug_root = debugfs_create_dir("ion", NULL);
1813 if (IS_ERR_OR_NULL(idev->debug_root))
1814 pr_err("ion: failed to create debug files.\n");
1815
1816 idev->custom_ioctl = custom_ioctl;
1817 idev->buffers = RB_ROOT;
1818 mutex_init(&idev->lock);
1819 idev->heaps = RB_ROOT;
1820 idev->user_clients = RB_ROOT;
1821 idev->kernel_clients = RB_ROOT;
Laura Abbott404f8242011-10-31 14:22:53 -07001822 debugfs_create_file("check_leaked_fds", 0664, idev->debug_root, idev,
1823 &debug_leak_fops);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001824 return idev;
1825}
1826
1827void ion_device_destroy(struct ion_device *dev)
1828{
1829 misc_deregister(&dev->dev);
1830 /* XXX need to free the heaps and clients ? */
1831 kfree(dev);
1832}