blob: ec93055a0d4c694dc5a0d2e98266388e34dfbd95 [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);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001318 mutex_unlock(&buffer->lock);
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 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001324
1325 vma->vm_ops = &ion_vm_ops;
1326 /* move the handle into the vm_private_data so we can access it from
1327 vma_open/close */
1328 vma->vm_private_data = handle;
1329 pr_debug("%s: %d client_cnt %d handle_cnt %d alloc_cnt %d\n",
1330 __func__, __LINE__,
1331 atomic_read(&client->ref.refcount),
1332 atomic_read(&handle->ref.refcount),
1333 atomic_read(&buffer->ref.refcount));
1334 return 0;
1335
Laura Abbott894fd582011-08-19 13:33:56 -07001336err2:
1337 buffer->umap_cnt--;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001338 /* drop the reference to the handle */
Laura Abbott894fd582011-08-19 13:33:56 -07001339err1:
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001340 ion_handle_put(handle);
1341err:
Iliyan Malchev3fe24362011-08-09 14:42:08 -07001342 /* drop the reference to the client */
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001343 ion_client_put(client);
1344 return ret;
1345}
1346
1347static const struct file_operations ion_share_fops = {
1348 .owner = THIS_MODULE,
1349 .release = ion_share_release,
1350 .mmap = ion_share_mmap,
1351};
1352
1353static int ion_ioctl_share(struct file *parent, struct ion_client *client,
1354 struct ion_handle *handle)
1355{
1356 int fd = get_unused_fd();
1357 struct file *file;
1358
1359 if (fd < 0)
1360 return -ENFILE;
1361
1362 file = anon_inode_getfile("ion_share_fd", &ion_share_fops,
1363 handle->buffer, O_RDWR);
1364 if (IS_ERR_OR_NULL(file))
1365 goto err;
Laura Abbott4b5d0482011-09-27 18:35:14 -07001366
1367 if (parent->f_flags & O_DSYNC)
1368 file->f_flags |= O_DSYNC;
1369
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001370 ion_buffer_get(handle->buffer);
1371 fd_install(fd, file);
1372
1373 return fd;
1374
1375err:
1376 put_unused_fd(fd);
1377 return -ENFILE;
1378}
1379
1380static long ion_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1381{
1382 struct ion_client *client = filp->private_data;
1383
1384 switch (cmd) {
1385 case ION_IOC_ALLOC:
1386 {
1387 struct ion_allocation_data data;
1388
1389 if (copy_from_user(&data, (void __user *)arg, sizeof(data)))
1390 return -EFAULT;
1391 data.handle = ion_alloc(client, data.len, data.align,
1392 data.flags);
Laura Abbotte1b9ce52011-11-11 18:31:39 -08001393
1394 if (IS_ERR_OR_NULL(data.handle))
Olav Hauganb06ee072011-12-13 15:31:41 -08001395 return -ENOMEM;
Laura Abbotte1b9ce52011-11-11 18:31:39 -08001396
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001397 if (copy_to_user((void __user *)arg, &data, sizeof(data)))
1398 return -EFAULT;
1399 break;
1400 }
1401 case ION_IOC_FREE:
1402 {
1403 struct ion_handle_data data;
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001404 bool valid;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001405
1406 if (copy_from_user(&data, (void __user *)arg,
1407 sizeof(struct ion_handle_data)))
1408 return -EFAULT;
1409 mutex_lock(&client->lock);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001410 valid = ion_handle_validate(client, data.handle);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001411 mutex_unlock(&client->lock);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001412 if (!valid)
1413 return -EINVAL;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001414 ion_free(client, data.handle);
1415 break;
1416 }
1417 case ION_IOC_MAP:
1418 case ION_IOC_SHARE:
1419 {
1420 struct ion_fd_data data;
1421
1422 if (copy_from_user(&data, (void __user *)arg, sizeof(data)))
1423 return -EFAULT;
1424 mutex_lock(&client->lock);
1425 if (!ion_handle_validate(client, data.handle)) {
1426 pr_err("%s: invalid handle passed to share ioctl.\n",
1427 __func__);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001428 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001429 return -EINVAL;
1430 }
1431 data.fd = ion_ioctl_share(filp, client, data.handle);
1432 mutex_unlock(&client->lock);
1433 if (copy_to_user((void __user *)arg, &data, sizeof(data)))
1434 return -EFAULT;
1435 break;
1436 }
1437 case ION_IOC_IMPORT:
1438 {
1439 struct ion_fd_data data;
1440 if (copy_from_user(&data, (void __user *)arg,
1441 sizeof(struct ion_fd_data)))
1442 return -EFAULT;
1443
1444 data.handle = ion_import_fd(client, data.fd);
1445 if (IS_ERR(data.handle))
1446 data.handle = NULL;
1447 if (copy_to_user((void __user *)arg, &data,
1448 sizeof(struct ion_fd_data)))
1449 return -EFAULT;
1450 break;
1451 }
1452 case ION_IOC_CUSTOM:
1453 {
1454 struct ion_device *dev = client->dev;
1455 struct ion_custom_data data;
1456
1457 if (!dev->custom_ioctl)
1458 return -ENOTTY;
1459 if (copy_from_user(&data, (void __user *)arg,
1460 sizeof(struct ion_custom_data)))
1461 return -EFAULT;
1462 return dev->custom_ioctl(client, data.cmd, data.arg);
1463 }
Laura Abbottabcb6f72011-10-04 16:26:49 -07001464 case ION_IOC_CLEAN_CACHES:
1465 case ION_IOC_INV_CACHES:
1466 case ION_IOC_CLEAN_INV_CACHES:
1467 {
1468 struct ion_flush_data data;
Laura Abbott9fa29e82011-11-14 09:42:53 -08001469 unsigned long start, end;
Laura Abbotte80ea012011-11-18 18:36:47 -08001470 struct ion_handle *handle = NULL;
1471 int ret;
Laura Abbottabcb6f72011-10-04 16:26:49 -07001472
1473 if (copy_from_user(&data, (void __user *)arg,
1474 sizeof(struct ion_flush_data)))
1475 return -EFAULT;
1476
Laura Abbott9fa29e82011-11-14 09:42:53 -08001477 start = (unsigned long) data.vaddr;
1478 end = (unsigned long) data.vaddr + data.length;
1479
1480 if (check_vaddr_bounds(start, end)) {
1481 pr_err("%s: virtual address %p is out of bounds\n",
1482 __func__, data.vaddr);
1483 return -EINVAL;
1484 }
1485
Laura Abbotte80ea012011-11-18 18:36:47 -08001486 if (!data.handle) {
1487 handle = ion_import_fd(client, data.fd);
1488 if (IS_ERR_OR_NULL(handle)) {
1489 pr_info("%s: Could not import handle: %d\n",
1490 __func__, (int)handle);
1491 return -EINVAL;
1492 }
1493 }
1494
1495 ret = ion_do_cache_op(client,
1496 data.handle ? data.handle : handle,
1497 data.vaddr, data.offset, data.length,
1498 cmd);
1499
1500 if (!data.handle)
1501 ion_free(client, handle);
1502
1503 break;
Laura Abbottabcb6f72011-10-04 16:26:49 -07001504
1505 }
Laura Abbott273dd8e2011-10-12 14:26:33 -07001506 case ION_IOC_GET_FLAGS:
1507 {
1508 struct ion_flag_data data;
1509 int ret;
1510 if (copy_from_user(&data, (void __user *)arg,
1511 sizeof(struct ion_flag_data)))
1512 return -EFAULT;
1513
1514 ret = ion_handle_get_flags(client, data.handle, &data.flags);
1515 if (ret < 0)
1516 return ret;
1517 if (copy_to_user((void __user *)arg, &data,
1518 sizeof(struct ion_flag_data)))
1519 return -EFAULT;
1520 break;
1521 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001522 default:
1523 return -ENOTTY;
1524 }
1525 return 0;
1526}
1527
1528static int ion_release(struct inode *inode, struct file *file)
1529{
1530 struct ion_client *client = file->private_data;
1531
1532 pr_debug("%s: %d\n", __func__, __LINE__);
1533 ion_client_put(client);
1534 return 0;
1535}
1536
1537static int ion_open(struct inode *inode, struct file *file)
1538{
1539 struct miscdevice *miscdev = file->private_data;
1540 struct ion_device *dev = container_of(miscdev, struct ion_device, dev);
1541 struct ion_client *client;
Laura Abbotteed86032011-12-05 15:32:36 -08001542 char debug_name[64];
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001543
1544 pr_debug("%s: %d\n", __func__, __LINE__);
Laura Abbotteed86032011-12-05 15:32:36 -08001545 snprintf(debug_name, 64, "%u", task_pid_nr(current->group_leader));
1546 client = ion_client_create(dev, -1, debug_name);
Rebecca Schultz Zavin6d3b9582011-07-06 18:07:24 -07001547 if (IS_ERR_OR_NULL(client))
1548 return PTR_ERR(client);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001549 file->private_data = client;
1550
1551 return 0;
1552}
1553
1554static const struct file_operations ion_fops = {
1555 .owner = THIS_MODULE,
1556 .open = ion_open,
1557 .release = ion_release,
1558 .unlocked_ioctl = ion_ioctl,
1559};
1560
1561static size_t ion_debug_heap_total(struct ion_client *client,
Laura Abbott3647ac32011-10-31 14:09:53 -07001562 enum ion_heap_ids id)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001563{
1564 size_t size = 0;
1565 struct rb_node *n;
1566
1567 mutex_lock(&client->lock);
1568 for (n = rb_first(&client->handles); n; n = rb_next(n)) {
1569 struct ion_handle *handle = rb_entry(n,
1570 struct ion_handle,
1571 node);
Laura Abbott3647ac32011-10-31 14:09:53 -07001572 if (handle->buffer->heap->id == id)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001573 size += handle->buffer->size;
1574 }
1575 mutex_unlock(&client->lock);
1576 return size;
1577}
1578
1579static int ion_debug_heap_show(struct seq_file *s, void *unused)
1580{
1581 struct ion_heap *heap = s->private;
1582 struct ion_device *dev = heap->dev;
1583 struct rb_node *n;
1584
1585 seq_printf(s, "%16.s %16.s %16.s\n", "client", "pid", "size");
1586 for (n = rb_first(&dev->user_clients); n; n = rb_next(n)) {
1587 struct ion_client *client = rb_entry(n, struct ion_client,
1588 node);
1589 char task_comm[TASK_COMM_LEN];
Laura Abbott3647ac32011-10-31 14:09:53 -07001590 size_t size = ion_debug_heap_total(client, heap->id);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001591 if (!size)
1592 continue;
1593
1594 get_task_comm(task_comm, client->task);
Laura Abbott8747bbe2011-10-31 14:18:13 -07001595 seq_printf(s, "%16.s %16u %16x\n", task_comm, client->pid,
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001596 size);
1597 }
1598
1599 for (n = rb_first(&dev->kernel_clients); n; n = rb_next(n)) {
1600 struct ion_client *client = rb_entry(n, struct ion_client,
1601 node);
Laura Abbott3647ac32011-10-31 14:09:53 -07001602 size_t size = ion_debug_heap_total(client, heap->id);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001603 if (!size)
1604 continue;
Laura Abbott8747bbe2011-10-31 14:18:13 -07001605 seq_printf(s, "%16.s %16u %16x\n", client->name, client->pid,
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001606 size);
1607 }
Laura Abbott68c80642011-10-21 17:32:27 -07001608 if (heap->ops->get_allocated) {
1609 seq_printf(s, "total bytes currently allocated: %lx\n",
1610 heap->ops->get_allocated(heap));
1611 }
1612 if (heap->ops->get_total) {
1613 seq_printf(s, "total heap size: %lx\n",
1614 heap->ops->get_total(heap));
1615 }
Olav Haugane1f5d832011-12-13 15:16:28 -08001616 if (heap->ops->get_alloc_cnt) {
1617 seq_printf(s, "allocation count: %lx\n",
1618 heap->ops->get_alloc_cnt(heap));
1619 }
1620 if (heap->ops->get_umap_cnt) {
1621 seq_printf(s, "umapping count: %lx\n",
1622 heap->ops->get_umap_cnt(heap));
1623 }
1624 if (heap->ops->get_kmap_cnt) {
1625 seq_printf(s, "kmapping count: %lx\n",
1626 heap->ops->get_kmap_cnt(heap));
1627 }
1628 if (heap->ops->get_secured) {
1629 seq_printf(s, "secured heap: %s\n",
1630 heap->ops->get_secured(heap) ? "Yes" : "No");
1631 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001632 return 0;
1633}
1634
1635static int ion_debug_heap_open(struct inode *inode, struct file *file)
1636{
1637 return single_open(file, ion_debug_heap_show, inode->i_private);
1638}
1639
1640static const struct file_operations debug_heap_fops = {
1641 .open = ion_debug_heap_open,
1642 .read = seq_read,
1643 .llseek = seq_lseek,
1644 .release = single_release,
1645};
1646
1647void ion_device_add_heap(struct ion_device *dev, struct ion_heap *heap)
1648{
1649 struct rb_node **p = &dev->heaps.rb_node;
1650 struct rb_node *parent = NULL;
1651 struct ion_heap *entry;
1652
1653 heap->dev = dev;
1654 mutex_lock(&dev->lock);
1655 while (*p) {
1656 parent = *p;
1657 entry = rb_entry(parent, struct ion_heap, node);
1658
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001659 if (heap->id < entry->id) {
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001660 p = &(*p)->rb_left;
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001661 } else if (heap->id > entry->id ) {
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001662 p = &(*p)->rb_right;
1663 } else {
1664 pr_err("%s: can not insert multiple heaps with "
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001665 "id %d\n", __func__, heap->id);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001666 goto end;
1667 }
1668 }
1669
1670 rb_link_node(&heap->node, parent, p);
1671 rb_insert_color(&heap->node, &dev->heaps);
1672 debugfs_create_file(heap->name, 0664, dev->debug_root, heap,
1673 &debug_heap_fops);
1674end:
1675 mutex_unlock(&dev->lock);
1676}
1677
Olav Haugan0a852512012-01-09 10:20:55 -08001678int ion_secure_heap(struct ion_device *dev, int heap_id)
1679{
1680 struct rb_node *n;
1681 int ret_val = 0;
1682
1683 /*
1684 * traverse the list of heaps available in this system
1685 * and find the heap that is specified.
1686 */
1687 mutex_lock(&dev->lock);
1688 for (n = rb_first(&dev->heaps); n != NULL; n = rb_next(n)) {
1689 struct ion_heap *heap = rb_entry(n, struct ion_heap, node);
1690 if (heap->type != ION_HEAP_TYPE_CP)
1691 continue;
1692 if (ION_HEAP(heap->id) != heap_id)
1693 continue;
1694 if (heap->ops->secure_heap)
1695 ret_val = heap->ops->secure_heap(heap);
1696 else
1697 ret_val = -EINVAL;
1698 break;
1699 }
1700 mutex_unlock(&dev->lock);
1701 return ret_val;
1702}
1703EXPORT_SYMBOL(ion_secure_heap);
1704
1705int ion_unsecure_heap(struct ion_device *dev, int heap_id)
1706{
1707 struct rb_node *n;
1708 int ret_val = 0;
1709
1710 /*
1711 * traverse the list of heaps available in this system
1712 * and find the heap that is specified.
1713 */
1714 mutex_lock(&dev->lock);
1715 for (n = rb_first(&dev->heaps); n != NULL; n = rb_next(n)) {
1716 struct ion_heap *heap = rb_entry(n, struct ion_heap, node);
1717 if (heap->type != ION_HEAP_TYPE_CP)
1718 continue;
1719 if (ION_HEAP(heap->id) != heap_id)
1720 continue;
1721 if (heap->ops->secure_heap)
1722 ret_val = heap->ops->unsecure_heap(heap);
1723 else
1724 ret_val = -EINVAL;
1725 break;
1726 }
1727 mutex_unlock(&dev->lock);
1728 return ret_val;
1729}
1730EXPORT_SYMBOL(ion_unsecure_heap);
1731
Laura Abbott404f8242011-10-31 14:22:53 -07001732static int ion_debug_leak_show(struct seq_file *s, void *unused)
1733{
1734 struct ion_device *dev = s->private;
1735 struct rb_node *n;
1736 struct rb_node *n2;
1737
1738 /* mark all buffers as 1 */
1739 seq_printf(s, "%16.s %16.s %16.s %16.s\n", "buffer", "heap", "size",
1740 "ref cnt");
1741 mutex_lock(&dev->lock);
1742 for (n = rb_first(&dev->buffers); n; n = rb_next(n)) {
1743 struct ion_buffer *buf = rb_entry(n, struct ion_buffer,
1744 node);
1745
1746 buf->marked = 1;
1747 }
1748
1749 /* now see which buffers we can access */
1750 for (n = rb_first(&dev->kernel_clients); n; n = rb_next(n)) {
1751 struct ion_client *client = rb_entry(n, struct ion_client,
1752 node);
1753
1754 mutex_lock(&client->lock);
1755 for (n2 = rb_first(&client->handles); n2; n2 = rb_next(n2)) {
1756 struct ion_handle *handle = rb_entry(n2,
1757 struct ion_handle, node);
1758
1759 handle->buffer->marked = 0;
1760
1761 }
1762 mutex_unlock(&client->lock);
1763
1764 }
1765
1766 for (n = rb_first(&dev->user_clients); n; n = rb_next(n)) {
1767 struct ion_client *client = rb_entry(n, struct ion_client,
1768 node);
1769
1770 mutex_lock(&client->lock);
1771 for (n2 = rb_first(&client->handles); n2; n2 = rb_next(n2)) {
1772 struct ion_handle *handle = rb_entry(n2,
1773 struct ion_handle, node);
1774
1775 handle->buffer->marked = 0;
1776
1777 }
1778 mutex_unlock(&client->lock);
1779
1780 }
1781 /* And anyone still marked as a 1 means a leaked handle somewhere */
1782 for (n = rb_first(&dev->buffers); n; n = rb_next(n)) {
1783 struct ion_buffer *buf = rb_entry(n, struct ion_buffer,
1784 node);
1785
1786 if (buf->marked == 1)
1787 seq_printf(s, "%16.x %16.s %16.x %16.d\n",
1788 (int)buf, buf->heap->name, buf->size,
1789 atomic_read(&buf->ref.refcount));
1790 }
1791 mutex_unlock(&dev->lock);
1792 return 0;
1793}
1794
1795static int ion_debug_leak_open(struct inode *inode, struct file *file)
1796{
1797 return single_open(file, ion_debug_leak_show, inode->i_private);
1798}
1799
1800static const struct file_operations debug_leak_fops = {
1801 .open = ion_debug_leak_open,
1802 .read = seq_read,
1803 .llseek = seq_lseek,
1804 .release = single_release,
1805};
1806
1807
1808
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001809struct ion_device *ion_device_create(long (*custom_ioctl)
1810 (struct ion_client *client,
1811 unsigned int cmd,
1812 unsigned long arg))
1813{
1814 struct ion_device *idev;
1815 int ret;
1816
1817 idev = kzalloc(sizeof(struct ion_device), GFP_KERNEL);
1818 if (!idev)
1819 return ERR_PTR(-ENOMEM);
1820
1821 idev->dev.minor = MISC_DYNAMIC_MINOR;
1822 idev->dev.name = "ion";
1823 idev->dev.fops = &ion_fops;
1824 idev->dev.parent = NULL;
1825 ret = misc_register(&idev->dev);
1826 if (ret) {
1827 pr_err("ion: failed to register misc device.\n");
1828 return ERR_PTR(ret);
1829 }
1830
1831 idev->debug_root = debugfs_create_dir("ion", NULL);
1832 if (IS_ERR_OR_NULL(idev->debug_root))
1833 pr_err("ion: failed to create debug files.\n");
1834
1835 idev->custom_ioctl = custom_ioctl;
1836 idev->buffers = RB_ROOT;
1837 mutex_init(&idev->lock);
1838 idev->heaps = RB_ROOT;
1839 idev->user_clients = RB_ROOT;
1840 idev->kernel_clients = RB_ROOT;
Laura Abbott404f8242011-10-31 14:22:53 -07001841 debugfs_create_file("check_leaked_fds", 0664, idev->debug_root, idev,
1842 &debug_leak_fops);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001843 return idev;
1844}
1845
1846void ion_device_destroy(struct ion_device *dev)
1847{
1848 misc_deregister(&dev->dev);
1849 /* XXX need to free the heaps and clients ? */
1850 kfree(dev);
1851}