blob: 940af32bf968ffa14d112eefc523477c2e2ee355 [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
Olav Hauganb3676592012-03-02 15:02:25 -0800110static void ion_iommu_release(struct kref *kref);
111
Laura Abbott8c017362011-09-22 20:59:12 -0700112static int ion_validate_buffer_flags(struct ion_buffer *buffer,
113 unsigned long flags)
114{
115 if (buffer->kmap_cnt || buffer->dmap_cnt || buffer->umap_cnt ||
116 buffer->iommu_map_cnt) {
117 if (buffer->flags != flags) {
118 pr_err("%s: buffer was already mapped with flags %lx,"
119 " cannot map with flags %lx\n", __func__,
120 buffer->flags, flags);
121 return 1;
122 }
123
124 } else {
125 buffer->flags = flags;
126 }
127 return 0;
128}
129
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700130/* this function should only be called while dev->lock is held */
131static void ion_buffer_add(struct ion_device *dev,
132 struct ion_buffer *buffer)
133{
134 struct rb_node **p = &dev->buffers.rb_node;
135 struct rb_node *parent = NULL;
136 struct ion_buffer *entry;
137
138 while (*p) {
139 parent = *p;
140 entry = rb_entry(parent, struct ion_buffer, node);
141
Rebecca Schultz Zavinf9fb95e2011-06-30 18:09:05 -0700142 if (buffer < entry) {
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700143 p = &(*p)->rb_left;
Rebecca Schultz Zavinf9fb95e2011-06-30 18:09:05 -0700144 } else if (buffer > entry) {
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700145 p = &(*p)->rb_right;
Rebecca Schultz Zavinf9fb95e2011-06-30 18:09:05 -0700146 } else {
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700147 pr_err("%s: buffer already found.", __func__);
148 BUG();
Rebecca Schultz Zavinf9fb95e2011-06-30 18:09:05 -0700149 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700150 }
151
152 rb_link_node(&buffer->node, parent, p);
153 rb_insert_color(&buffer->node, &dev->buffers);
154}
155
Olav Haugan0fa9b602012-01-25 11:50:38 -0800156static void ion_iommu_add(struct ion_buffer *buffer,
Laura Abbott8c017362011-09-22 20:59:12 -0700157 struct ion_iommu_map *iommu)
158{
159 struct rb_node **p = &buffer->iommu_maps.rb_node;
160 struct rb_node *parent = NULL;
161 struct ion_iommu_map *entry;
162
163 while (*p) {
164 parent = *p;
165 entry = rb_entry(parent, struct ion_iommu_map, node);
166
167 if (iommu->key < entry->key) {
168 p = &(*p)->rb_left;
169 } else if (iommu->key > entry->key) {
170 p = &(*p)->rb_right;
171 } else {
172 pr_err("%s: buffer %p already has mapping for domain %d"
173 " and partition %d\n", __func__,
174 buffer,
175 iommu_map_domain(iommu),
176 iommu_map_partition(iommu));
177 BUG();
178 }
179 }
180
181 rb_link_node(&iommu->node, parent, p);
182 rb_insert_color(&iommu->node, &buffer->iommu_maps);
183
184}
185
186static struct ion_iommu_map *ion_iommu_lookup(struct ion_buffer *buffer,
187 unsigned int domain_no,
188 unsigned int partition_no)
189{
190 struct rb_node **p = &buffer->iommu_maps.rb_node;
191 struct rb_node *parent = NULL;
192 struct ion_iommu_map *entry;
193 uint64_t key = domain_no;
194 key = key << 32 | partition_no;
195
196 while (*p) {
197 parent = *p;
198 entry = rb_entry(parent, struct ion_iommu_map, node);
199
200 if (key < entry->key)
201 p = &(*p)->rb_left;
202 else if (key > entry->key)
203 p = &(*p)->rb_right;
204 else
205 return entry;
206 }
207
208 return NULL;
209}
210
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700211/* this function should only be called while dev->lock is held */
Iliyan Malchev3fe24362011-08-09 14:42:08 -0700212static struct ion_buffer *ion_buffer_create(struct ion_heap *heap,
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700213 struct ion_device *dev,
214 unsigned long len,
215 unsigned long align,
216 unsigned long flags)
217{
218 struct ion_buffer *buffer;
219 int ret;
220
221 buffer = kzalloc(sizeof(struct ion_buffer), GFP_KERNEL);
222 if (!buffer)
223 return ERR_PTR(-ENOMEM);
224
225 buffer->heap = heap;
226 kref_init(&buffer->ref);
227
228 ret = heap->ops->allocate(heap, buffer, len, align, flags);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700229 if (ret) {
230 kfree(buffer);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700231 return ERR_PTR(ret);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700232 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700233 buffer->dev = dev;
234 buffer->size = len;
235 mutex_init(&buffer->lock);
236 ion_buffer_add(dev, buffer);
237 return buffer;
238}
239
Olav Hauganb3676592012-03-02 15:02:25 -0800240/**
241 * Check for delayed IOMMU unmapping. Also unmap any outstanding
242 * mappings which would otherwise have been leaked.
243 */
244static void ion_iommu_delayed_unmap(struct ion_buffer *buffer)
245{
246 struct ion_iommu_map *iommu_map;
247 struct rb_node *node;
248 const struct rb_root *rb = &(buffer->iommu_maps);
249 unsigned long ref_count;
250 unsigned int delayed_unmap;
251
252 mutex_lock(&buffer->lock);
253
254 while ((node = rb_first(rb)) != 0) {
255 iommu_map = rb_entry(node, struct ion_iommu_map, node);
256 ref_count = atomic_read(&iommu_map->ref.refcount);
257 delayed_unmap = iommu_map->flags & ION_IOMMU_UNMAP_DELAYED;
258
259 if ((delayed_unmap && ref_count > 1) || !delayed_unmap) {
260 pr_err("%s: Virtual memory address leak in domain %u, partition %u\n",
261 __func__, iommu_map->domain_info[DI_DOMAIN_NUM],
262 iommu_map->domain_info[DI_PARTITION_NUM]);
263 }
264 /* set ref count to 1 to force release */
265 kref_init(&iommu_map->ref);
266 kref_put(&iommu_map->ref, ion_iommu_release);
267 }
268
269 mutex_unlock(&buffer->lock);
270}
271
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700272static void ion_buffer_destroy(struct kref *kref)
273{
274 struct ion_buffer *buffer = container_of(kref, struct ion_buffer, ref);
275 struct ion_device *dev = buffer->dev;
276
Olav Hauganb3676592012-03-02 15:02:25 -0800277 ion_iommu_delayed_unmap(buffer);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700278 buffer->heap->ops->free(buffer);
279 mutex_lock(&dev->lock);
280 rb_erase(&buffer->node, &dev->buffers);
281 mutex_unlock(&dev->lock);
282 kfree(buffer);
283}
284
285static void ion_buffer_get(struct ion_buffer *buffer)
286{
287 kref_get(&buffer->ref);
288}
289
290static int ion_buffer_put(struct ion_buffer *buffer)
291{
292 return kref_put(&buffer->ref, ion_buffer_destroy);
293}
294
Iliyan Malchev3fe24362011-08-09 14:42:08 -0700295static struct ion_handle *ion_handle_create(struct ion_client *client,
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700296 struct ion_buffer *buffer)
297{
298 struct ion_handle *handle;
299
300 handle = kzalloc(sizeof(struct ion_handle), GFP_KERNEL);
301 if (!handle)
302 return ERR_PTR(-ENOMEM);
303 kref_init(&handle->ref);
Iliyan Malchev3fe24362011-08-09 14:42:08 -0700304 rb_init_node(&handle->node);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700305 handle->client = client;
306 ion_buffer_get(buffer);
307 handle->buffer = buffer;
308
309 return handle;
310}
311
Laura Abbottec149ff2012-01-26 13:33:11 -0800312/* Client lock must be locked when calling */
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700313static void ion_handle_destroy(struct kref *kref)
314{
315 struct ion_handle *handle = container_of(kref, struct ion_handle, ref);
316 /* XXX Can a handle be destroyed while it's map count is non-zero?:
317 if (handle->map_cnt) unmap
318 */
Laura Abbottd2a87372011-10-20 17:53:49 -0700319 WARN_ON(handle->kmap_cnt || handle->dmap_cnt || handle->usermap_cnt);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700320 ion_buffer_put(handle->buffer);
Iliyan Malchev3fe24362011-08-09 14:42:08 -0700321 if (!RB_EMPTY_NODE(&handle->node))
322 rb_erase(&handle->node, &handle->client->handles);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700323 kfree(handle);
324}
325
326struct ion_buffer *ion_handle_buffer(struct ion_handle *handle)
327{
328 return handle->buffer;
329}
330
331static void ion_handle_get(struct ion_handle *handle)
332{
333 kref_get(&handle->ref);
334}
335
336static int ion_handle_put(struct ion_handle *handle)
337{
338 return kref_put(&handle->ref, ion_handle_destroy);
339}
340
341static struct ion_handle *ion_handle_lookup(struct ion_client *client,
342 struct ion_buffer *buffer)
343{
344 struct rb_node *n;
345
346 for (n = rb_first(&client->handles); n; n = rb_next(n)) {
347 struct ion_handle *handle = rb_entry(n, struct ion_handle,
348 node);
349 if (handle->buffer == buffer)
350 return handle;
351 }
352 return NULL;
353}
354
Iliyan Malchev3fe24362011-08-09 14:42:08 -0700355static bool ion_handle_validate(struct ion_client *client, struct ion_handle *handle)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700356{
357 struct rb_node *n = client->handles.rb_node;
358
359 while (n) {
360 struct ion_handle *handle_node = rb_entry(n, struct ion_handle,
361 node);
362 if (handle < handle_node)
363 n = n->rb_left;
364 else if (handle > handle_node)
365 n = n->rb_right;
366 else
367 return true;
368 }
369 return false;
370}
371
372static void ion_handle_add(struct ion_client *client, struct ion_handle *handle)
373{
374 struct rb_node **p = &client->handles.rb_node;
375 struct rb_node *parent = NULL;
376 struct ion_handle *entry;
377
378 while (*p) {
379 parent = *p;
380 entry = rb_entry(parent, struct ion_handle, node);
381
382 if (handle < entry)
383 p = &(*p)->rb_left;
384 else if (handle > entry)
385 p = &(*p)->rb_right;
386 else
387 WARN(1, "%s: buffer already found.", __func__);
388 }
389
390 rb_link_node(&handle->node, parent, p);
391 rb_insert_color(&handle->node, &client->handles);
392}
393
394struct ion_handle *ion_alloc(struct ion_client *client, size_t len,
395 size_t align, unsigned int flags)
396{
397 struct rb_node *n;
398 struct ion_handle *handle;
399 struct ion_device *dev = client->dev;
400 struct ion_buffer *buffer = NULL;
Olav Haugan0a852512012-01-09 10:20:55 -0800401 unsigned long secure_allocation = flags & ION_SECURE;
Olav Haugan35e2f2f2012-01-11 17:31:47 -0800402 const unsigned int MAX_DBG_STR_LEN = 64;
403 char dbg_str[MAX_DBG_STR_LEN];
404 unsigned int dbg_str_idx = 0;
405
406 dbg_str[0] = '\0';
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700407
408 /*
409 * traverse the list of heaps available in this system in priority
410 * order. If the heap type is supported by the client, and matches the
411 * request of the caller allocate from it. Repeat until allocate has
412 * succeeded or all heaps have been tried
413 */
414 mutex_lock(&dev->lock);
415 for (n = rb_first(&dev->heaps); n != NULL; n = rb_next(n)) {
416 struct ion_heap *heap = rb_entry(n, struct ion_heap, node);
417 /* if the client doesn't support this heap type */
418 if (!((1 << heap->type) & client->heap_mask))
419 continue;
420 /* if the caller didn't specify this heap type */
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700421 if (!((1 << heap->id) & flags))
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700422 continue;
Olav Haugan0a852512012-01-09 10:20:55 -0800423 /* Do not allow un-secure heap if secure is specified */
424 if (secure_allocation && (heap->type != ION_HEAP_TYPE_CP))
425 continue;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700426 buffer = ion_buffer_create(heap, dev, len, align, flags);
427 if (!IS_ERR_OR_NULL(buffer))
428 break;
Olav Haugan35e2f2f2012-01-11 17:31:47 -0800429 if (dbg_str_idx < MAX_DBG_STR_LEN) {
430 unsigned int len_left = MAX_DBG_STR_LEN-dbg_str_idx-1;
431 int ret_value = snprintf(&dbg_str[dbg_str_idx],
432 len_left, "%s ", heap->name);
433 if (ret_value >= len_left) {
434 /* overflow */
435 dbg_str[MAX_DBG_STR_LEN-1] = '\0';
436 dbg_str_idx = MAX_DBG_STR_LEN;
437 } else if (ret_value >= 0) {
438 dbg_str_idx += ret_value;
439 } else {
440 /* error */
441 dbg_str[MAX_DBG_STR_LEN-1] = '\0';
442 }
443 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700444 }
445 mutex_unlock(&dev->lock);
446
Olav Haugan35e2f2f2012-01-11 17:31:47 -0800447 if (IS_ERR_OR_NULL(buffer)) {
448 pr_debug("ION is unable to allocate 0x%x bytes (alignment: "
449 "0x%x) from heap(s) %sfor client %s with heap "
450 "mask 0x%x\n",
451 len, align, dbg_str, client->name, client->heap_mask);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700452 return ERR_PTR(PTR_ERR(buffer));
Olav Haugan35e2f2f2012-01-11 17:31:47 -0800453 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700454
455 handle = ion_handle_create(client, buffer);
456
457 if (IS_ERR_OR_NULL(handle))
458 goto end;
459
460 /*
461 * ion_buffer_create will create a buffer with a ref_cnt of 1,
462 * and ion_handle_create will take a second reference, drop one here
463 */
464 ion_buffer_put(buffer);
465
466 mutex_lock(&client->lock);
467 ion_handle_add(client, handle);
468 mutex_unlock(&client->lock);
469 return handle;
470
471end:
472 ion_buffer_put(buffer);
473 return handle;
474}
Olav Hauganbd2b6922012-01-25 09:28:55 -0800475EXPORT_SYMBOL(ion_alloc);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700476
477void ion_free(struct ion_client *client, struct ion_handle *handle)
478{
Rebecca Schultz Zavinc72866d2011-07-07 17:07:56 -0700479 bool valid_handle;
480
481 BUG_ON(client != handle->client);
482
483 mutex_lock(&client->lock);
484 valid_handle = ion_handle_validate(client, handle);
Rebecca Schultz Zavinc72866d2011-07-07 17:07:56 -0700485 if (!valid_handle) {
Laura Abbottec149ff2012-01-26 13:33:11 -0800486 mutex_unlock(&client->lock);
Olav Haugan6ede5672012-04-19 10:20:22 -0700487 WARN(1, "%s: invalid handle passed to free.\n", __func__);
Rebecca Schultz Zavinc72866d2011-07-07 17:07:56 -0700488 return;
489 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700490 ion_handle_put(handle);
Laura Abbottec149ff2012-01-26 13:33:11 -0800491 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700492}
Olav Hauganbd2b6922012-01-25 09:28:55 -0800493EXPORT_SYMBOL(ion_free);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700494
495static void ion_client_get(struct ion_client *client);
496static int ion_client_put(struct ion_client *client);
497
Iliyan Malchev3fe24362011-08-09 14:42:08 -0700498static bool _ion_map(int *buffer_cnt, int *handle_cnt)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700499{
500 bool map;
501
502 BUG_ON(*handle_cnt != 0 && *buffer_cnt == 0);
503
504 if (*buffer_cnt)
505 map = false;
506 else
507 map = true;
508 if (*handle_cnt == 0)
509 (*buffer_cnt)++;
510 (*handle_cnt)++;
511 return map;
512}
513
Iliyan Malchev3fe24362011-08-09 14:42:08 -0700514static bool _ion_unmap(int *buffer_cnt, int *handle_cnt)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700515{
516 BUG_ON(*handle_cnt == 0);
517 (*handle_cnt)--;
518 if (*handle_cnt != 0)
519 return false;
520 BUG_ON(*buffer_cnt == 0);
521 (*buffer_cnt)--;
522 if (*buffer_cnt == 0)
523 return true;
524 return false;
525}
526
527int ion_phys(struct ion_client *client, struct ion_handle *handle,
528 ion_phys_addr_t *addr, size_t *len)
529{
530 struct ion_buffer *buffer;
531 int ret;
532
533 mutex_lock(&client->lock);
534 if (!ion_handle_validate(client, handle)) {
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700535 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700536 return -EINVAL;
537 }
538
539 buffer = handle->buffer;
540
541 if (!buffer->heap->ops->phys) {
542 pr_err("%s: ion_phys is not implemented by this heap.\n",
543 __func__);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700544 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700545 return -ENODEV;
546 }
547 mutex_unlock(&client->lock);
548 ret = buffer->heap->ops->phys(buffer->heap, buffer, addr, len);
549 return ret;
550}
Olav Hauganbd2b6922012-01-25 09:28:55 -0800551EXPORT_SYMBOL(ion_phys);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700552
Laura Abbott894fd582011-08-19 13:33:56 -0700553void *ion_map_kernel(struct ion_client *client, struct ion_handle *handle,
554 unsigned long flags)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700555{
556 struct ion_buffer *buffer;
557 void *vaddr;
558
559 mutex_lock(&client->lock);
560 if (!ion_handle_validate(client, handle)) {
561 pr_err("%s: invalid handle passed to map_kernel.\n",
562 __func__);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700563 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700564 return ERR_PTR(-EINVAL);
565 }
566
567 buffer = handle->buffer;
568 mutex_lock(&buffer->lock);
569
570 if (!handle->buffer->heap->ops->map_kernel) {
571 pr_err("%s: map_kernel is not implemented by this heap.\n",
572 __func__);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700573 mutex_unlock(&buffer->lock);
574 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700575 return ERR_PTR(-ENODEV);
576 }
577
Laura Abbott8c017362011-09-22 20:59:12 -0700578 if (ion_validate_buffer_flags(buffer, flags)) {
Laura Abbott894fd582011-08-19 13:33:56 -0700579 vaddr = ERR_PTR(-EEXIST);
580 goto out;
Laura Abbott894fd582011-08-19 13:33:56 -0700581 }
582
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700583 if (_ion_map(&buffer->kmap_cnt, &handle->kmap_cnt)) {
Laura Abbott894fd582011-08-19 13:33:56 -0700584 vaddr = buffer->heap->ops->map_kernel(buffer->heap, buffer,
585 flags);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700586 if (IS_ERR_OR_NULL(vaddr))
587 _ion_unmap(&buffer->kmap_cnt, &handle->kmap_cnt);
588 buffer->vaddr = vaddr;
589 } else {
590 vaddr = buffer->vaddr;
591 }
Laura Abbott894fd582011-08-19 13:33:56 -0700592
593out:
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700594 mutex_unlock(&buffer->lock);
595 mutex_unlock(&client->lock);
596 return vaddr;
597}
Olav Hauganbd2b6922012-01-25 09:28:55 -0800598EXPORT_SYMBOL(ion_map_kernel);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700599
Olav Hauganb3676592012-03-02 15:02:25 -0800600static struct ion_iommu_map *__ion_iommu_map(struct ion_buffer *buffer,
Laura Abbott8c017362011-09-22 20:59:12 -0700601 int domain_num, int partition_num, unsigned long align,
602 unsigned long iova_length, unsigned long flags,
603 unsigned long *iova)
604{
605 struct ion_iommu_map *data;
606 int ret;
607
608 data = kmalloc(sizeof(*data), GFP_ATOMIC);
609
610 if (!data)
Olav Hauganb3676592012-03-02 15:02:25 -0800611 return ERR_PTR(-ENOMEM);
Laura Abbott8c017362011-09-22 20:59:12 -0700612
613 data->buffer = buffer;
614 iommu_map_domain(data) = domain_num;
615 iommu_map_partition(data) = partition_num;
616
617 ret = buffer->heap->ops->map_iommu(buffer, data,
618 domain_num,
619 partition_num,
620 align,
621 iova_length,
622 flags);
623
624 if (ret)
625 goto out;
626
627 kref_init(&data->ref);
628 *iova = data->iova_addr;
629
630 ion_iommu_add(buffer, data);
631
Olav Hauganb3676592012-03-02 15:02:25 -0800632 return data;
Laura Abbott8c017362011-09-22 20:59:12 -0700633
634out:
Laura Abbott8c017362011-09-22 20:59:12 -0700635 kfree(data);
Olav Hauganb3676592012-03-02 15:02:25 -0800636 return ERR_PTR(ret);
Laura Abbott8c017362011-09-22 20:59:12 -0700637}
638
639int ion_map_iommu(struct ion_client *client, struct ion_handle *handle,
640 int domain_num, int partition_num, unsigned long align,
641 unsigned long iova_length, unsigned long *iova,
642 unsigned long *buffer_size,
Olav Hauganb3676592012-03-02 15:02:25 -0800643 unsigned long flags, unsigned long iommu_flags)
Laura Abbott8c017362011-09-22 20:59:12 -0700644{
645 struct ion_buffer *buffer;
646 struct ion_iommu_map *iommu_map;
647 int ret = 0;
648
Olav Haugan79e9ffa2012-02-24 13:11:10 -0800649 if (ION_IS_CACHED(flags)) {
650 pr_err("%s: Cannot map iommu as cached.\n", __func__);
651 return -EINVAL;
652 }
653
Laura Abbott8c017362011-09-22 20:59:12 -0700654 mutex_lock(&client->lock);
655 if (!ion_handle_validate(client, handle)) {
656 pr_err("%s: invalid handle passed to map_kernel.\n",
657 __func__);
658 mutex_unlock(&client->lock);
659 return -EINVAL;
660 }
661
662 buffer = handle->buffer;
663 mutex_lock(&buffer->lock);
664
665 if (!handle->buffer->heap->ops->map_iommu) {
666 pr_err("%s: map_iommu is not implemented by this heap.\n",
667 __func__);
668 ret = -ENODEV;
669 goto out;
670 }
671
Laura Abbott8c017362011-09-22 20:59:12 -0700672 /*
673 * If clients don't want a custom iova length, just use whatever
674 * the buffer size is
675 */
676 if (!iova_length)
677 iova_length = buffer->size;
678
679 if (buffer->size > iova_length) {
680 pr_debug("%s: iova length %lx is not at least buffer size"
681 " %x\n", __func__, iova_length, buffer->size);
682 ret = -EINVAL;
683 goto out;
684 }
685
686 if (buffer->size & ~PAGE_MASK) {
687 pr_debug("%s: buffer size %x is not aligned to %lx", __func__,
688 buffer->size, PAGE_SIZE);
689 ret = -EINVAL;
690 goto out;
691 }
692
693 if (iova_length & ~PAGE_MASK) {
694 pr_debug("%s: iova_length %lx is not aligned to %lx", __func__,
695 iova_length, PAGE_SIZE);
696 ret = -EINVAL;
697 goto out;
698 }
699
700 iommu_map = ion_iommu_lookup(buffer, domain_num, partition_num);
Olav Hauganb3676592012-03-02 15:02:25 -0800701 _ion_map(&buffer->iommu_map_cnt, &handle->iommu_map_cnt);
702 if (!iommu_map) {
703 iommu_map = __ion_iommu_map(buffer, domain_num, partition_num,
704 align, iova_length, flags, iova);
705 if (IS_ERR_OR_NULL(iommu_map)) {
Laura Abbott8c017362011-09-22 20:59:12 -0700706 _ion_unmap(&buffer->iommu_map_cnt,
707 &handle->iommu_map_cnt);
Olav Hauganb3676592012-03-02 15:02:25 -0800708 } else {
709 iommu_map->flags = iommu_flags;
710
711 if (iommu_map->flags & ION_IOMMU_UNMAP_DELAYED)
712 kref_get(&iommu_map->ref);
713 }
Laura Abbott8c017362011-09-22 20:59:12 -0700714 } else {
Olav Hauganb3676592012-03-02 15:02:25 -0800715 if (iommu_map->flags != iommu_flags) {
716 pr_err("%s: handle %p is already mapped with iommu flags %lx, trying to map with flags %lx\n",
717 __func__, handle,
718 iommu_map->flags, iommu_flags);
719 _ion_unmap(&buffer->iommu_map_cnt,
720 &handle->iommu_map_cnt);
721 ret = -EINVAL;
722 } else if (iommu_map->mapped_size != iova_length) {
Laura Abbott8c017362011-09-22 20:59:12 -0700723 pr_err("%s: handle %p is already mapped with length"
Olav Hauganb3676592012-03-02 15:02:25 -0800724 " %x, trying to map with length %lx\n",
Laura Abbott8c017362011-09-22 20:59:12 -0700725 __func__, handle, iommu_map->mapped_size,
726 iova_length);
727 _ion_unmap(&buffer->iommu_map_cnt,
728 &handle->iommu_map_cnt);
729 ret = -EINVAL;
730 } else {
731 kref_get(&iommu_map->ref);
732 *iova = iommu_map->iova_addr;
733 }
734 }
735 *buffer_size = buffer->size;
736out:
737 mutex_unlock(&buffer->lock);
738 mutex_unlock(&client->lock);
739 return ret;
740}
741EXPORT_SYMBOL(ion_map_iommu);
742
743static void ion_iommu_release(struct kref *kref)
744{
745 struct ion_iommu_map *map = container_of(kref, struct ion_iommu_map,
746 ref);
747 struct ion_buffer *buffer = map->buffer;
748
749 rb_erase(&map->node, &buffer->iommu_maps);
750 buffer->heap->ops->unmap_iommu(map);
751 kfree(map);
752}
753
754void ion_unmap_iommu(struct ion_client *client, struct ion_handle *handle,
755 int domain_num, int partition_num)
756{
757 struct ion_iommu_map *iommu_map;
758 struct ion_buffer *buffer;
759
760 mutex_lock(&client->lock);
761 buffer = handle->buffer;
762
763 mutex_lock(&buffer->lock);
764
765 iommu_map = ion_iommu_lookup(buffer, domain_num, partition_num);
766
767 if (!iommu_map) {
768 WARN(1, "%s: (%d,%d) was never mapped for %p\n", __func__,
769 domain_num, partition_num, buffer);
770 goto out;
771 }
772
773 _ion_unmap(&buffer->iommu_map_cnt, &handle->iommu_map_cnt);
774 kref_put(&iommu_map->ref, ion_iommu_release);
775
776out:
777 mutex_unlock(&buffer->lock);
778
779 mutex_unlock(&client->lock);
780
781}
782EXPORT_SYMBOL(ion_unmap_iommu);
783
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700784struct scatterlist *ion_map_dma(struct ion_client *client,
Laura Abbott894fd582011-08-19 13:33:56 -0700785 struct ion_handle *handle,
786 unsigned long flags)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700787{
788 struct ion_buffer *buffer;
789 struct scatterlist *sglist;
790
791 mutex_lock(&client->lock);
792 if (!ion_handle_validate(client, handle)) {
793 pr_err("%s: invalid handle passed to map_dma.\n",
794 __func__);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700795 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700796 return ERR_PTR(-EINVAL);
797 }
798 buffer = handle->buffer;
799 mutex_lock(&buffer->lock);
800
801 if (!handle->buffer->heap->ops->map_dma) {
802 pr_err("%s: map_kernel is not implemented by this heap.\n",
803 __func__);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700804 mutex_unlock(&buffer->lock);
805 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700806 return ERR_PTR(-ENODEV);
807 }
Laura Abbott894fd582011-08-19 13:33:56 -0700808
Laura Abbott8c017362011-09-22 20:59:12 -0700809 if (ion_validate_buffer_flags(buffer, flags)) {
810 sglist = ERR_PTR(-EEXIST);
811 goto out;
Laura Abbott894fd582011-08-19 13:33:56 -0700812 }
813
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700814 if (_ion_map(&buffer->dmap_cnt, &handle->dmap_cnt)) {
815 sglist = buffer->heap->ops->map_dma(buffer->heap, buffer);
816 if (IS_ERR_OR_NULL(sglist))
817 _ion_unmap(&buffer->dmap_cnt, &handle->dmap_cnt);
818 buffer->sglist = sglist;
819 } else {
820 sglist = buffer->sglist;
821 }
Laura Abbott894fd582011-08-19 13:33:56 -0700822
823out:
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700824 mutex_unlock(&buffer->lock);
825 mutex_unlock(&client->lock);
826 return sglist;
827}
Olav Hauganbd2b6922012-01-25 09:28:55 -0800828EXPORT_SYMBOL(ion_map_dma);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700829
830void ion_unmap_kernel(struct ion_client *client, struct ion_handle *handle)
831{
832 struct ion_buffer *buffer;
833
834 mutex_lock(&client->lock);
835 buffer = handle->buffer;
836 mutex_lock(&buffer->lock);
837 if (_ion_unmap(&buffer->kmap_cnt, &handle->kmap_cnt)) {
838 buffer->heap->ops->unmap_kernel(buffer->heap, buffer);
839 buffer->vaddr = NULL;
840 }
841 mutex_unlock(&buffer->lock);
842 mutex_unlock(&client->lock);
843}
Olav Hauganbd2b6922012-01-25 09:28:55 -0800844EXPORT_SYMBOL(ion_unmap_kernel);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700845
846void ion_unmap_dma(struct ion_client *client, struct ion_handle *handle)
847{
848 struct ion_buffer *buffer;
849
850 mutex_lock(&client->lock);
851 buffer = handle->buffer;
852 mutex_lock(&buffer->lock);
853 if (_ion_unmap(&buffer->dmap_cnt, &handle->dmap_cnt)) {
854 buffer->heap->ops->unmap_dma(buffer->heap, buffer);
855 buffer->sglist = NULL;
856 }
857 mutex_unlock(&buffer->lock);
858 mutex_unlock(&client->lock);
859}
Olav Hauganbd2b6922012-01-25 09:28:55 -0800860EXPORT_SYMBOL(ion_unmap_dma);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700861
862struct ion_buffer *ion_share(struct ion_client *client,
863 struct ion_handle *handle)
864{
Rebecca Schultz Zavinc72866d2011-07-07 17:07:56 -0700865 bool valid_handle;
866
867 mutex_lock(&client->lock);
868 valid_handle = ion_handle_validate(client, handle);
869 mutex_unlock(&client->lock);
870 if (!valid_handle) {
871 WARN("%s: invalid handle passed to share.\n", __func__);
872 return ERR_PTR(-EINVAL);
873 }
874
Iliyan Malchev3fe24362011-08-09 14:42:08 -0700875 /* do not take an extra reference here, the burden is on the caller
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700876 * to make sure the buffer doesn't go away while it's passing it
877 * to another client -- ion_free should not be called on this handle
878 * until the buffer has been imported into the other client
879 */
880 return handle->buffer;
881}
Olav Hauganbd2b6922012-01-25 09:28:55 -0800882EXPORT_SYMBOL(ion_share);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700883
884struct ion_handle *ion_import(struct ion_client *client,
885 struct ion_buffer *buffer)
886{
887 struct ion_handle *handle = NULL;
888
889 mutex_lock(&client->lock);
890 /* if a handle exists for this buffer just take a reference to it */
891 handle = ion_handle_lookup(client, buffer);
892 if (!IS_ERR_OR_NULL(handle)) {
893 ion_handle_get(handle);
894 goto end;
895 }
896 handle = ion_handle_create(client, buffer);
897 if (IS_ERR_OR_NULL(handle))
898 goto end;
899 ion_handle_add(client, handle);
900end:
901 mutex_unlock(&client->lock);
902 return handle;
903}
Olav Hauganbd2b6922012-01-25 09:28:55 -0800904EXPORT_SYMBOL(ion_import);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700905
Laura Abbottabcb6f72011-10-04 16:26:49 -0700906static int check_vaddr_bounds(unsigned long start, unsigned long end)
907{
908 struct mm_struct *mm = current->active_mm;
909 struct vm_area_struct *vma;
910 int ret = 1;
911
912 if (end < start)
913 goto out;
914
915 down_read(&mm->mmap_sem);
916 vma = find_vma(mm, start);
917 if (vma && vma->vm_start < end) {
918 if (start < vma->vm_start)
919 goto out_up;
920 if (end > vma->vm_end)
921 goto out_up;
922 ret = 0;
923 }
924
925out_up:
926 up_read(&mm->mmap_sem);
927out:
928 return ret;
929}
930
Olav Haugan41f85792012-02-08 15:28:05 -0800931int ion_do_cache_op(struct ion_client *client, struct ion_handle *handle,
Laura Abbottabcb6f72011-10-04 16:26:49 -0700932 void *uaddr, unsigned long offset, unsigned long len,
933 unsigned int cmd)
934{
935 struct ion_buffer *buffer;
Laura Abbottabcb6f72011-10-04 16:26:49 -0700936 int ret = -EINVAL;
937
938 mutex_lock(&client->lock);
939 if (!ion_handle_validate(client, handle)) {
940 pr_err("%s: invalid handle passed to do_cache_op.\n",
941 __func__);
942 mutex_unlock(&client->lock);
943 return -EINVAL;
944 }
945 buffer = handle->buffer;
946 mutex_lock(&buffer->lock);
947
Laura Abbottcbaa6682011-10-19 12:14:14 -0700948 if (!ION_IS_CACHED(buffer->flags)) {
Laura Abbottabcb6f72011-10-04 16:26:49 -0700949 ret = 0;
950 goto out;
951 }
952
953 if (!handle->buffer->heap->ops->cache_op) {
954 pr_err("%s: cache_op is not implemented by this heap.\n",
955 __func__);
956 ret = -ENODEV;
957 goto out;
958 }
959
Laura Abbottabcb6f72011-10-04 16:26:49 -0700960
961 ret = buffer->heap->ops->cache_op(buffer->heap, buffer, uaddr,
962 offset, len, cmd);
963
964out:
965 mutex_unlock(&buffer->lock);
966 mutex_unlock(&client->lock);
967 return ret;
968
969}
970
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700971static const struct file_operations ion_share_fops;
972
973struct ion_handle *ion_import_fd(struct ion_client *client, int fd)
974{
975 struct file *file = fget(fd);
976 struct ion_handle *handle;
977
978 if (!file) {
979 pr_err("%s: imported fd not found in file table.\n", __func__);
980 return ERR_PTR(-EINVAL);
981 }
982 if (file->f_op != &ion_share_fops) {
Laura Abbott084d6eb2011-10-24 19:09:50 -0700983 pr_err("%s: imported file %s is not a shared ion"
984 " file.", __func__, file->f_dentry->d_name.name);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700985 handle = ERR_PTR(-EINVAL);
986 goto end;
987 }
988 handle = ion_import(client, file->private_data);
989end:
990 fput(file);
991 return handle;
992}
Olav Hauganbd2b6922012-01-25 09:28:55 -0800993EXPORT_SYMBOL(ion_import_fd);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700994
995static int ion_debug_client_show(struct seq_file *s, void *unused)
996{
997 struct ion_client *client = s->private;
998 struct rb_node *n;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700999
Laura Abbott68c80642011-10-21 17:32:27 -07001000 seq_printf(s, "%16.16s: %16.16s : %16.16s : %16.16s\n", "heap_name",
1001 "size_in_bytes", "handle refcount", "buffer");
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001002 mutex_lock(&client->lock);
1003 for (n = rb_first(&client->handles); n; n = rb_next(n)) {
1004 struct ion_handle *handle = rb_entry(n, struct ion_handle,
1005 node);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001006
Laura Abbott8747bbe2011-10-31 14:18:13 -07001007 seq_printf(s, "%16.16s: %16x : %16d : %16p\n",
Laura Abbott68c80642011-10-21 17:32:27 -07001008 handle->buffer->heap->name,
1009 handle->buffer->size,
1010 atomic_read(&handle->ref.refcount),
1011 handle->buffer);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001012 }
Laura Abbott68c80642011-10-21 17:32:27 -07001013
1014 seq_printf(s, "%16.16s %d\n", "client refcount:",
1015 atomic_read(&client->ref.refcount));
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001016 mutex_unlock(&client->lock);
1017
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001018 return 0;
1019}
1020
1021static int ion_debug_client_open(struct inode *inode, struct file *file)
1022{
1023 return single_open(file, ion_debug_client_show, inode->i_private);
1024}
1025
1026static const struct file_operations debug_client_fops = {
1027 .open = ion_debug_client_open,
1028 .read = seq_read,
1029 .llseek = seq_lseek,
1030 .release = single_release,
1031};
1032
Rebecca Schultz Zavin83e3dab2011-07-01 20:41:25 -07001033static struct ion_client *ion_client_lookup(struct ion_device *dev,
1034 struct task_struct *task)
1035{
1036 struct rb_node *n = dev->user_clients.rb_node;
1037 struct ion_client *client;
1038
1039 mutex_lock(&dev->lock);
1040 while (n) {
1041 client = rb_entry(n, struct ion_client, node);
1042 if (task == client->task) {
1043 ion_client_get(client);
1044 mutex_unlock(&dev->lock);
1045 return client;
1046 } else if (task < client->task) {
1047 n = n->rb_left;
1048 } else if (task > client->task) {
1049 n = n->rb_right;
1050 }
1051 }
1052 mutex_unlock(&dev->lock);
1053 return NULL;
1054}
1055
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001056struct ion_client *ion_client_create(struct ion_device *dev,
1057 unsigned int heap_mask,
1058 const char *name)
1059{
1060 struct ion_client *client;
1061 struct task_struct *task;
1062 struct rb_node **p;
1063 struct rb_node *parent = NULL;
1064 struct ion_client *entry;
Rebecca Schultz Zavin83e3dab2011-07-01 20:41:25 -07001065 pid_t pid;
Olav Haugane8a31972012-05-16 13:11:41 -07001066 unsigned int name_len;
1067
1068 if (!name) {
1069 pr_err("%s: Name cannot be null\n", __func__);
1070 return ERR_PTR(-EINVAL);
1071 }
1072 name_len = strnlen(name, 64);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001073
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001074 get_task_struct(current->group_leader);
1075 task_lock(current->group_leader);
Rebecca Schultz Zavin83e3dab2011-07-01 20:41:25 -07001076 pid = task_pid_nr(current->group_leader);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001077 /* don't bother to store task struct for kernel threads,
1078 they can't be killed anyway */
1079 if (current->group_leader->flags & PF_KTHREAD) {
1080 put_task_struct(current->group_leader);
1081 task = NULL;
1082 } else {
1083 task = current->group_leader;
1084 }
1085 task_unlock(current->group_leader);
Rebecca Schultz Zavin83e3dab2011-07-01 20:41:25 -07001086
1087 /* if this isn't a kernel thread, see if a client already
1088 exists */
1089 if (task) {
1090 client = ion_client_lookup(dev, task);
1091 if (!IS_ERR_OR_NULL(client)) {
1092 put_task_struct(current->group_leader);
1093 return client;
1094 }
1095 }
1096
1097 client = kzalloc(sizeof(struct ion_client), GFP_KERNEL);
1098 if (!client) {
1099 put_task_struct(current->group_leader);
1100 return ERR_PTR(-ENOMEM);
1101 }
1102
1103 client->dev = dev;
1104 client->handles = RB_ROOT;
1105 mutex_init(&client->lock);
Olav Haugan63e5f3b2012-01-11 16:42:37 -08001106
Olav Haugan6625c7d12012-01-24 13:50:43 -08001107 client->name = kzalloc(name_len+1, GFP_KERNEL);
Olav Haugan63e5f3b2012-01-11 16:42:37 -08001108 if (!client->name) {
1109 put_task_struct(current->group_leader);
1110 kfree(client);
1111 return ERR_PTR(-ENOMEM);
1112 } else {
Olav Haugan6625c7d12012-01-24 13:50:43 -08001113 strlcpy(client->name, name, name_len+1);
Olav Haugan63e5f3b2012-01-11 16:42:37 -08001114 }
1115
Rebecca Schultz Zavin83e3dab2011-07-01 20:41:25 -07001116 client->heap_mask = heap_mask;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001117 client->task = task;
Rebecca Schultz Zavin83e3dab2011-07-01 20:41:25 -07001118 client->pid = pid;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001119 kref_init(&client->ref);
1120
1121 mutex_lock(&dev->lock);
1122 if (task) {
1123 p = &dev->user_clients.rb_node;
1124 while (*p) {
1125 parent = *p;
1126 entry = rb_entry(parent, struct ion_client, node);
1127
1128 if (task < entry->task)
1129 p = &(*p)->rb_left;
1130 else if (task > entry->task)
1131 p = &(*p)->rb_right;
1132 }
1133 rb_link_node(&client->node, parent, p);
1134 rb_insert_color(&client->node, &dev->user_clients);
1135 } else {
1136 p = &dev->kernel_clients.rb_node;
1137 while (*p) {
1138 parent = *p;
1139 entry = rb_entry(parent, struct ion_client, node);
1140
1141 if (client < entry)
1142 p = &(*p)->rb_left;
1143 else if (client > entry)
1144 p = &(*p)->rb_right;
1145 }
1146 rb_link_node(&client->node, parent, p);
1147 rb_insert_color(&client->node, &dev->kernel_clients);
1148 }
1149
Laura Abbotteed86032011-12-05 15:32:36 -08001150
1151 client->debug_root = debugfs_create_file(name, 0664,
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001152 dev->debug_root, client,
1153 &debug_client_fops);
1154 mutex_unlock(&dev->lock);
1155
1156 return client;
1157}
1158
Rebecca Schultz Zavin0b7e8ae2011-07-06 18:07:01 -07001159static void _ion_client_destroy(struct kref *kref)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001160{
Rebecca Schultz Zavin0b7e8ae2011-07-06 18:07:01 -07001161 struct ion_client *client = container_of(kref, struct ion_client, ref);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001162 struct ion_device *dev = client->dev;
1163 struct rb_node *n;
1164
1165 pr_debug("%s: %d\n", __func__, __LINE__);
1166 while ((n = rb_first(&client->handles))) {
1167 struct ion_handle *handle = rb_entry(n, struct ion_handle,
1168 node);
1169 ion_handle_destroy(&handle->ref);
1170 }
1171 mutex_lock(&dev->lock);
1172 if (client->task) {
1173 rb_erase(&client->node, &dev->user_clients);
1174 put_task_struct(client->task);
1175 } else {
1176 rb_erase(&client->node, &dev->kernel_clients);
1177 }
1178 debugfs_remove_recursive(client->debug_root);
1179 mutex_unlock(&dev->lock);
1180
Olav Haugan63e5f3b2012-01-11 16:42:37 -08001181 kfree(client->name);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001182 kfree(client);
1183}
1184
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001185static void ion_client_get(struct ion_client *client)
1186{
1187 kref_get(&client->ref);
1188}
1189
1190static int ion_client_put(struct ion_client *client)
1191{
1192 return kref_put(&client->ref, _ion_client_destroy);
1193}
1194
Rebecca Schultz Zavin0b7e8ae2011-07-06 18:07:01 -07001195void ion_client_destroy(struct ion_client *client)
1196{
Jordan Crousea75022c2011-10-12 16:57:47 -06001197 if (client)
1198 ion_client_put(client);
Rebecca Schultz Zavin0b7e8ae2011-07-06 18:07:01 -07001199}
Olav Hauganbd2b6922012-01-25 09:28:55 -08001200EXPORT_SYMBOL(ion_client_destroy);
Rebecca Schultz Zavin0b7e8ae2011-07-06 18:07:01 -07001201
Laura Abbott273dd8e2011-10-12 14:26:33 -07001202int ion_handle_get_flags(struct ion_client *client, struct ion_handle *handle,
1203 unsigned long *flags)
1204{
1205 struct ion_buffer *buffer;
1206
1207 mutex_lock(&client->lock);
1208 if (!ion_handle_validate(client, handle)) {
1209 pr_err("%s: invalid handle passed to %s.\n",
1210 __func__, __func__);
1211 mutex_unlock(&client->lock);
1212 return -EINVAL;
1213 }
1214 buffer = handle->buffer;
1215 mutex_lock(&buffer->lock);
1216 *flags = buffer->flags;
1217 mutex_unlock(&buffer->lock);
1218 mutex_unlock(&client->lock);
1219
1220 return 0;
1221}
1222EXPORT_SYMBOL(ion_handle_get_flags);
1223
Laura Abbott8c017362011-09-22 20:59:12 -07001224int ion_handle_get_size(struct ion_client *client, struct ion_handle *handle,
1225 unsigned long *size)
1226{
1227 struct ion_buffer *buffer;
1228
1229 mutex_lock(&client->lock);
1230 if (!ion_handle_validate(client, handle)) {
1231 pr_err("%s: invalid handle passed to %s.\n",
1232 __func__, __func__);
1233 mutex_unlock(&client->lock);
1234 return -EINVAL;
1235 }
1236 buffer = handle->buffer;
1237 mutex_lock(&buffer->lock);
1238 *size = buffer->size;
1239 mutex_unlock(&buffer->lock);
1240 mutex_unlock(&client->lock);
1241
1242 return 0;
1243}
1244EXPORT_SYMBOL(ion_handle_get_size);
1245
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001246static int ion_share_release(struct inode *inode, struct file* file)
1247{
1248 struct ion_buffer *buffer = file->private_data;
1249
1250 pr_debug("%s: %d\n", __func__, __LINE__);
1251 /* drop the reference to the buffer -- this prevents the
1252 buffer from going away because the client holding it exited
1253 while it was being passed */
1254 ion_buffer_put(buffer);
1255 return 0;
1256}
1257
1258static void ion_vma_open(struct vm_area_struct *vma)
1259{
1260
1261 struct ion_buffer *buffer = vma->vm_file->private_data;
1262 struct ion_handle *handle = vma->vm_private_data;
1263 struct ion_client *client;
1264
1265 pr_debug("%s: %d\n", __func__, __LINE__);
1266 /* check that the client still exists and take a reference so
1267 it can't go away until this vma is closed */
1268 client = ion_client_lookup(buffer->dev, current->group_leader);
1269 if (IS_ERR_OR_NULL(client)) {
1270 vma->vm_private_data = NULL;
1271 return;
1272 }
Laura Abbott0f2175b2011-12-09 14:26:07 -08001273 ion_handle_get(handle);
Laura Abbott77168502011-12-05 11:06:24 -08001274 mutex_lock(&buffer->lock);
1275 buffer->umap_cnt++;
1276 mutex_unlock(&buffer->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001277 pr_debug("%s: %d client_cnt %d handle_cnt %d alloc_cnt %d\n",
1278 __func__, __LINE__,
1279 atomic_read(&client->ref.refcount),
1280 atomic_read(&handle->ref.refcount),
1281 atomic_read(&buffer->ref.refcount));
1282}
1283
1284static void ion_vma_close(struct vm_area_struct *vma)
1285{
1286 struct ion_handle *handle = vma->vm_private_data;
1287 struct ion_buffer *buffer = vma->vm_file->private_data;
1288 struct ion_client *client;
1289
1290 pr_debug("%s: %d\n", __func__, __LINE__);
1291 /* this indicates the client is gone, nothing to do here */
1292 if (!handle)
1293 return;
1294 client = handle->client;
Laura Abbott77168502011-12-05 11:06:24 -08001295 mutex_lock(&buffer->lock);
1296 buffer->umap_cnt--;
1297 mutex_unlock(&buffer->lock);
Laura Abbotta6835092011-11-14 15:27:02 -08001298
1299 if (buffer->heap->ops->unmap_user)
1300 buffer->heap->ops->unmap_user(buffer->heap, buffer);
1301
1302
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001303 pr_debug("%s: %d client_cnt %d handle_cnt %d alloc_cnt %d\n",
1304 __func__, __LINE__,
1305 atomic_read(&client->ref.refcount),
1306 atomic_read(&handle->ref.refcount),
1307 atomic_read(&buffer->ref.refcount));
Laura Abbottec149ff2012-01-26 13:33:11 -08001308 mutex_lock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001309 ion_handle_put(handle);
Laura Abbottec149ff2012-01-26 13:33:11 -08001310 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001311 ion_client_put(client);
1312 pr_debug("%s: %d client_cnt %d handle_cnt %d alloc_cnt %d\n",
1313 __func__, __LINE__,
1314 atomic_read(&client->ref.refcount),
1315 atomic_read(&handle->ref.refcount),
1316 atomic_read(&buffer->ref.refcount));
1317}
1318
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001319static struct vm_operations_struct ion_vm_ops = {
1320 .open = ion_vma_open,
1321 .close = ion_vma_close,
1322};
1323
1324static int ion_share_mmap(struct file *file, struct vm_area_struct *vma)
1325{
1326 struct ion_buffer *buffer = file->private_data;
1327 unsigned long size = vma->vm_end - vma->vm_start;
1328 struct ion_client *client;
1329 struct ion_handle *handle;
1330 int ret;
Laura Abbott894fd582011-08-19 13:33:56 -07001331 unsigned long flags = file->f_flags & O_DSYNC ?
1332 ION_SET_CACHE(UNCACHED) :
1333 ION_SET_CACHE(CACHED);
1334
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001335
1336 pr_debug("%s: %d\n", __func__, __LINE__);
1337 /* make sure the client still exists, it's possible for the client to
1338 have gone away but the map/share fd still to be around, take
1339 a reference to it so it can't go away while this mapping exists */
1340 client = ion_client_lookup(buffer->dev, current->group_leader);
1341 if (IS_ERR_OR_NULL(client)) {
1342 pr_err("%s: trying to mmap an ion handle in a process with no "
1343 "ion client\n", __func__);
1344 return -EINVAL;
1345 }
1346
1347 if ((size > buffer->size) || (size + (vma->vm_pgoff << PAGE_SHIFT) >
1348 buffer->size)) {
1349 pr_err("%s: trying to map larger area than handle has available"
1350 "\n", __func__);
1351 ret = -EINVAL;
1352 goto err;
1353 }
1354
1355 /* find the handle and take a reference to it */
1356 handle = ion_import(client, buffer);
1357 if (IS_ERR_OR_NULL(handle)) {
1358 ret = -EINVAL;
1359 goto err;
1360 }
1361
1362 if (!handle->buffer->heap->ops->map_user) {
1363 pr_err("%s: this heap does not define a method for mapping "
1364 "to userspace\n", __func__);
1365 ret = -EINVAL;
1366 goto err1;
1367 }
1368
1369 mutex_lock(&buffer->lock);
Laura Abbott894fd582011-08-19 13:33:56 -07001370
Laura Abbott8c017362011-09-22 20:59:12 -07001371 if (ion_validate_buffer_flags(buffer, flags)) {
1372 ret = -EEXIST;
1373 mutex_unlock(&buffer->lock);
1374 goto err1;
Laura Abbott894fd582011-08-19 13:33:56 -07001375 }
Laura Abbott8c017362011-09-22 20:59:12 -07001376
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001377 /* now map it to userspace */
Laura Abbott894fd582011-08-19 13:33:56 -07001378 ret = buffer->heap->ops->map_user(buffer->heap, buffer, vma,
1379 flags);
Laura Abbotte8bc7aa2011-12-09 14:49:33 -08001380
1381 buffer->umap_cnt++;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001382 if (ret) {
1383 pr_err("%s: failure mapping buffer to userspace\n",
1384 __func__);
Laura Abbott894fd582011-08-19 13:33:56 -07001385 goto err2;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001386 }
Laura Abbotte8bc7aa2011-12-09 14:49:33 -08001387 mutex_unlock(&buffer->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001388
1389 vma->vm_ops = &ion_vm_ops;
1390 /* move the handle into the vm_private_data so we can access it from
1391 vma_open/close */
1392 vma->vm_private_data = handle;
1393 pr_debug("%s: %d client_cnt %d handle_cnt %d alloc_cnt %d\n",
1394 __func__, __LINE__,
1395 atomic_read(&client->ref.refcount),
1396 atomic_read(&handle->ref.refcount),
1397 atomic_read(&buffer->ref.refcount));
1398 return 0;
1399
Laura Abbott894fd582011-08-19 13:33:56 -07001400err2:
1401 buffer->umap_cnt--;
Laura Abbotte8bc7aa2011-12-09 14:49:33 -08001402 mutex_unlock(&buffer->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001403 /* drop the reference to the handle */
Laura Abbott894fd582011-08-19 13:33:56 -07001404err1:
Laura Abbottec149ff2012-01-26 13:33:11 -08001405 mutex_lock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001406 ion_handle_put(handle);
Laura Abbottec149ff2012-01-26 13:33:11 -08001407 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001408err:
Iliyan Malchev3fe24362011-08-09 14:42:08 -07001409 /* drop the reference to the client */
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001410 ion_client_put(client);
1411 return ret;
1412}
1413
1414static const struct file_operations ion_share_fops = {
1415 .owner = THIS_MODULE,
1416 .release = ion_share_release,
1417 .mmap = ion_share_mmap,
1418};
1419
1420static int ion_ioctl_share(struct file *parent, struct ion_client *client,
1421 struct ion_handle *handle)
1422{
1423 int fd = get_unused_fd();
1424 struct file *file;
1425
1426 if (fd < 0)
1427 return -ENFILE;
1428
1429 file = anon_inode_getfile("ion_share_fd", &ion_share_fops,
1430 handle->buffer, O_RDWR);
1431 if (IS_ERR_OR_NULL(file))
1432 goto err;
Laura Abbott4b5d0482011-09-27 18:35:14 -07001433
1434 if (parent->f_flags & O_DSYNC)
1435 file->f_flags |= O_DSYNC;
1436
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001437 ion_buffer_get(handle->buffer);
1438 fd_install(fd, file);
1439
1440 return fd;
1441
1442err:
1443 put_unused_fd(fd);
1444 return -ENFILE;
1445}
1446
1447static long ion_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1448{
1449 struct ion_client *client = filp->private_data;
1450
1451 switch (cmd) {
1452 case ION_IOC_ALLOC:
1453 {
1454 struct ion_allocation_data data;
1455
1456 if (copy_from_user(&data, (void __user *)arg, sizeof(data)))
1457 return -EFAULT;
1458 data.handle = ion_alloc(client, data.len, data.align,
1459 data.flags);
Laura Abbotte1b9ce52011-11-11 18:31:39 -08001460
1461 if (IS_ERR_OR_NULL(data.handle))
Olav Hauganb06ee072011-12-13 15:31:41 -08001462 return -ENOMEM;
Laura Abbotte1b9ce52011-11-11 18:31:39 -08001463
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001464 if (copy_to_user((void __user *)arg, &data, sizeof(data)))
1465 return -EFAULT;
1466 break;
1467 }
1468 case ION_IOC_FREE:
1469 {
1470 struct ion_handle_data data;
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001471 bool valid;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001472
1473 if (copy_from_user(&data, (void __user *)arg,
1474 sizeof(struct ion_handle_data)))
1475 return -EFAULT;
1476 mutex_lock(&client->lock);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001477 valid = ion_handle_validate(client, data.handle);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001478 mutex_unlock(&client->lock);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001479 if (!valid)
1480 return -EINVAL;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001481 ion_free(client, data.handle);
1482 break;
1483 }
1484 case ION_IOC_MAP:
1485 case ION_IOC_SHARE:
1486 {
1487 struct ion_fd_data data;
1488
1489 if (copy_from_user(&data, (void __user *)arg, sizeof(data)))
1490 return -EFAULT;
1491 mutex_lock(&client->lock);
1492 if (!ion_handle_validate(client, data.handle)) {
1493 pr_err("%s: invalid handle passed to share ioctl.\n",
1494 __func__);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001495 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001496 return -EINVAL;
1497 }
1498 data.fd = ion_ioctl_share(filp, client, data.handle);
1499 mutex_unlock(&client->lock);
1500 if (copy_to_user((void __user *)arg, &data, sizeof(data)))
1501 return -EFAULT;
Olav Hauganc2d2cf52012-05-15 14:40:11 -07001502 if (data.fd < 0)
1503 return data.fd;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001504 break;
1505 }
1506 case ION_IOC_IMPORT:
1507 {
1508 struct ion_fd_data data;
Olav Hauganc2d2cf52012-05-15 14:40:11 -07001509 int ret = 0;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001510 if (copy_from_user(&data, (void __user *)arg,
1511 sizeof(struct ion_fd_data)))
1512 return -EFAULT;
1513
1514 data.handle = ion_import_fd(client, data.fd);
Olav Hauganc2d2cf52012-05-15 14:40:11 -07001515 if (IS_ERR(data.handle)) {
1516 ret = PTR_ERR(data.handle);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001517 data.handle = NULL;
Olav Hauganc2d2cf52012-05-15 14:40:11 -07001518 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001519 if (copy_to_user((void __user *)arg, &data,
1520 sizeof(struct ion_fd_data)))
1521 return -EFAULT;
Olav Hauganc2d2cf52012-05-15 14:40:11 -07001522 if (ret < 0)
1523 return ret;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001524 break;
1525 }
1526 case ION_IOC_CUSTOM:
1527 {
1528 struct ion_device *dev = client->dev;
1529 struct ion_custom_data data;
1530
1531 if (!dev->custom_ioctl)
1532 return -ENOTTY;
1533 if (copy_from_user(&data, (void __user *)arg,
1534 sizeof(struct ion_custom_data)))
1535 return -EFAULT;
1536 return dev->custom_ioctl(client, data.cmd, data.arg);
1537 }
Laura Abbottabcb6f72011-10-04 16:26:49 -07001538 case ION_IOC_CLEAN_CACHES:
1539 case ION_IOC_INV_CACHES:
1540 case ION_IOC_CLEAN_INV_CACHES:
1541 {
1542 struct ion_flush_data data;
Laura Abbott9fa29e82011-11-14 09:42:53 -08001543 unsigned long start, end;
Laura Abbotte80ea012011-11-18 18:36:47 -08001544 struct ion_handle *handle = NULL;
1545 int ret;
Laura Abbottabcb6f72011-10-04 16:26:49 -07001546
1547 if (copy_from_user(&data, (void __user *)arg,
1548 sizeof(struct ion_flush_data)))
1549 return -EFAULT;
1550
Laura Abbott9fa29e82011-11-14 09:42:53 -08001551 start = (unsigned long) data.vaddr;
1552 end = (unsigned long) data.vaddr + data.length;
1553
1554 if (check_vaddr_bounds(start, end)) {
1555 pr_err("%s: virtual address %p is out of bounds\n",
1556 __func__, data.vaddr);
1557 return -EINVAL;
1558 }
1559
Laura Abbotte80ea012011-11-18 18:36:47 -08001560 if (!data.handle) {
1561 handle = ion_import_fd(client, data.fd);
1562 if (IS_ERR_OR_NULL(handle)) {
1563 pr_info("%s: Could not import handle: %d\n",
1564 __func__, (int)handle);
1565 return -EINVAL;
1566 }
1567 }
1568
1569 ret = ion_do_cache_op(client,
1570 data.handle ? data.handle : handle,
1571 data.vaddr, data.offset, data.length,
1572 cmd);
1573
1574 if (!data.handle)
1575 ion_free(client, handle);
1576
Olav Haugand7baec02012-05-15 14:38:09 -07001577 if (ret < 0)
1578 return ret;
Laura Abbotte80ea012011-11-18 18:36:47 -08001579 break;
Laura Abbottabcb6f72011-10-04 16:26:49 -07001580
1581 }
Laura Abbott273dd8e2011-10-12 14:26:33 -07001582 case ION_IOC_GET_FLAGS:
1583 {
1584 struct ion_flag_data data;
1585 int ret;
1586 if (copy_from_user(&data, (void __user *)arg,
1587 sizeof(struct ion_flag_data)))
1588 return -EFAULT;
1589
1590 ret = ion_handle_get_flags(client, data.handle, &data.flags);
1591 if (ret < 0)
1592 return ret;
1593 if (copy_to_user((void __user *)arg, &data,
1594 sizeof(struct ion_flag_data)))
1595 return -EFAULT;
1596 break;
1597 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001598 default:
1599 return -ENOTTY;
1600 }
1601 return 0;
1602}
1603
1604static int ion_release(struct inode *inode, struct file *file)
1605{
1606 struct ion_client *client = file->private_data;
1607
1608 pr_debug("%s: %d\n", __func__, __LINE__);
1609 ion_client_put(client);
1610 return 0;
1611}
1612
1613static int ion_open(struct inode *inode, struct file *file)
1614{
1615 struct miscdevice *miscdev = file->private_data;
1616 struct ion_device *dev = container_of(miscdev, struct ion_device, dev);
1617 struct ion_client *client;
Laura Abbotteed86032011-12-05 15:32:36 -08001618 char debug_name[64];
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001619
1620 pr_debug("%s: %d\n", __func__, __LINE__);
Laura Abbotteed86032011-12-05 15:32:36 -08001621 snprintf(debug_name, 64, "%u", task_pid_nr(current->group_leader));
1622 client = ion_client_create(dev, -1, debug_name);
Rebecca Schultz Zavin6d3b9582011-07-06 18:07:24 -07001623 if (IS_ERR_OR_NULL(client))
1624 return PTR_ERR(client);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001625 file->private_data = client;
1626
1627 return 0;
1628}
1629
1630static const struct file_operations ion_fops = {
1631 .owner = THIS_MODULE,
1632 .open = ion_open,
1633 .release = ion_release,
1634 .unlocked_ioctl = ion_ioctl,
1635};
1636
1637static size_t ion_debug_heap_total(struct ion_client *client,
Laura Abbott3647ac32011-10-31 14:09:53 -07001638 enum ion_heap_ids id)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001639{
1640 size_t size = 0;
1641 struct rb_node *n;
1642
1643 mutex_lock(&client->lock);
1644 for (n = rb_first(&client->handles); n; n = rb_next(n)) {
1645 struct ion_handle *handle = rb_entry(n,
1646 struct ion_handle,
1647 node);
Laura Abbott3647ac32011-10-31 14:09:53 -07001648 if (handle->buffer->heap->id == id)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001649 size += handle->buffer->size;
1650 }
1651 mutex_unlock(&client->lock);
1652 return size;
1653}
1654
1655static int ion_debug_heap_show(struct seq_file *s, void *unused)
1656{
1657 struct ion_heap *heap = s->private;
1658 struct ion_device *dev = heap->dev;
1659 struct rb_node *n;
1660
1661 seq_printf(s, "%16.s %16.s %16.s\n", "client", "pid", "size");
1662 for (n = rb_first(&dev->user_clients); n; n = rb_next(n)) {
1663 struct ion_client *client = rb_entry(n, struct ion_client,
1664 node);
1665 char task_comm[TASK_COMM_LEN];
Laura Abbott3647ac32011-10-31 14:09:53 -07001666 size_t size = ion_debug_heap_total(client, heap->id);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001667 if (!size)
1668 continue;
1669
1670 get_task_comm(task_comm, client->task);
Laura Abbott8747bbe2011-10-31 14:18:13 -07001671 seq_printf(s, "%16.s %16u %16x\n", task_comm, client->pid,
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001672 size);
1673 }
1674
1675 for (n = rb_first(&dev->kernel_clients); n; n = rb_next(n)) {
1676 struct ion_client *client = rb_entry(n, struct ion_client,
1677 node);
Laura Abbott3647ac32011-10-31 14:09:53 -07001678 size_t size = ion_debug_heap_total(client, heap->id);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001679 if (!size)
1680 continue;
Laura Abbott8747bbe2011-10-31 14:18:13 -07001681 seq_printf(s, "%16.s %16u %16x\n", client->name, client->pid,
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001682 size);
1683 }
Olav Haugan3d4fe1a2012-01-13 11:42:15 -08001684 if (heap->ops->print_debug)
1685 heap->ops->print_debug(heap, s);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001686 return 0;
1687}
1688
1689static int ion_debug_heap_open(struct inode *inode, struct file *file)
1690{
1691 return single_open(file, ion_debug_heap_show, inode->i_private);
1692}
1693
1694static const struct file_operations debug_heap_fops = {
1695 .open = ion_debug_heap_open,
1696 .read = seq_read,
1697 .llseek = seq_lseek,
1698 .release = single_release,
1699};
1700
1701void ion_device_add_heap(struct ion_device *dev, struct ion_heap *heap)
1702{
1703 struct rb_node **p = &dev->heaps.rb_node;
1704 struct rb_node *parent = NULL;
1705 struct ion_heap *entry;
1706
1707 heap->dev = dev;
1708 mutex_lock(&dev->lock);
1709 while (*p) {
1710 parent = *p;
1711 entry = rb_entry(parent, struct ion_heap, node);
1712
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001713 if (heap->id < entry->id) {
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001714 p = &(*p)->rb_left;
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001715 } else if (heap->id > entry->id ) {
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001716 p = &(*p)->rb_right;
1717 } else {
1718 pr_err("%s: can not insert multiple heaps with "
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -07001719 "id %d\n", __func__, heap->id);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001720 goto end;
1721 }
1722 }
1723
1724 rb_link_node(&heap->node, parent, p);
1725 rb_insert_color(&heap->node, &dev->heaps);
1726 debugfs_create_file(heap->name, 0664, dev->debug_root, heap,
1727 &debug_heap_fops);
1728end:
1729 mutex_unlock(&dev->lock);
1730}
1731
Olav Haugan0a852512012-01-09 10:20:55 -08001732int ion_secure_heap(struct ion_device *dev, int heap_id)
1733{
1734 struct rb_node *n;
1735 int ret_val = 0;
1736
1737 /*
1738 * traverse the list of heaps available in this system
1739 * and find the heap that is specified.
1740 */
1741 mutex_lock(&dev->lock);
1742 for (n = rb_first(&dev->heaps); n != NULL; n = rb_next(n)) {
1743 struct ion_heap *heap = rb_entry(n, struct ion_heap, node);
1744 if (heap->type != ION_HEAP_TYPE_CP)
1745 continue;
1746 if (ION_HEAP(heap->id) != heap_id)
1747 continue;
1748 if (heap->ops->secure_heap)
1749 ret_val = heap->ops->secure_heap(heap);
1750 else
1751 ret_val = -EINVAL;
1752 break;
1753 }
1754 mutex_unlock(&dev->lock);
1755 return ret_val;
1756}
Olav Haugan0a852512012-01-09 10:20:55 -08001757
1758int ion_unsecure_heap(struct ion_device *dev, int heap_id)
1759{
1760 struct rb_node *n;
1761 int ret_val = 0;
1762
1763 /*
1764 * traverse the list of heaps available in this system
1765 * and find the heap that is specified.
1766 */
1767 mutex_lock(&dev->lock);
1768 for (n = rb_first(&dev->heaps); n != NULL; n = rb_next(n)) {
1769 struct ion_heap *heap = rb_entry(n, struct ion_heap, node);
1770 if (heap->type != ION_HEAP_TYPE_CP)
1771 continue;
1772 if (ION_HEAP(heap->id) != heap_id)
1773 continue;
1774 if (heap->ops->secure_heap)
1775 ret_val = heap->ops->unsecure_heap(heap);
1776 else
1777 ret_val = -EINVAL;
1778 break;
1779 }
1780 mutex_unlock(&dev->lock);
1781 return ret_val;
1782}
Olav Haugan0a852512012-01-09 10:20:55 -08001783
Laura Abbott404f8242011-10-31 14:22:53 -07001784static int ion_debug_leak_show(struct seq_file *s, void *unused)
1785{
1786 struct ion_device *dev = s->private;
1787 struct rb_node *n;
1788 struct rb_node *n2;
1789
1790 /* mark all buffers as 1 */
1791 seq_printf(s, "%16.s %16.s %16.s %16.s\n", "buffer", "heap", "size",
1792 "ref cnt");
1793 mutex_lock(&dev->lock);
1794 for (n = rb_first(&dev->buffers); n; n = rb_next(n)) {
1795 struct ion_buffer *buf = rb_entry(n, struct ion_buffer,
1796 node);
1797
1798 buf->marked = 1;
1799 }
1800
1801 /* now see which buffers we can access */
1802 for (n = rb_first(&dev->kernel_clients); n; n = rb_next(n)) {
1803 struct ion_client *client = rb_entry(n, struct ion_client,
1804 node);
1805
1806 mutex_lock(&client->lock);
1807 for (n2 = rb_first(&client->handles); n2; n2 = rb_next(n2)) {
1808 struct ion_handle *handle = rb_entry(n2,
1809 struct ion_handle, node);
1810
1811 handle->buffer->marked = 0;
1812
1813 }
1814 mutex_unlock(&client->lock);
1815
1816 }
1817
1818 for (n = rb_first(&dev->user_clients); n; n = rb_next(n)) {
1819 struct ion_client *client = rb_entry(n, struct ion_client,
1820 node);
1821
1822 mutex_lock(&client->lock);
1823 for (n2 = rb_first(&client->handles); n2; n2 = rb_next(n2)) {
1824 struct ion_handle *handle = rb_entry(n2,
1825 struct ion_handle, node);
1826
1827 handle->buffer->marked = 0;
1828
1829 }
1830 mutex_unlock(&client->lock);
1831
1832 }
1833 /* And anyone still marked as a 1 means a leaked handle somewhere */
1834 for (n = rb_first(&dev->buffers); n; n = rb_next(n)) {
1835 struct ion_buffer *buf = rb_entry(n, struct ion_buffer,
1836 node);
1837
1838 if (buf->marked == 1)
1839 seq_printf(s, "%16.x %16.s %16.x %16.d\n",
1840 (int)buf, buf->heap->name, buf->size,
1841 atomic_read(&buf->ref.refcount));
1842 }
1843 mutex_unlock(&dev->lock);
1844 return 0;
1845}
1846
1847static int ion_debug_leak_open(struct inode *inode, struct file *file)
1848{
1849 return single_open(file, ion_debug_leak_show, inode->i_private);
1850}
1851
1852static const struct file_operations debug_leak_fops = {
1853 .open = ion_debug_leak_open,
1854 .read = seq_read,
1855 .llseek = seq_lseek,
1856 .release = single_release,
1857};
1858
1859
1860
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001861struct ion_device *ion_device_create(long (*custom_ioctl)
1862 (struct ion_client *client,
1863 unsigned int cmd,
1864 unsigned long arg))
1865{
1866 struct ion_device *idev;
1867 int ret;
1868
1869 idev = kzalloc(sizeof(struct ion_device), GFP_KERNEL);
1870 if (!idev)
1871 return ERR_PTR(-ENOMEM);
1872
1873 idev->dev.minor = MISC_DYNAMIC_MINOR;
1874 idev->dev.name = "ion";
1875 idev->dev.fops = &ion_fops;
1876 idev->dev.parent = NULL;
1877 ret = misc_register(&idev->dev);
1878 if (ret) {
1879 pr_err("ion: failed to register misc device.\n");
1880 return ERR_PTR(ret);
1881 }
1882
1883 idev->debug_root = debugfs_create_dir("ion", NULL);
1884 if (IS_ERR_OR_NULL(idev->debug_root))
1885 pr_err("ion: failed to create debug files.\n");
1886
1887 idev->custom_ioctl = custom_ioctl;
1888 idev->buffers = RB_ROOT;
1889 mutex_init(&idev->lock);
1890 idev->heaps = RB_ROOT;
1891 idev->user_clients = RB_ROOT;
1892 idev->kernel_clients = RB_ROOT;
Laura Abbott404f8242011-10-31 14:22:53 -07001893 debugfs_create_file("check_leaked_fds", 0664, idev->debug_root, idev,
1894 &debug_leak_fops);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001895 return idev;
1896}
1897
1898void ion_device_destroy(struct ion_device *dev)
1899{
1900 misc_deregister(&dev->dev);
1901 /* XXX need to free the heaps and clients ? */
1902 kfree(dev);
1903}