blob: 4e34e89f4f461c3b2d0af0c47171e3552d0b26b8 [file] [log] [blame]
Rebecca Schultz Zavin0c38bfd2011-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 Zavin0c38bfd2011-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
Steve Mucklef132c6c2012-06-06 18:30:57 -070018#include <linux/module.h>
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070019#include <linux/device.h>
20#include <linux/file.h>
21#include <linux/fs.h>
22#include <linux/anon_inodes.h>
23#include <linux/ion.h>
24#include <linux/list.h>
Laura Abbottb14ed962012-01-30 14:18:08 -080025#include <linux/memblock.h>
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070026#include <linux/miscdevice.h>
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070027#include <linux/mm.h>
28#include <linux/mm_types.h>
29#include <linux/rbtree.h>
30#include <linux/sched.h>
31#include <linux/slab.h>
32#include <linux/seq_file.h>
33#include <linux/uaccess.h>
34#include <linux/debugfs.h>
Laura Abbottb14ed962012-01-30 14:18:08 -080035#include <linux/dma-buf.h>
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070036
Laura Abbott8c017362011-09-22 20:59:12 -070037#include <mach/iommu_domains.h>
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070038#include "ion_priv.h"
39#define DEBUG
40
41/**
42 * struct ion_device - the metadata of the ion device node
43 * @dev: the actual misc device
44 * @buffers: an rb tree of all the existing buffers
45 * @lock: lock protecting the buffers & heaps trees
46 * @heaps: list of all the heaps in the system
47 * @user_clients: list of all the clients created from userspace
48 */
49struct ion_device {
50 struct miscdevice dev;
51 struct rb_root buffers;
52 struct mutex lock;
53 struct rb_root heaps;
54 long (*custom_ioctl) (struct ion_client *client, unsigned int cmd,
55 unsigned long arg);
Laura Abbottb14ed962012-01-30 14:18:08 -080056 struct rb_root clients;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070057 struct dentry *debug_root;
58};
59
60/**
61 * struct ion_client - a process/hw block local address space
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070062 * @node: node in the tree of all clients
63 * @dev: backpointer to ion device
64 * @handles: an rb tree of all the handles in this client
65 * @lock: lock protecting the tree of handles
66 * @heap_mask: mask of all supported heaps
67 * @name: used for debugging
68 * @task: used for debugging
69 *
70 * A client represents a list of buffers this client may access.
71 * The mutex stored here is used to protect both handles tree
72 * as well as the handles themselves, and should be held while modifying either.
73 */
74struct ion_client {
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070075 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 Zavin0c38bfd2011-06-29 19:44:29 -070081 struct task_struct *task;
82 pid_t pid;
83 struct dentry *debug_root;
84};
85
86/**
87 * ion_handle - a client local reference to a buffer
88 * @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
92 * @kmap_cnt: count of times this client has mapped to kernel
93 * @dmap_cnt: count of times this client has mapped for dma
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070094 *
95 * Modifications to node, map_cnt or mapping should be protected by the
96 * lock in the client. Other fields are never changed after initialization.
97 */
98struct ion_handle {
99 struct kref ref;
100 struct ion_client *client;
101 struct ion_buffer *buffer;
102 struct rb_node node;
103 unsigned int kmap_cnt;
Laura Abbott8c017362011-09-22 20:59:12 -0700104 unsigned int iommu_map_cnt;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700105};
106
Olav Hauganb3676592012-03-02 15:02:25 -0800107static void ion_iommu_release(struct kref *kref);
108
Laura Abbott8c017362011-09-22 20:59:12 -0700109static int ion_validate_buffer_flags(struct ion_buffer *buffer,
110 unsigned long flags)
111{
112 if (buffer->kmap_cnt || buffer->dmap_cnt || buffer->umap_cnt ||
113 buffer->iommu_map_cnt) {
114 if (buffer->flags != flags) {
115 pr_err("%s: buffer was already mapped with flags %lx,"
116 " cannot map with flags %lx\n", __func__,
117 buffer->flags, flags);
118 return 1;
119 }
120
121 } else {
122 buffer->flags = flags;
123 }
124 return 0;
125}
126
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700127/* this function should only be called while dev->lock is held */
128static void ion_buffer_add(struct ion_device *dev,
129 struct ion_buffer *buffer)
130{
131 struct rb_node **p = &dev->buffers.rb_node;
132 struct rb_node *parent = NULL;
133 struct ion_buffer *entry;
134
135 while (*p) {
136 parent = *p;
137 entry = rb_entry(parent, struct ion_buffer, node);
138
139 if (buffer < entry) {
140 p = &(*p)->rb_left;
141 } else if (buffer > entry) {
142 p = &(*p)->rb_right;
143 } else {
144 pr_err("%s: buffer already found.", __func__);
145 BUG();
146 }
147 }
148
149 rb_link_node(&buffer->node, parent, p);
150 rb_insert_color(&buffer->node, &dev->buffers);
151}
152
Olav Haugan0fa9b602012-01-25 11:50:38 -0800153static void ion_iommu_add(struct ion_buffer *buffer,
Laura Abbott8c017362011-09-22 20:59:12 -0700154 struct ion_iommu_map *iommu)
155{
156 struct rb_node **p = &buffer->iommu_maps.rb_node;
157 struct rb_node *parent = NULL;
158 struct ion_iommu_map *entry;
159
160 while (*p) {
161 parent = *p;
162 entry = rb_entry(parent, struct ion_iommu_map, node);
163
164 if (iommu->key < entry->key) {
165 p = &(*p)->rb_left;
166 } else if (iommu->key > entry->key) {
167 p = &(*p)->rb_right;
168 } else {
169 pr_err("%s: buffer %p already has mapping for domain %d"
170 " and partition %d\n", __func__,
171 buffer,
172 iommu_map_domain(iommu),
173 iommu_map_partition(iommu));
174 BUG();
175 }
176 }
177
178 rb_link_node(&iommu->node, parent, p);
179 rb_insert_color(&iommu->node, &buffer->iommu_maps);
180
181}
182
183static struct ion_iommu_map *ion_iommu_lookup(struct ion_buffer *buffer,
184 unsigned int domain_no,
185 unsigned int partition_no)
186{
187 struct rb_node **p = &buffer->iommu_maps.rb_node;
188 struct rb_node *parent = NULL;
189 struct ion_iommu_map *entry;
190 uint64_t key = domain_no;
191 key = key << 32 | partition_no;
192
193 while (*p) {
194 parent = *p;
195 entry = rb_entry(parent, struct ion_iommu_map, node);
196
197 if (key < entry->key)
198 p = &(*p)->rb_left;
199 else if (key > entry->key)
200 p = &(*p)->rb_right;
201 else
202 return entry;
203 }
204
205 return NULL;
206}
207
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700208/* this function should only be called while dev->lock is held */
209static struct ion_buffer *ion_buffer_create(struct ion_heap *heap,
210 struct ion_device *dev,
211 unsigned long len,
212 unsigned long align,
213 unsigned long flags)
214{
215 struct ion_buffer *buffer;
Laura Abbottb14ed962012-01-30 14:18:08 -0800216 struct sg_table *table;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700217 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);
227 if (ret) {
228 kfree(buffer);
229 return ERR_PTR(ret);
230 }
Laura Abbottb14ed962012-01-30 14:18:08 -0800231
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700232 buffer->dev = dev;
233 buffer->size = len;
Laura Abbottb14ed962012-01-30 14:18:08 -0800234
235 table = buffer->heap->ops->map_dma(buffer->heap, buffer);
236 if (IS_ERR_OR_NULL(table)) {
237 heap->ops->free(buffer);
238 kfree(buffer);
239 return ERR_PTR(PTR_ERR(table));
240 }
241 buffer->sg_table = table;
242
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700243 mutex_init(&buffer->lock);
244 ion_buffer_add(dev, buffer);
245 return buffer;
246}
247
Olav Hauganb3676592012-03-02 15:02:25 -0800248/**
249 * Check for delayed IOMMU unmapping. Also unmap any outstanding
250 * mappings which would otherwise have been leaked.
251 */
252static void ion_iommu_delayed_unmap(struct ion_buffer *buffer)
253{
254 struct ion_iommu_map *iommu_map;
255 struct rb_node *node;
256 const struct rb_root *rb = &(buffer->iommu_maps);
257 unsigned long ref_count;
258 unsigned int delayed_unmap;
259
260 mutex_lock(&buffer->lock);
261
262 while ((node = rb_first(rb)) != 0) {
263 iommu_map = rb_entry(node, struct ion_iommu_map, node);
264 ref_count = atomic_read(&iommu_map->ref.refcount);
265 delayed_unmap = iommu_map->flags & ION_IOMMU_UNMAP_DELAYED;
266
267 if ((delayed_unmap && ref_count > 1) || !delayed_unmap) {
268 pr_err("%s: Virtual memory address leak in domain %u, partition %u\n",
269 __func__, iommu_map->domain_info[DI_DOMAIN_NUM],
270 iommu_map->domain_info[DI_PARTITION_NUM]);
271 }
272 /* set ref count to 1 to force release */
273 kref_init(&iommu_map->ref);
274 kref_put(&iommu_map->ref, ion_iommu_release);
275 }
276
277 mutex_unlock(&buffer->lock);
278}
279
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700280static void ion_buffer_destroy(struct kref *kref)
281{
282 struct ion_buffer *buffer = container_of(kref, struct ion_buffer, ref);
283 struct ion_device *dev = buffer->dev;
284
Laura Abbottb14ed962012-01-30 14:18:08 -0800285 if (WARN_ON(buffer->kmap_cnt > 0))
286 buffer->heap->ops->unmap_kernel(buffer->heap, buffer);
287
288 buffer->heap->ops->unmap_dma(buffer->heap, buffer);
289
Olav Hauganb3676592012-03-02 15:02:25 -0800290 ion_iommu_delayed_unmap(buffer);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700291 buffer->heap->ops->free(buffer);
292 mutex_lock(&dev->lock);
293 rb_erase(&buffer->node, &dev->buffers);
294 mutex_unlock(&dev->lock);
295 kfree(buffer);
296}
297
298static void ion_buffer_get(struct ion_buffer *buffer)
299{
300 kref_get(&buffer->ref);
301}
302
303static int ion_buffer_put(struct ion_buffer *buffer)
304{
305 return kref_put(&buffer->ref, ion_buffer_destroy);
306}
307
308static struct ion_handle *ion_handle_create(struct ion_client *client,
309 struct ion_buffer *buffer)
310{
311 struct ion_handle *handle;
312
313 handle = kzalloc(sizeof(struct ion_handle), GFP_KERNEL);
314 if (!handle)
315 return ERR_PTR(-ENOMEM);
316 kref_init(&handle->ref);
317 rb_init_node(&handle->node);
318 handle->client = client;
319 ion_buffer_get(buffer);
320 handle->buffer = buffer;
321
322 return handle;
323}
324
Laura Abbottb14ed962012-01-30 14:18:08 -0800325static void ion_handle_kmap_put(struct ion_handle *);
326
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700327static void ion_handle_destroy(struct kref *kref)
328{
329 struct ion_handle *handle = container_of(kref, struct ion_handle, ref);
Laura Abbottb14ed962012-01-30 14:18:08 -0800330 struct ion_client *client = handle->client;
331 struct ion_buffer *buffer = handle->buffer;
332
333 mutex_lock(&client->lock);
334
335 mutex_lock(&buffer->lock);
336 while (handle->kmap_cnt)
337 ion_handle_kmap_put(handle);
338 mutex_unlock(&buffer->lock);
339
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700340 if (!RB_EMPTY_NODE(&handle->node))
Laura Abbottb14ed962012-01-30 14:18:08 -0800341 rb_erase(&handle->node, &client->handles);
342 mutex_unlock(&client->lock);
343
344 ion_buffer_put(buffer);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700345 kfree(handle);
346}
347
348struct ion_buffer *ion_handle_buffer(struct ion_handle *handle)
349{
350 return handle->buffer;
351}
352
353static void ion_handle_get(struct ion_handle *handle)
354{
355 kref_get(&handle->ref);
356}
357
358static int ion_handle_put(struct ion_handle *handle)
359{
360 return kref_put(&handle->ref, ion_handle_destroy);
361}
362
363static struct ion_handle *ion_handle_lookup(struct ion_client *client,
364 struct ion_buffer *buffer)
365{
366 struct rb_node *n;
367
368 for (n = rb_first(&client->handles); n; n = rb_next(n)) {
369 struct ion_handle *handle = rb_entry(n, struct ion_handle,
370 node);
371 if (handle->buffer == buffer)
372 return handle;
373 }
374 return NULL;
375}
376
377static bool ion_handle_validate(struct ion_client *client, struct ion_handle *handle)
378{
379 struct rb_node *n = client->handles.rb_node;
380
381 while (n) {
382 struct ion_handle *handle_node = rb_entry(n, struct ion_handle,
383 node);
384 if (handle < handle_node)
385 n = n->rb_left;
386 else if (handle > handle_node)
387 n = n->rb_right;
388 else
389 return true;
390 }
391 return false;
392}
393
394static void ion_handle_add(struct ion_client *client, struct ion_handle *handle)
395{
396 struct rb_node **p = &client->handles.rb_node;
397 struct rb_node *parent = NULL;
398 struct ion_handle *entry;
399
400 while (*p) {
401 parent = *p;
402 entry = rb_entry(parent, struct ion_handle, node);
403
404 if (handle < entry)
405 p = &(*p)->rb_left;
406 else if (handle > entry)
407 p = &(*p)->rb_right;
408 else
409 WARN(1, "%s: buffer already found.", __func__);
410 }
411
412 rb_link_node(&handle->node, parent, p);
413 rb_insert_color(&handle->node, &client->handles);
414}
415
416struct ion_handle *ion_alloc(struct ion_client *client, size_t len,
417 size_t align, unsigned int flags)
418{
419 struct rb_node *n;
420 struct ion_handle *handle;
421 struct ion_device *dev = client->dev;
422 struct ion_buffer *buffer = NULL;
Olav Haugan0a852512012-01-09 10:20:55 -0800423 unsigned long secure_allocation = flags & ION_SECURE;
Olav Haugan35e2f2f2012-01-11 17:31:47 -0800424 const unsigned int MAX_DBG_STR_LEN = 64;
425 char dbg_str[MAX_DBG_STR_LEN];
426 unsigned int dbg_str_idx = 0;
427
428 dbg_str[0] = '\0';
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700429
430 /*
431 * traverse the list of heaps available in this system in priority
432 * order. If the heap type is supported by the client, and matches the
433 * request of the caller allocate from it. Repeat until allocate has
434 * succeeded or all heaps have been tried
435 */
Laura Abbottb14ed962012-01-30 14:18:08 -0800436 if (WARN_ON(!len))
437 return ERR_PTR(-EINVAL);
438
439 len = PAGE_ALIGN(len);
440
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700441 mutex_lock(&dev->lock);
442 for (n = rb_first(&dev->heaps); n != NULL; n = rb_next(n)) {
443 struct ion_heap *heap = rb_entry(n, struct ion_heap, node);
444 /* if the client doesn't support this heap type */
445 if (!((1 << heap->type) & client->heap_mask))
446 continue;
447 /* if the caller didn't specify this heap type */
448 if (!((1 << heap->id) & flags))
449 continue;
Olav Haugan0a852512012-01-09 10:20:55 -0800450 /* Do not allow un-secure heap if secure is specified */
451 if (secure_allocation && (heap->type != ION_HEAP_TYPE_CP))
452 continue;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700453 buffer = ion_buffer_create(heap, dev, len, align, flags);
454 if (!IS_ERR_OR_NULL(buffer))
455 break;
Olav Haugan35e2f2f2012-01-11 17:31:47 -0800456 if (dbg_str_idx < MAX_DBG_STR_LEN) {
457 unsigned int len_left = MAX_DBG_STR_LEN-dbg_str_idx-1;
458 int ret_value = snprintf(&dbg_str[dbg_str_idx],
459 len_left, "%s ", heap->name);
460 if (ret_value >= len_left) {
461 /* overflow */
462 dbg_str[MAX_DBG_STR_LEN-1] = '\0';
463 dbg_str_idx = MAX_DBG_STR_LEN;
464 } else if (ret_value >= 0) {
465 dbg_str_idx += ret_value;
466 } else {
467 /* error */
468 dbg_str[MAX_DBG_STR_LEN-1] = '\0';
469 }
470 }
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700471 }
472 mutex_unlock(&dev->lock);
473
Laura Abbottb14ed962012-01-30 14:18:08 -0800474 if (buffer == NULL)
475 return ERR_PTR(-ENODEV);
476
477 if (IS_ERR(buffer)) {
Olav Haugan35e2f2f2012-01-11 17:31:47 -0800478 pr_debug("ION is unable to allocate 0x%x bytes (alignment: "
479 "0x%x) from heap(s) %sfor client %s with heap "
480 "mask 0x%x\n",
481 len, align, dbg_str, client->name, client->heap_mask);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700482 return ERR_PTR(PTR_ERR(buffer));
Olav Haugan35e2f2f2012-01-11 17:31:47 -0800483 }
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700484
485 handle = ion_handle_create(client, buffer);
486
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700487 /*
488 * ion_buffer_create will create a buffer with a ref_cnt of 1,
489 * and ion_handle_create will take a second reference, drop one here
490 */
491 ion_buffer_put(buffer);
492
Laura Abbottb14ed962012-01-30 14:18:08 -0800493 if (!IS_ERR(handle)) {
494 mutex_lock(&client->lock);
495 ion_handle_add(client, handle);
496 mutex_unlock(&client->lock);
497 }
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700498
Laura Abbottb14ed962012-01-30 14:18:08 -0800499
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700500 return handle;
501}
Olav Hauganbd2b6922012-01-25 09:28:55 -0800502EXPORT_SYMBOL(ion_alloc);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700503
504void ion_free(struct ion_client *client, struct ion_handle *handle)
505{
506 bool valid_handle;
507
508 BUG_ON(client != handle->client);
509
510 mutex_lock(&client->lock);
511 valid_handle = ion_handle_validate(client, handle);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700512 if (!valid_handle) {
Laura Abbottec149ff2012-01-26 13:33:11 -0800513 mutex_unlock(&client->lock);
Olav Haugan6ede5672012-04-19 10:20:22 -0700514 WARN(1, "%s: invalid handle passed to free.\n", __func__);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700515 return;
516 }
Laura Abbottec149ff2012-01-26 13:33:11 -0800517 mutex_unlock(&client->lock);
Laura Abbottb14ed962012-01-30 14:18:08 -0800518 ion_handle_put(handle);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700519}
Olav Hauganbd2b6922012-01-25 09:28:55 -0800520EXPORT_SYMBOL(ion_free);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700521
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700522int ion_phys(struct ion_client *client, struct ion_handle *handle,
523 ion_phys_addr_t *addr, size_t *len)
524{
525 struct ion_buffer *buffer;
526 int ret;
527
528 mutex_lock(&client->lock);
529 if (!ion_handle_validate(client, handle)) {
530 mutex_unlock(&client->lock);
531 return -EINVAL;
532 }
533
534 buffer = handle->buffer;
535
536 if (!buffer->heap->ops->phys) {
537 pr_err("%s: ion_phys is not implemented by this heap.\n",
538 __func__);
539 mutex_unlock(&client->lock);
540 return -ENODEV;
541 }
542 mutex_unlock(&client->lock);
543 ret = buffer->heap->ops->phys(buffer->heap, buffer, addr, len);
544 return ret;
545}
Olav Hauganbd2b6922012-01-25 09:28:55 -0800546EXPORT_SYMBOL(ion_phys);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700547
Laura Abbottb14ed962012-01-30 14:18:08 -0800548static void *ion_buffer_kmap_get(struct ion_buffer *buffer)
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700549{
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700550 void *vaddr;
551
Laura Abbottb14ed962012-01-30 14:18:08 -0800552 if (buffer->kmap_cnt) {
553 buffer->kmap_cnt++;
554 return buffer->vaddr;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700555 }
Laura Abbottb14ed962012-01-30 14:18:08 -0800556 vaddr = buffer->heap->ops->map_kernel(buffer->heap, buffer);
557 if (IS_ERR_OR_NULL(vaddr))
558 return vaddr;
559 buffer->vaddr = vaddr;
560 buffer->kmap_cnt++;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700561 return vaddr;
562}
Laura Abbottb14ed962012-01-30 14:18:08 -0800563
564static void *ion_handle_kmap_get(struct ion_handle *handle)
565{
566 struct ion_buffer *buffer = handle->buffer;
567 void *vaddr;
568
569 if (handle->kmap_cnt) {
570 handle->kmap_cnt++;
571 return buffer->vaddr;
572 }
573 vaddr = ion_buffer_kmap_get(buffer);
574 if (IS_ERR_OR_NULL(vaddr))
575 return vaddr;
576 handle->kmap_cnt++;
577 return vaddr;
578}
579
580static void ion_buffer_kmap_put(struct ion_buffer *buffer)
581{
582 buffer->kmap_cnt--;
583 if (!buffer->kmap_cnt) {
584 buffer->heap->ops->unmap_kernel(buffer->heap, buffer);
585 buffer->vaddr = NULL;
586 }
587}
588
589static void ion_handle_kmap_put(struct ion_handle *handle)
590{
591 struct ion_buffer *buffer = handle->buffer;
592
593 handle->kmap_cnt--;
594 if (!handle->kmap_cnt)
595 ion_buffer_kmap_put(buffer);
596}
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700597
Olav Hauganb3676592012-03-02 15:02:25 -0800598static struct ion_iommu_map *__ion_iommu_map(struct ion_buffer *buffer,
Laura Abbott8c017362011-09-22 20:59:12 -0700599 int domain_num, int partition_num, unsigned long align,
600 unsigned long iova_length, unsigned long flags,
601 unsigned long *iova)
602{
603 struct ion_iommu_map *data;
604 int ret;
605
606 data = kmalloc(sizeof(*data), GFP_ATOMIC);
607
608 if (!data)
Olav Hauganb3676592012-03-02 15:02:25 -0800609 return ERR_PTR(-ENOMEM);
Laura Abbott8c017362011-09-22 20:59:12 -0700610
611 data->buffer = buffer;
612 iommu_map_domain(data) = domain_num;
613 iommu_map_partition(data) = partition_num;
614
615 ret = buffer->heap->ops->map_iommu(buffer, data,
616 domain_num,
617 partition_num,
618 align,
619 iova_length,
620 flags);
621
622 if (ret)
623 goto out;
624
625 kref_init(&data->ref);
626 *iova = data->iova_addr;
627
628 ion_iommu_add(buffer, data);
629
Olav Hauganb3676592012-03-02 15:02:25 -0800630 return data;
Laura Abbott8c017362011-09-22 20:59:12 -0700631
632out:
Laura Abbott8c017362011-09-22 20:59:12 -0700633 kfree(data);
Olav Hauganb3676592012-03-02 15:02:25 -0800634 return ERR_PTR(ret);
Laura Abbott8c017362011-09-22 20:59:12 -0700635}
636
637int ion_map_iommu(struct ion_client *client, struct ion_handle *handle,
638 int domain_num, int partition_num, unsigned long align,
639 unsigned long iova_length, unsigned long *iova,
640 unsigned long *buffer_size,
Olav Hauganb3676592012-03-02 15:02:25 -0800641 unsigned long flags, unsigned long iommu_flags)
Laura Abbott8c017362011-09-22 20:59:12 -0700642{
643 struct ion_buffer *buffer;
644 struct ion_iommu_map *iommu_map;
645 int ret = 0;
646
Olav Haugan79e9ffa2012-02-24 13:11:10 -0800647 if (ION_IS_CACHED(flags)) {
648 pr_err("%s: Cannot map iommu as cached.\n", __func__);
649 return -EINVAL;
650 }
651
Laura Abbott8c017362011-09-22 20:59:12 -0700652 mutex_lock(&client->lock);
653 if (!ion_handle_validate(client, handle)) {
654 pr_err("%s: invalid handle passed to map_kernel.\n",
655 __func__);
656 mutex_unlock(&client->lock);
657 return -EINVAL;
658 }
659
660 buffer = handle->buffer;
661 mutex_lock(&buffer->lock);
662
663 if (!handle->buffer->heap->ops->map_iommu) {
664 pr_err("%s: map_iommu is not implemented by this heap.\n",
665 __func__);
666 ret = -ENODEV;
667 goto out;
668 }
669
Laura Abbott8c017362011-09-22 20:59:12 -0700670 /*
671 * If clients don't want a custom iova length, just use whatever
672 * the buffer size is
673 */
674 if (!iova_length)
675 iova_length = buffer->size;
676
677 if (buffer->size > iova_length) {
678 pr_debug("%s: iova length %lx is not at least buffer size"
679 " %x\n", __func__, iova_length, buffer->size);
680 ret = -EINVAL;
681 goto out;
682 }
683
684 if (buffer->size & ~PAGE_MASK) {
685 pr_debug("%s: buffer size %x is not aligned to %lx", __func__,
686 buffer->size, PAGE_SIZE);
687 ret = -EINVAL;
688 goto out;
689 }
690
691 if (iova_length & ~PAGE_MASK) {
692 pr_debug("%s: iova_length %lx is not aligned to %lx", __func__,
693 iova_length, PAGE_SIZE);
694 ret = -EINVAL;
695 goto out;
696 }
697
698 iommu_map = ion_iommu_lookup(buffer, domain_num, partition_num);
Olav Hauganb3676592012-03-02 15:02:25 -0800699 if (!iommu_map) {
700 iommu_map = __ion_iommu_map(buffer, domain_num, partition_num,
701 align, iova_length, flags, iova);
Laura Abbottb14ed962012-01-30 14:18:08 -0800702 if (!IS_ERR_OR_NULL(iommu_map)) {
Olav Hauganb3676592012-03-02 15:02:25 -0800703 iommu_map->flags = iommu_flags;
704
705 if (iommu_map->flags & ION_IOMMU_UNMAP_DELAYED)
706 kref_get(&iommu_map->ref);
707 }
Laura Abbott8c017362011-09-22 20:59:12 -0700708 } else {
Olav Hauganb3676592012-03-02 15:02:25 -0800709 if (iommu_map->flags != iommu_flags) {
710 pr_err("%s: handle %p is already mapped with iommu flags %lx, trying to map with flags %lx\n",
711 __func__, handle,
712 iommu_map->flags, iommu_flags);
Olav Hauganb3676592012-03-02 15:02:25 -0800713 ret = -EINVAL;
714 } else if (iommu_map->mapped_size != iova_length) {
Laura Abbott8c017362011-09-22 20:59:12 -0700715 pr_err("%s: handle %p is already mapped with length"
Olav Hauganb3676592012-03-02 15:02:25 -0800716 " %x, trying to map with length %lx\n",
Laura Abbott8c017362011-09-22 20:59:12 -0700717 __func__, handle, iommu_map->mapped_size,
718 iova_length);
Laura Abbott8c017362011-09-22 20:59:12 -0700719 ret = -EINVAL;
720 } else {
721 kref_get(&iommu_map->ref);
722 *iova = iommu_map->iova_addr;
723 }
724 }
Laura Abbottb14ed962012-01-30 14:18:08 -0800725 if (!ret)
726 buffer->iommu_map_cnt++;
Laura Abbott8c017362011-09-22 20:59:12 -0700727 *buffer_size = buffer->size;
728out:
729 mutex_unlock(&buffer->lock);
730 mutex_unlock(&client->lock);
731 return ret;
732}
733EXPORT_SYMBOL(ion_map_iommu);
734
735static void ion_iommu_release(struct kref *kref)
736{
737 struct ion_iommu_map *map = container_of(kref, struct ion_iommu_map,
738 ref);
739 struct ion_buffer *buffer = map->buffer;
740
741 rb_erase(&map->node, &buffer->iommu_maps);
742 buffer->heap->ops->unmap_iommu(map);
743 kfree(map);
744}
745
746void ion_unmap_iommu(struct ion_client *client, struct ion_handle *handle,
747 int domain_num, int partition_num)
748{
749 struct ion_iommu_map *iommu_map;
750 struct ion_buffer *buffer;
751
752 mutex_lock(&client->lock);
753 buffer = handle->buffer;
754
755 mutex_lock(&buffer->lock);
756
757 iommu_map = ion_iommu_lookup(buffer, domain_num, partition_num);
758
759 if (!iommu_map) {
760 WARN(1, "%s: (%d,%d) was never mapped for %p\n", __func__,
761 domain_num, partition_num, buffer);
762 goto out;
763 }
764
Laura Abbott8c017362011-09-22 20:59:12 -0700765 kref_put(&iommu_map->ref, ion_iommu_release);
766
Laura Abbottb14ed962012-01-30 14:18:08 -0800767 buffer->iommu_map_cnt--;
Laura Abbott8c017362011-09-22 20:59:12 -0700768out:
769 mutex_unlock(&buffer->lock);
770
771 mutex_unlock(&client->lock);
772
773}
774EXPORT_SYMBOL(ion_unmap_iommu);
775
Laura Abbottb14ed962012-01-30 14:18:08 -0800776void *ion_map_kernel(struct ion_client *client, struct ion_handle *handle,
777 unsigned long flags)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700778{
779 struct ion_buffer *buffer;
Laura Abbottb14ed962012-01-30 14:18:08 -0800780 void *vaddr;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700781
782 mutex_lock(&client->lock);
783 if (!ion_handle_validate(client, handle)) {
Laura Abbottb14ed962012-01-30 14:18:08 -0800784 pr_err("%s: invalid handle passed to map_kernel.\n",
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700785 __func__);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700786 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700787 return ERR_PTR(-EINVAL);
788 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700789
Laura Abbottb14ed962012-01-30 14:18:08 -0800790 buffer = handle->buffer;
791
792 if (!handle->buffer->heap->ops->map_kernel) {
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700793 pr_err("%s: map_kernel is not implemented by this heap.\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(-ENODEV);
797 }
Laura Abbott894fd582011-08-19 13:33:56 -0700798
Laura Abbott8c017362011-09-22 20:59:12 -0700799 if (ion_validate_buffer_flags(buffer, flags)) {
Laura Abbottb14ed962012-01-30 14:18:08 -0800800 mutex_unlock(&client->lock);
801 return ERR_PTR(-EEXIST);
Laura Abbott894fd582011-08-19 13:33:56 -0700802 }
803
Laura Abbottb14ed962012-01-30 14:18:08 -0800804 mutex_lock(&buffer->lock);
805 vaddr = ion_handle_kmap_get(handle);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700806 mutex_unlock(&buffer->lock);
807 mutex_unlock(&client->lock);
Laura Abbottb14ed962012-01-30 14:18:08 -0800808 return vaddr;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700809}
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700810
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700811void ion_unmap_kernel(struct ion_client *client, struct ion_handle *handle)
812{
813 struct ion_buffer *buffer;
814
815 mutex_lock(&client->lock);
816 buffer = handle->buffer;
817 mutex_lock(&buffer->lock);
Laura Abbottb14ed962012-01-30 14:18:08 -0800818 ion_handle_kmap_put(handle);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700819 mutex_unlock(&buffer->lock);
820 mutex_unlock(&client->lock);
821}
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700822
Laura Abbottabcb6f72011-10-04 16:26:49 -0700823static int check_vaddr_bounds(unsigned long start, unsigned long end)
824{
825 struct mm_struct *mm = current->active_mm;
826 struct vm_area_struct *vma;
827 int ret = 1;
828
829 if (end < start)
830 goto out;
831
832 down_read(&mm->mmap_sem);
833 vma = find_vma(mm, start);
834 if (vma && vma->vm_start < end) {
835 if (start < vma->vm_start)
836 goto out_up;
837 if (end > vma->vm_end)
838 goto out_up;
839 ret = 0;
840 }
841
842out_up:
843 up_read(&mm->mmap_sem);
844out:
845 return ret;
846}
847
Olav Haugan41f85792012-02-08 15:28:05 -0800848int ion_do_cache_op(struct ion_client *client, struct ion_handle *handle,
Laura Abbottabcb6f72011-10-04 16:26:49 -0700849 void *uaddr, unsigned long offset, unsigned long len,
850 unsigned int cmd)
851{
852 struct ion_buffer *buffer;
Laura Abbottabcb6f72011-10-04 16:26:49 -0700853 int ret = -EINVAL;
854
855 mutex_lock(&client->lock);
856 if (!ion_handle_validate(client, handle)) {
857 pr_err("%s: invalid handle passed to do_cache_op.\n",
858 __func__);
859 mutex_unlock(&client->lock);
860 return -EINVAL;
861 }
862 buffer = handle->buffer;
863 mutex_lock(&buffer->lock);
864
Laura Abbottcbaa6682011-10-19 12:14:14 -0700865 if (!ION_IS_CACHED(buffer->flags)) {
Laura Abbottabcb6f72011-10-04 16:26:49 -0700866 ret = 0;
867 goto out;
868 }
869
870 if (!handle->buffer->heap->ops->cache_op) {
871 pr_err("%s: cache_op is not implemented by this heap.\n",
872 __func__);
873 ret = -ENODEV;
874 goto out;
875 }
876
Laura Abbottabcb6f72011-10-04 16:26:49 -0700877
878 ret = buffer->heap->ops->cache_op(buffer->heap, buffer, uaddr,
879 offset, len, cmd);
880
881out:
882 mutex_unlock(&buffer->lock);
883 mutex_unlock(&client->lock);
884 return ret;
885
886}
887
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700888static int ion_debug_client_show(struct seq_file *s, void *unused)
889{
890 struct ion_client *client = s->private;
891 struct rb_node *n;
Olav Haugan854c9e12012-05-16 16:34:28 -0700892 struct rb_node *n2;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700893
Olav Haugan854c9e12012-05-16 16:34:28 -0700894 seq_printf(s, "%16.16s: %16.16s : %16.16s : %12.12s : %12.12s : %s\n",
895 "heap_name", "size_in_bytes", "handle refcount",
896 "buffer", "physical", "[domain,partition] - virt");
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700897
898 mutex_lock(&client->lock);
899 for (n = rb_first(&client->handles); n; n = rb_next(n)) {
900 struct ion_handle *handle = rb_entry(n, struct ion_handle,
901 node);
902 enum ion_heap_type type = handle->buffer->heap->type;
903
Olav Haugan854c9e12012-05-16 16:34:28 -0700904 seq_printf(s, "%16.16s: %16x : %16d : %12p",
Laura Abbott68c80642011-10-21 17:32:27 -0700905 handle->buffer->heap->name,
906 handle->buffer->size,
907 atomic_read(&handle->ref.refcount),
908 handle->buffer);
Olav Haugan854c9e12012-05-16 16:34:28 -0700909
910 if (type == ION_HEAP_TYPE_SYSTEM_CONTIG ||
911 type == ION_HEAP_TYPE_CARVEOUT ||
912 type == ION_HEAP_TYPE_CP)
913 seq_printf(s, " : %12lx", handle->buffer->priv_phys);
914 else
915 seq_printf(s, " : %12s", "N/A");
916
917 for (n2 = rb_first(&handle->buffer->iommu_maps); n2;
918 n2 = rb_next(n2)) {
919 struct ion_iommu_map *imap =
920 rb_entry(n2, struct ion_iommu_map, node);
921 seq_printf(s, " : [%d,%d] - %8lx",
922 imap->domain_info[DI_DOMAIN_NUM],
923 imap->domain_info[DI_PARTITION_NUM],
924 imap->iova_addr);
925 }
926 seq_printf(s, "\n");
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700927 }
928 mutex_unlock(&client->lock);
929
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700930 return 0;
931}
932
933static int ion_debug_client_open(struct inode *inode, struct file *file)
934{
935 return single_open(file, ion_debug_client_show, inode->i_private);
936}
937
938static const struct file_operations debug_client_fops = {
939 .open = ion_debug_client_open,
940 .read = seq_read,
941 .llseek = seq_lseek,
942 .release = single_release,
943};
944
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700945struct ion_client *ion_client_create(struct ion_device *dev,
946 unsigned int heap_mask,
947 const char *name)
948{
949 struct ion_client *client;
950 struct task_struct *task;
951 struct rb_node **p;
952 struct rb_node *parent = NULL;
953 struct ion_client *entry;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700954 pid_t pid;
Olav Haugane8a31972012-05-16 13:11:41 -0700955 unsigned int name_len;
956
957 if (!name) {
958 pr_err("%s: Name cannot be null\n", __func__);
959 return ERR_PTR(-EINVAL);
960 }
961 name_len = strnlen(name, 64);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700962
963 get_task_struct(current->group_leader);
964 task_lock(current->group_leader);
965 pid = task_pid_nr(current->group_leader);
966 /* don't bother to store task struct for kernel threads,
967 they can't be killed anyway */
968 if (current->group_leader->flags & PF_KTHREAD) {
969 put_task_struct(current->group_leader);
970 task = NULL;
971 } else {
972 task = current->group_leader;
973 }
974 task_unlock(current->group_leader);
975
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700976 client = kzalloc(sizeof(struct ion_client), GFP_KERNEL);
977 if (!client) {
Laura Abbottb14ed962012-01-30 14:18:08 -0800978 if (task)
979 put_task_struct(current->group_leader);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700980 return ERR_PTR(-ENOMEM);
981 }
982
983 client->dev = dev;
984 client->handles = RB_ROOT;
985 mutex_init(&client->lock);
Olav Haugan63e5f3b2012-01-11 16:42:37 -0800986
Olav Haugan6625c7d2012-01-24 13:50:43 -0800987 client->name = kzalloc(name_len+1, GFP_KERNEL);
Olav Haugan63e5f3b2012-01-11 16:42:37 -0800988 if (!client->name) {
989 put_task_struct(current->group_leader);
990 kfree(client);
991 return ERR_PTR(-ENOMEM);
992 } else {
Olav Haugan6625c7d2012-01-24 13:50:43 -0800993 strlcpy(client->name, name, name_len+1);
Olav Haugan63e5f3b2012-01-11 16:42:37 -0800994 }
995
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700996 client->heap_mask = heap_mask;
997 client->task = task;
998 client->pid = pid;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700999
1000 mutex_lock(&dev->lock);
Laura Abbottb14ed962012-01-30 14:18:08 -08001001 p = &dev->clients.rb_node;
1002 while (*p) {
1003 parent = *p;
1004 entry = rb_entry(parent, struct ion_client, node);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001005
Laura Abbottb14ed962012-01-30 14:18:08 -08001006 if (client < entry)
1007 p = &(*p)->rb_left;
1008 else if (client > entry)
1009 p = &(*p)->rb_right;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001010 }
Laura Abbottb14ed962012-01-30 14:18:08 -08001011 rb_link_node(&client->node, parent, p);
1012 rb_insert_color(&client->node, &dev->clients);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001013
Laura Abbotteed86032011-12-05 15:32:36 -08001014
1015 client->debug_root = debugfs_create_file(name, 0664,
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001016 dev->debug_root, client,
1017 &debug_client_fops);
1018 mutex_unlock(&dev->lock);
1019
1020 return client;
1021}
1022
Laura Abbottb14ed962012-01-30 14:18:08 -08001023void ion_client_destroy(struct ion_client *client)
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001024{
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001025 struct ion_device *dev = client->dev;
1026 struct rb_node *n;
1027
1028 pr_debug("%s: %d\n", __func__, __LINE__);
1029 while ((n = rb_first(&client->handles))) {
1030 struct ion_handle *handle = rb_entry(n, struct ion_handle,
1031 node);
1032 ion_handle_destroy(&handle->ref);
1033 }
1034 mutex_lock(&dev->lock);
Laura Abbottb14ed962012-01-30 14:18:08 -08001035 if (client->task)
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001036 put_task_struct(client->task);
Laura Abbottb14ed962012-01-30 14:18:08 -08001037 rb_erase(&client->node, &dev->clients);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001038 debugfs_remove_recursive(client->debug_root);
1039 mutex_unlock(&dev->lock);
1040
Olav Haugan63e5f3b2012-01-11 16:42:37 -08001041 kfree(client->name);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001042 kfree(client);
1043}
1044
Laura Abbott273dd8e2011-10-12 14:26:33 -07001045int ion_handle_get_flags(struct ion_client *client, struct ion_handle *handle,
1046 unsigned long *flags)
Rebecca Schultz Zavin46d71332012-05-07 16:06:32 -07001047{
1048 struct ion_buffer *buffer;
Rebecca Schultz Zavin46d71332012-05-07 16:06:32 -07001049
1050 mutex_lock(&client->lock);
1051 if (!ion_handle_validate(client, handle)) {
Laura Abbott273dd8e2011-10-12 14:26:33 -07001052 pr_err("%s: invalid handle passed to %s.\n",
1053 __func__, __func__);
Rebecca Schultz Zavin46d71332012-05-07 16:06:32 -07001054 mutex_unlock(&client->lock);
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001055 return -EINVAL;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001056 }
Laura Abbott273dd8e2011-10-12 14:26:33 -07001057 buffer = handle->buffer;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001058 mutex_lock(&buffer->lock);
Laura Abbott273dd8e2011-10-12 14:26:33 -07001059 *flags = buffer->flags;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001060 mutex_unlock(&buffer->lock);
Laura Abbott273dd8e2011-10-12 14:26:33 -07001061 mutex_unlock(&client->lock);
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001062
Laura Abbott273dd8e2011-10-12 14:26:33 -07001063 return 0;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001064}
Laura Abbott273dd8e2011-10-12 14:26:33 -07001065EXPORT_SYMBOL(ion_handle_get_flags);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001066
Laura Abbott8c017362011-09-22 20:59:12 -07001067int ion_handle_get_size(struct ion_client *client, struct ion_handle *handle,
1068 unsigned long *size)
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001069{
Laura Abbott8c017362011-09-22 20:59:12 -07001070 struct ion_buffer *buffer;
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001071
Laura Abbott8c017362011-09-22 20:59:12 -07001072 mutex_lock(&client->lock);
1073 if (!ion_handle_validate(client, handle)) {
1074 pr_err("%s: invalid handle passed to %s.\n",
1075 __func__, __func__);
1076 mutex_unlock(&client->lock);
1077 return -EINVAL;
Rebecca Schultz Zavinbe4a1ee2012-04-26 20:44:10 -07001078 }
Laura Abbott8c017362011-09-22 20:59:12 -07001079 buffer = handle->buffer;
Rebecca Schultz Zavinbe4a1ee2012-04-26 20:44:10 -07001080 mutex_lock(&buffer->lock);
Laura Abbott8c017362011-09-22 20:59:12 -07001081 *size = buffer->size;
Rebecca Schultz Zavinbe4a1ee2012-04-26 20:44:10 -07001082 mutex_unlock(&buffer->lock);
Laura Abbott8c017362011-09-22 20:59:12 -07001083 mutex_unlock(&client->lock);
1084
1085 return 0;
1086}
1087EXPORT_SYMBOL(ion_handle_get_size);
1088
Laura Abbottb14ed962012-01-30 14:18:08 -08001089struct sg_table *ion_sg_table(struct ion_client *client,
1090 struct ion_handle *handle)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001091{
Laura Abbottb14ed962012-01-30 14:18:08 -08001092 struct ion_buffer *buffer;
1093 struct sg_table *table;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001094
Laura Abbottb14ed962012-01-30 14:18:08 -08001095 mutex_lock(&client->lock);
1096 if (!ion_handle_validate(client, handle)) {
1097 pr_err("%s: invalid handle passed to map_dma.\n",
1098 __func__);
1099 mutex_unlock(&client->lock);
1100 return ERR_PTR(-EINVAL);
1101 }
1102 buffer = handle->buffer;
1103 table = buffer->sg_table;
1104 mutex_unlock(&client->lock);
1105 return table;
1106}
1107
1108static struct sg_table *ion_map_dma_buf(struct dma_buf_attachment *attachment,
1109 enum dma_data_direction direction)
1110{
1111 struct dma_buf *dmabuf = attachment->dmabuf;
1112 struct ion_buffer *buffer = dmabuf->priv;
1113
1114 return buffer->sg_table;
1115}
1116
1117static void ion_unmap_dma_buf(struct dma_buf_attachment *attachment,
1118 struct sg_table *table,
1119 enum dma_data_direction direction)
1120{
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001121}
1122
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001123static void ion_vma_open(struct vm_area_struct *vma)
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001124{
Laura Abbottb14ed962012-01-30 14:18:08 -08001125 struct ion_buffer *buffer = vma->vm_private_data;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001126
1127 pr_debug("%s: %d\n", __func__, __LINE__);
Laura Abbottb14ed962012-01-30 14:18:08 -08001128
Rebecca Schultz Zavinbe4a1ee2012-04-26 20:44:10 -07001129 mutex_lock(&buffer->lock);
Laura Abbott77168502011-12-05 11:06:24 -08001130 buffer->umap_cnt++;
Rebecca Schultz Zavinbe4a1ee2012-04-26 20:44:10 -07001131 mutex_unlock(&buffer->lock);
1132}
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001133
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001134static void ion_vma_close(struct vm_area_struct *vma)
1135{
Laura Abbottb14ed962012-01-30 14:18:08 -08001136 struct ion_buffer *buffer = vma->vm_private_data;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001137
1138 pr_debug("%s: %d\n", __func__, __LINE__);
Laura Abbottb14ed962012-01-30 14:18:08 -08001139
Laura Abbott77168502011-12-05 11:06:24 -08001140 mutex_lock(&buffer->lock);
1141 buffer->umap_cnt--;
1142 mutex_unlock(&buffer->lock);
Laura Abbotta6835092011-11-14 15:27:02 -08001143
1144 if (buffer->heap->ops->unmap_user)
1145 buffer->heap->ops->unmap_user(buffer->heap, buffer);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001146}
1147
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001148static struct vm_operations_struct ion_vm_ops = {
1149 .open = ion_vma_open,
1150 .close = ion_vma_close,
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001151};
1152
Laura Abbottb14ed962012-01-30 14:18:08 -08001153static int ion_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma)
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001154{
Laura Abbottb14ed962012-01-30 14:18:08 -08001155 struct ion_buffer *buffer = dmabuf->priv;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001156 int ret;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001157
Laura Abbottb14ed962012-01-30 14:18:08 -08001158 if (!buffer->heap->ops->map_user) {
1159 pr_err("%s: this heap does not define a method for mapping "
1160 "to userspace\n", __func__);
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001161 return -EINVAL;
1162 }
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001163
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001164 mutex_lock(&buffer->lock);
1165 /* now map it to userspace */
Laura Abbottb14ed962012-01-30 14:18:08 -08001166 ret = buffer->heap->ops->map_user(buffer->heap, buffer, vma);
Laura Abbotte8bc7aa2011-12-09 14:49:33 -08001167
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001168 if (ret) {
Laura Abbottb14ed962012-01-30 14:18:08 -08001169 mutex_unlock(&buffer->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001170 pr_err("%s: failure mapping buffer to userspace\n",
1171 __func__);
Laura Abbottb14ed962012-01-30 14:18:08 -08001172 } else {
1173 buffer->umap_cnt++;
1174 mutex_unlock(&buffer->lock);
1175
1176 vma->vm_ops = &ion_vm_ops;
1177 /*
1178 * move the buffer into the vm_private_data so we can access it
1179 * from vma_open/close
1180 */
1181 vma->vm_private_data = buffer;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001182 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001183 return ret;
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001184}
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001185
Laura Abbottb14ed962012-01-30 14:18:08 -08001186static void ion_dma_buf_release(struct dma_buf *dmabuf)
1187{
1188 struct ion_buffer *buffer = dmabuf->priv;
1189 ion_buffer_put(buffer);
1190}
1191
1192static void *ion_dma_buf_kmap(struct dma_buf *dmabuf, unsigned long offset)
1193{
1194 struct ion_buffer *buffer = dmabuf->priv;
1195 return buffer->vaddr + offset;
1196}
1197
1198static void ion_dma_buf_kunmap(struct dma_buf *dmabuf, unsigned long offset,
1199 void *ptr)
1200{
1201 return;
1202}
1203
1204static int ion_dma_buf_begin_cpu_access(struct dma_buf *dmabuf, size_t start,
1205 size_t len,
1206 enum dma_data_direction direction)
1207{
1208 struct ion_buffer *buffer = dmabuf->priv;
1209 void *vaddr;
1210
1211 if (!buffer->heap->ops->map_kernel) {
1212 pr_err("%s: map kernel is not implemented by this heap.\n",
1213 __func__);
1214 return -ENODEV;
1215 }
1216
1217 mutex_lock(&buffer->lock);
1218 vaddr = ion_buffer_kmap_get(buffer);
1219 mutex_unlock(&buffer->lock);
1220 if (IS_ERR(vaddr))
1221 return PTR_ERR(vaddr);
1222 if (!vaddr)
1223 return -ENOMEM;
1224 return 0;
1225}
1226
1227static void ion_dma_buf_end_cpu_access(struct dma_buf *dmabuf, size_t start,
1228 size_t len,
1229 enum dma_data_direction direction)
1230{
1231 struct ion_buffer *buffer = dmabuf->priv;
1232
1233 mutex_lock(&buffer->lock);
1234 ion_buffer_kmap_put(buffer);
1235 mutex_unlock(&buffer->lock);
1236}
1237
1238struct dma_buf_ops dma_buf_ops = {
1239 .map_dma_buf = ion_map_dma_buf,
1240 .unmap_dma_buf = ion_unmap_dma_buf,
1241 .mmap = ion_mmap,
1242 .release = ion_dma_buf_release,
1243 .begin_cpu_access = ion_dma_buf_begin_cpu_access,
1244 .end_cpu_access = ion_dma_buf_end_cpu_access,
1245 .kmap_atomic = ion_dma_buf_kmap,
1246 .kunmap_atomic = ion_dma_buf_kunmap,
1247 .kmap = ion_dma_buf_kmap,
1248 .kunmap = ion_dma_buf_kunmap,
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001249};
1250
Laura Abbottb14ed962012-01-30 14:18:08 -08001251static int ion_share_set_flags(struct ion_client *client,
1252 struct ion_handle *handle,
1253 unsigned long flags)
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001254{
Laura Abbottb14ed962012-01-30 14:18:08 -08001255 struct ion_buffer *buffer;
1256 bool valid_handle;
1257 unsigned long ion_flags = ION_SET_CACHE(CACHED);
1258 if (flags & O_DSYNC)
1259 ion_flags = ION_SET_CACHE(UNCACHED);
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001260
Laura Abbottb14ed962012-01-30 14:18:08 -08001261 mutex_lock(&client->lock);
1262 valid_handle = ion_handle_validate(client, handle);
1263 mutex_unlock(&client->lock);
1264 if (!valid_handle) {
1265 WARN(1, "%s: invalid handle passed to set_flags.\n", __func__);
1266 return -EINVAL;
1267 }
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001268
Laura Abbottb14ed962012-01-30 14:18:08 -08001269 buffer = handle->buffer;
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001270
Laura Abbottb14ed962012-01-30 14:18:08 -08001271 mutex_lock(&buffer->lock);
1272 if (ion_validate_buffer_flags(buffer, ion_flags)) {
1273 mutex_unlock(&buffer->lock);
1274 return -EEXIST;
1275 }
1276 mutex_unlock(&buffer->lock);
1277 return 0;
1278}
Laura Abbott4b5d0482011-09-27 18:35:14 -07001279
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001280
Laura Abbottb14ed962012-01-30 14:18:08 -08001281int ion_share_dma_buf(struct ion_client *client, struct ion_handle *handle)
1282{
1283 struct ion_buffer *buffer;
1284 struct dma_buf *dmabuf;
1285 bool valid_handle;
1286 int fd;
1287
1288 mutex_lock(&client->lock);
1289 valid_handle = ion_handle_validate(client, handle);
1290 mutex_unlock(&client->lock);
1291 if (!valid_handle) {
1292 WARN("%s: invalid handle passed to share.\n", __func__);
1293 return -EINVAL;
1294 }
1295
1296 buffer = handle->buffer;
1297 ion_buffer_get(buffer);
1298 dmabuf = dma_buf_export(buffer, &dma_buf_ops, buffer->size, O_RDWR);
1299 if (IS_ERR(dmabuf)) {
1300 ion_buffer_put(buffer);
1301 return PTR_ERR(dmabuf);
1302 }
1303 fd = dma_buf_fd(dmabuf, O_CLOEXEC);
1304 if (fd < 0) {
1305 dma_buf_put(dmabuf);
1306 ion_buffer_put(buffer);
1307 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001308 return fd;
Laura Abbottb14ed962012-01-30 14:18:08 -08001309}
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001310
Laura Abbottb14ed962012-01-30 14:18:08 -08001311struct ion_handle *ion_import_dma_buf(struct ion_client *client, int fd)
1312{
1313 struct dma_buf *dmabuf;
1314 struct ion_buffer *buffer;
1315 struct ion_handle *handle;
1316
1317 dmabuf = dma_buf_get(fd);
1318 if (IS_ERR_OR_NULL(dmabuf))
1319 return ERR_PTR(PTR_ERR(dmabuf));
1320 /* if this memory came from ion */
1321
1322 if (dmabuf->ops != &dma_buf_ops) {
1323 pr_err("%s: can not import dmabuf from another exporter\n",
1324 __func__);
1325 dma_buf_put(dmabuf);
1326 return ERR_PTR(-EINVAL);
1327 }
1328 buffer = dmabuf->priv;
1329
1330 mutex_lock(&client->lock);
1331 /* if a handle exists for this buffer just take a reference to it */
1332 handle = ion_handle_lookup(client, buffer);
1333 if (!IS_ERR_OR_NULL(handle)) {
1334 ion_handle_get(handle);
1335 goto end;
1336 }
1337 handle = ion_handle_create(client, buffer);
1338 if (IS_ERR_OR_NULL(handle))
1339 goto end;
1340 ion_handle_add(client, handle);
1341end:
1342 mutex_unlock(&client->lock);
1343 dma_buf_put(dmabuf);
1344 return handle;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001345}
1346
1347static long ion_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1348{
1349 struct ion_client *client = filp->private_data;
1350
1351 switch (cmd) {
1352 case ION_IOC_ALLOC:
1353 {
1354 struct ion_allocation_data data;
1355
1356 if (copy_from_user(&data, (void __user *)arg, sizeof(data)))
1357 return -EFAULT;
1358 data.handle = ion_alloc(client, data.len, data.align,
1359 data.flags);
KyongHo Cho9ae7e012011-09-07 11:27:07 +09001360
Laura Abbottb14ed962012-01-30 14:18:08 -08001361 if (IS_ERR(data.handle))
1362 return PTR_ERR(data.handle);
KyongHo Cho9ae7e012011-09-07 11:27:07 +09001363
Laura Abbottb14ed962012-01-30 14:18:08 -08001364 if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
1365 ion_free(client, data.handle);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001366 return -EFAULT;
Laura Abbottb14ed962012-01-30 14:18:08 -08001367 }
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001368 break;
1369 }
1370 case ION_IOC_FREE:
1371 {
1372 struct ion_handle_data data;
1373 bool valid;
1374
1375 if (copy_from_user(&data, (void __user *)arg,
1376 sizeof(struct ion_handle_data)))
1377 return -EFAULT;
1378 mutex_lock(&client->lock);
1379 valid = ion_handle_validate(client, data.handle);
1380 mutex_unlock(&client->lock);
1381 if (!valid)
1382 return -EINVAL;
1383 ion_free(client, data.handle);
1384 break;
1385 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001386 case ION_IOC_MAP:
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001387 case ION_IOC_SHARE:
1388 {
1389 struct ion_fd_data data;
Laura Abbottb14ed962012-01-30 14:18:08 -08001390 int ret;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001391 if (copy_from_user(&data, (void __user *)arg, sizeof(data)))
1392 return -EFAULT;
Laura Abbottb14ed962012-01-30 14:18:08 -08001393
1394 ret = ion_share_set_flags(client, data.handle, filp->f_flags);
1395 if (ret)
1396 return ret;
1397
1398 data.fd = ion_share_dma_buf(client, data.handle);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001399 if (copy_to_user((void __user *)arg, &data, sizeof(data)))
1400 return -EFAULT;
Olav Hauganc2d2cf52012-05-15 14:40:11 -07001401 if (data.fd < 0)
1402 return data.fd;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001403 break;
1404 }
1405 case ION_IOC_IMPORT:
1406 {
1407 struct ion_fd_data data;
Olav Hauganc2d2cf52012-05-15 14:40:11 -07001408 int ret = 0;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001409 if (copy_from_user(&data, (void __user *)arg,
1410 sizeof(struct ion_fd_data)))
1411 return -EFAULT;
Laura Abbottb14ed962012-01-30 14:18:08 -08001412 data.handle = ion_import_dma_buf(client, data.fd);
1413 if (IS_ERR(data.handle))
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001414 data.handle = NULL;
1415 if (copy_to_user((void __user *)arg, &data,
1416 sizeof(struct ion_fd_data)))
1417 return -EFAULT;
Olav Hauganc2d2cf52012-05-15 14:40:11 -07001418 if (ret < 0)
1419 return ret;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001420 break;
1421 }
1422 case ION_IOC_CUSTOM:
1423 {
1424 struct ion_device *dev = client->dev;
1425 struct ion_custom_data data;
1426
1427 if (!dev->custom_ioctl)
1428 return -ENOTTY;
1429 if (copy_from_user(&data, (void __user *)arg,
1430 sizeof(struct ion_custom_data)))
1431 return -EFAULT;
1432 return dev->custom_ioctl(client, data.cmd, data.arg);
1433 }
Laura Abbottabcb6f72011-10-04 16:26:49 -07001434 case ION_IOC_CLEAN_CACHES:
1435 case ION_IOC_INV_CACHES:
1436 case ION_IOC_CLEAN_INV_CACHES:
1437 {
1438 struct ion_flush_data data;
Laura Abbott9fa29e82011-11-14 09:42:53 -08001439 unsigned long start, end;
Laura Abbotte80ea012011-11-18 18:36:47 -08001440 struct ion_handle *handle = NULL;
1441 int ret;
Laura Abbottabcb6f72011-10-04 16:26:49 -07001442
1443 if (copy_from_user(&data, (void __user *)arg,
1444 sizeof(struct ion_flush_data)))
1445 return -EFAULT;
1446
Laura Abbott9fa29e82011-11-14 09:42:53 -08001447 start = (unsigned long) data.vaddr;
1448 end = (unsigned long) data.vaddr + data.length;
1449
1450 if (check_vaddr_bounds(start, end)) {
1451 pr_err("%s: virtual address %p is out of bounds\n",
1452 __func__, data.vaddr);
1453 return -EINVAL;
1454 }
1455
Laura Abbotte80ea012011-11-18 18:36:47 -08001456 if (!data.handle) {
Laura Abbottb14ed962012-01-30 14:18:08 -08001457 handle = ion_import_dma_buf(client, data.fd);
1458 if (IS_ERR(handle)) {
Laura Abbotte80ea012011-11-18 18:36:47 -08001459 pr_info("%s: Could not import handle: %d\n",
1460 __func__, (int)handle);
1461 return -EINVAL;
1462 }
1463 }
1464
1465 ret = ion_do_cache_op(client,
1466 data.handle ? data.handle : handle,
1467 data.vaddr, data.offset, data.length,
1468 cmd);
1469
1470 if (!data.handle)
1471 ion_free(client, handle);
1472
Olav Haugand7baec02012-05-15 14:38:09 -07001473 if (ret < 0)
1474 return ret;
Laura Abbotte80ea012011-11-18 18:36:47 -08001475 break;
Laura Abbottabcb6f72011-10-04 16:26:49 -07001476
1477 }
Laura Abbott273dd8e2011-10-12 14:26:33 -07001478 case ION_IOC_GET_FLAGS:
1479 {
1480 struct ion_flag_data data;
1481 int ret;
1482 if (copy_from_user(&data, (void __user *)arg,
1483 sizeof(struct ion_flag_data)))
1484 return -EFAULT;
1485
1486 ret = ion_handle_get_flags(client, data.handle, &data.flags);
1487 if (ret < 0)
1488 return ret;
1489 if (copy_to_user((void __user *)arg, &data,
1490 sizeof(struct ion_flag_data)))
1491 return -EFAULT;
1492 break;
1493 }
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001494 default:
1495 return -ENOTTY;
1496 }
1497 return 0;
1498}
1499
1500static int ion_release(struct inode *inode, struct file *file)
1501{
1502 struct ion_client *client = file->private_data;
1503
1504 pr_debug("%s: %d\n", __func__, __LINE__);
Laura Abbottb14ed962012-01-30 14:18:08 -08001505 ion_client_destroy(client);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001506 return 0;
1507}
1508
1509static int ion_open(struct inode *inode, struct file *file)
1510{
1511 struct miscdevice *miscdev = file->private_data;
1512 struct ion_device *dev = container_of(miscdev, struct ion_device, dev);
1513 struct ion_client *client;
Laura Abbotteed86032011-12-05 15:32:36 -08001514 char debug_name[64];
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001515
1516 pr_debug("%s: %d\n", __func__, __LINE__);
Laura Abbotteed86032011-12-05 15:32:36 -08001517 snprintf(debug_name, 64, "%u", task_pid_nr(current->group_leader));
1518 client = ion_client_create(dev, -1, debug_name);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001519 if (IS_ERR_OR_NULL(client))
1520 return PTR_ERR(client);
1521 file->private_data = client;
1522
1523 return 0;
1524}
1525
1526static const struct file_operations ion_fops = {
1527 .owner = THIS_MODULE,
1528 .open = ion_open,
1529 .release = ion_release,
1530 .unlocked_ioctl = ion_ioctl,
1531};
1532
1533static size_t ion_debug_heap_total(struct ion_client *client,
Laura Abbott3647ac32011-10-31 14:09:53 -07001534 enum ion_heap_ids id)
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001535{
1536 size_t size = 0;
1537 struct rb_node *n;
1538
1539 mutex_lock(&client->lock);
1540 for (n = rb_first(&client->handles); n; n = rb_next(n)) {
1541 struct ion_handle *handle = rb_entry(n,
1542 struct ion_handle,
1543 node);
Laura Abbott3647ac32011-10-31 14:09:53 -07001544 if (handle->buffer->heap->id == id)
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001545 size += handle->buffer->size;
1546 }
1547 mutex_unlock(&client->lock);
1548 return size;
1549}
1550
Olav Haugan0671b9a2012-05-25 11:58:56 -07001551/**
1552 * Searches through a clients handles to find if the buffer is owned
1553 * by this client. Used for debug output.
1554 * @param client pointer to candidate owner of buffer
1555 * @param buf pointer to buffer that we are trying to find the owner of
1556 * @return 1 if found, 0 otherwise
1557 */
1558static int ion_debug_find_buffer_owner(const struct ion_client *client,
1559 const struct ion_buffer *buf)
1560{
1561 struct rb_node *n;
1562
1563 for (n = rb_first(&client->handles); n; n = rb_next(n)) {
1564 const struct ion_handle *handle = rb_entry(n,
1565 const struct ion_handle,
1566 node);
1567 if (handle->buffer == buf)
1568 return 1;
1569 }
1570 return 0;
1571}
1572
1573/**
1574 * Adds mem_map_data pointer to the tree of mem_map
1575 * Used for debug output.
1576 * @param mem_map The mem_map tree
1577 * @param data The new data to add to the tree
1578 */
1579static void ion_debug_mem_map_add(struct rb_root *mem_map,
1580 struct mem_map_data *data)
1581{
1582 struct rb_node **p = &mem_map->rb_node;
1583 struct rb_node *parent = NULL;
1584 struct mem_map_data *entry;
1585
1586 while (*p) {
1587 parent = *p;
1588 entry = rb_entry(parent, struct mem_map_data, node);
1589
1590 if (data->addr < entry->addr) {
1591 p = &(*p)->rb_left;
1592 } else if (data->addr > entry->addr) {
1593 p = &(*p)->rb_right;
1594 } else {
1595 pr_err("%s: mem_map_data already found.", __func__);
1596 BUG();
1597 }
1598 }
1599 rb_link_node(&data->node, parent, p);
1600 rb_insert_color(&data->node, mem_map);
1601}
1602
1603/**
1604 * Search for an owner of a buffer by iterating over all ION clients.
1605 * @param dev ion device containing pointers to all the clients.
1606 * @param buffer pointer to buffer we are trying to find the owner of.
1607 * @return name of owner.
1608 */
1609const char *ion_debug_locate_owner(const struct ion_device *dev,
1610 const struct ion_buffer *buffer)
1611{
1612 struct rb_node *j;
1613 const char *client_name = NULL;
1614
Laura Abbottb14ed962012-01-30 14:18:08 -08001615 for (j = rb_first(&dev->clients); j && !client_name;
Olav Haugan0671b9a2012-05-25 11:58:56 -07001616 j = rb_next(j)) {
1617 struct ion_client *client = rb_entry(j, struct ion_client,
1618 node);
1619 if (ion_debug_find_buffer_owner(client, buffer))
1620 client_name = client->name;
1621 }
1622 return client_name;
1623}
1624
1625/**
1626 * Create a mem_map of the heap.
1627 * @param s seq_file to log error message to.
1628 * @param heap The heap to create mem_map for.
1629 * @param mem_map The mem map to be created.
1630 */
1631void ion_debug_mem_map_create(struct seq_file *s, struct ion_heap *heap,
1632 struct rb_root *mem_map)
1633{
1634 struct ion_device *dev = heap->dev;
1635 struct rb_node *n;
1636
1637 for (n = rb_first(&dev->buffers); n; n = rb_next(n)) {
1638 struct ion_buffer *buffer =
1639 rb_entry(n, struct ion_buffer, node);
1640 if (buffer->heap->id == heap->id) {
1641 struct mem_map_data *data =
1642 kzalloc(sizeof(*data), GFP_KERNEL);
1643 if (!data) {
1644 seq_printf(s, "ERROR: out of memory. "
1645 "Part of memory map will not be logged\n");
1646 break;
1647 }
1648 data->addr = buffer->priv_phys;
1649 data->addr_end = buffer->priv_phys + buffer->size-1;
1650 data->size = buffer->size;
1651 data->client_name = ion_debug_locate_owner(dev, buffer);
1652 ion_debug_mem_map_add(mem_map, data);
1653 }
1654 }
1655}
1656
1657/**
1658 * Free the memory allocated by ion_debug_mem_map_create
1659 * @param mem_map The mem map to free.
1660 */
1661static void ion_debug_mem_map_destroy(struct rb_root *mem_map)
1662{
1663 if (mem_map) {
1664 struct rb_node *n;
1665 while ((n = rb_first(mem_map)) != 0) {
1666 struct mem_map_data *data =
1667 rb_entry(n, struct mem_map_data, node);
1668 rb_erase(&data->node, mem_map);
1669 kfree(data);
1670 }
1671 }
1672}
1673
1674/**
1675 * Print heap debug information.
1676 * @param s seq_file to log message to.
1677 * @param heap pointer to heap that we will print debug information for.
1678 */
1679static void ion_heap_print_debug(struct seq_file *s, struct ion_heap *heap)
1680{
1681 if (heap->ops->print_debug) {
1682 struct rb_root mem_map = RB_ROOT;
1683 ion_debug_mem_map_create(s, heap, &mem_map);
1684 heap->ops->print_debug(heap, s, &mem_map);
1685 ion_debug_mem_map_destroy(&mem_map);
1686 }
1687}
1688
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001689static int ion_debug_heap_show(struct seq_file *s, void *unused)
1690{
1691 struct ion_heap *heap = s->private;
1692 struct ion_device *dev = heap->dev;
1693 struct rb_node *n;
1694
Olav Haugane4900b52012-05-25 11:58:03 -07001695 mutex_lock(&dev->lock);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001696 seq_printf(s, "%16.s %16.s %16.s\n", "client", "pid", "size");
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001697
Laura Abbottb14ed962012-01-30 14:18:08 -08001698 for (n = rb_first(&dev->clients); n; n = rb_next(n)) {
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001699 struct ion_client *client = rb_entry(n, struct ion_client,
1700 node);
Laura Abbott3647ac32011-10-31 14:09:53 -07001701 size_t size = ion_debug_heap_total(client, heap->id);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001702 if (!size)
1703 continue;
Laura Abbottb14ed962012-01-30 14:18:08 -08001704 if (client->task) {
1705 char task_comm[TASK_COMM_LEN];
1706
1707 get_task_comm(task_comm, client->task);
1708 seq_printf(s, "%16.s %16u %16u\n", task_comm,
1709 client->pid, size);
1710 } else {
1711 seq_printf(s, "%16.s %16u %16u\n", client->name,
1712 client->pid, size);
1713 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001714 }
Olav Haugan0671b9a2012-05-25 11:58:56 -07001715 ion_heap_print_debug(s, heap);
Olav Haugane4900b52012-05-25 11:58:03 -07001716 mutex_unlock(&dev->lock);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001717 return 0;
1718}
1719
1720static int ion_debug_heap_open(struct inode *inode, struct file *file)
1721{
1722 return single_open(file, ion_debug_heap_show, inode->i_private);
1723}
1724
1725static const struct file_operations debug_heap_fops = {
1726 .open = ion_debug_heap_open,
1727 .read = seq_read,
1728 .llseek = seq_lseek,
1729 .release = single_release,
1730};
1731
1732void ion_device_add_heap(struct ion_device *dev, struct ion_heap *heap)
1733{
1734 struct rb_node **p = &dev->heaps.rb_node;
1735 struct rb_node *parent = NULL;
1736 struct ion_heap *entry;
1737
Laura Abbottb14ed962012-01-30 14:18:08 -08001738 if (!heap->ops->allocate || !heap->ops->free || !heap->ops->map_dma ||
1739 !heap->ops->unmap_dma)
1740 pr_err("%s: can not add heap with invalid ops struct.\n",
1741 __func__);
1742
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001743 heap->dev = dev;
1744 mutex_lock(&dev->lock);
1745 while (*p) {
1746 parent = *p;
1747 entry = rb_entry(parent, struct ion_heap, node);
1748
1749 if (heap->id < entry->id) {
1750 p = &(*p)->rb_left;
1751 } else if (heap->id > entry->id ) {
1752 p = &(*p)->rb_right;
1753 } else {
1754 pr_err("%s: can not insert multiple heaps with "
1755 "id %d\n", __func__, heap->id);
1756 goto end;
1757 }
1758 }
1759
1760 rb_link_node(&heap->node, parent, p);
1761 rb_insert_color(&heap->node, &dev->heaps);
1762 debugfs_create_file(heap->name, 0664, dev->debug_root, heap,
1763 &debug_heap_fops);
1764end:
1765 mutex_unlock(&dev->lock);
1766}
1767
Laura Abbott7e446482012-06-13 15:59:39 -07001768int ion_secure_heap(struct ion_device *dev, int heap_id, int version,
1769 void *data)
Olav Haugan0a852512012-01-09 10:20:55 -08001770{
1771 struct rb_node *n;
1772 int ret_val = 0;
1773
1774 /*
1775 * traverse the list of heaps available in this system
1776 * and find the heap that is specified.
1777 */
1778 mutex_lock(&dev->lock);
1779 for (n = rb_first(&dev->heaps); n != NULL; n = rb_next(n)) {
1780 struct ion_heap *heap = rb_entry(n, struct ion_heap, node);
1781 if (heap->type != ION_HEAP_TYPE_CP)
1782 continue;
1783 if (ION_HEAP(heap->id) != heap_id)
1784 continue;
1785 if (heap->ops->secure_heap)
Laura Abbott7e446482012-06-13 15:59:39 -07001786 ret_val = heap->ops->secure_heap(heap, version, data);
Olav Haugan0a852512012-01-09 10:20:55 -08001787 else
1788 ret_val = -EINVAL;
1789 break;
1790 }
1791 mutex_unlock(&dev->lock);
1792 return ret_val;
1793}
Olav Haugan0a852512012-01-09 10:20:55 -08001794
Laura Abbott7e446482012-06-13 15:59:39 -07001795int ion_unsecure_heap(struct ion_device *dev, int heap_id, int version,
1796 void *data)
Olav Haugan0a852512012-01-09 10:20:55 -08001797{
1798 struct rb_node *n;
1799 int ret_val = 0;
1800
1801 /*
1802 * traverse the list of heaps available in this system
1803 * and find the heap that is specified.
1804 */
1805 mutex_lock(&dev->lock);
1806 for (n = rb_first(&dev->heaps); n != NULL; n = rb_next(n)) {
1807 struct ion_heap *heap = rb_entry(n, struct ion_heap, node);
1808 if (heap->type != ION_HEAP_TYPE_CP)
1809 continue;
1810 if (ION_HEAP(heap->id) != heap_id)
1811 continue;
1812 if (heap->ops->secure_heap)
Laura Abbott7e446482012-06-13 15:59:39 -07001813 ret_val = heap->ops->unsecure_heap(heap, version, data);
Olav Haugan0a852512012-01-09 10:20:55 -08001814 else
1815 ret_val = -EINVAL;
1816 break;
1817 }
1818 mutex_unlock(&dev->lock);
1819 return ret_val;
1820}
Olav Haugan0a852512012-01-09 10:20:55 -08001821
Laura Abbott404f8242011-10-31 14:22:53 -07001822static int ion_debug_leak_show(struct seq_file *s, void *unused)
1823{
1824 struct ion_device *dev = s->private;
1825 struct rb_node *n;
1826 struct rb_node *n2;
1827
1828 /* mark all buffers as 1 */
1829 seq_printf(s, "%16.s %16.s %16.s %16.s\n", "buffer", "heap", "size",
1830 "ref cnt");
1831 mutex_lock(&dev->lock);
1832 for (n = rb_first(&dev->buffers); n; n = rb_next(n)) {
1833 struct ion_buffer *buf = rb_entry(n, struct ion_buffer,
1834 node);
1835
1836 buf->marked = 1;
1837 }
1838
1839 /* now see which buffers we can access */
Laura Abbottb14ed962012-01-30 14:18:08 -08001840 for (n = rb_first(&dev->clients); n; n = rb_next(n)) {
Laura Abbott404f8242011-10-31 14:22:53 -07001841 struct ion_client *client = rb_entry(n, struct ion_client,
1842 node);
1843
1844 mutex_lock(&client->lock);
1845 for (n2 = rb_first(&client->handles); n2; n2 = rb_next(n2)) {
1846 struct ion_handle *handle = rb_entry(n2,
1847 struct ion_handle, node);
1848
1849 handle->buffer->marked = 0;
1850
1851 }
1852 mutex_unlock(&client->lock);
1853
1854 }
1855
Laura Abbott404f8242011-10-31 14:22:53 -07001856 /* And anyone still marked as a 1 means a leaked handle somewhere */
1857 for (n = rb_first(&dev->buffers); n; n = rb_next(n)) {
1858 struct ion_buffer *buf = rb_entry(n, struct ion_buffer,
1859 node);
1860
1861 if (buf->marked == 1)
1862 seq_printf(s, "%16.x %16.s %16.x %16.d\n",
1863 (int)buf, buf->heap->name, buf->size,
1864 atomic_read(&buf->ref.refcount));
1865 }
1866 mutex_unlock(&dev->lock);
1867 return 0;
1868}
1869
1870static int ion_debug_leak_open(struct inode *inode, struct file *file)
1871{
1872 return single_open(file, ion_debug_leak_show, inode->i_private);
1873}
1874
1875static const struct file_operations debug_leak_fops = {
1876 .open = ion_debug_leak_open,
1877 .read = seq_read,
1878 .llseek = seq_lseek,
1879 .release = single_release,
1880};
1881
1882
1883
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001884struct ion_device *ion_device_create(long (*custom_ioctl)
1885 (struct ion_client *client,
1886 unsigned int cmd,
1887 unsigned long arg))
1888{
1889 struct ion_device *idev;
1890 int ret;
1891
1892 idev = kzalloc(sizeof(struct ion_device), GFP_KERNEL);
1893 if (!idev)
1894 return ERR_PTR(-ENOMEM);
1895
1896 idev->dev.minor = MISC_DYNAMIC_MINOR;
1897 idev->dev.name = "ion";
1898 idev->dev.fops = &ion_fops;
1899 idev->dev.parent = NULL;
1900 ret = misc_register(&idev->dev);
1901 if (ret) {
1902 pr_err("ion: failed to register misc device.\n");
1903 return ERR_PTR(ret);
1904 }
1905
1906 idev->debug_root = debugfs_create_dir("ion", NULL);
1907 if (IS_ERR_OR_NULL(idev->debug_root))
1908 pr_err("ion: failed to create debug files.\n");
1909
1910 idev->custom_ioctl = custom_ioctl;
1911 idev->buffers = RB_ROOT;
1912 mutex_init(&idev->lock);
1913 idev->heaps = RB_ROOT;
Laura Abbottb14ed962012-01-30 14:18:08 -08001914 idev->clients = RB_ROOT;
Laura Abbott404f8242011-10-31 14:22:53 -07001915 debugfs_create_file("check_leaked_fds", 0664, idev->debug_root, idev,
1916 &debug_leak_fops);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001917 return idev;
1918}
1919
1920void ion_device_destroy(struct ion_device *dev)
1921{
1922 misc_deregister(&dev->dev);
1923 /* XXX need to free the heaps and clients ? */
1924 kfree(dev);
1925}
Laura Abbottb14ed962012-01-30 14:18:08 -08001926
1927void __init ion_reserve(struct ion_platform_data *data)
1928{
1929 int i, ret;
1930
1931 for (i = 0; i < data->nr; i++) {
1932 if (data->heaps[i].size == 0)
1933 continue;
1934 ret = memblock_reserve(data->heaps[i].base,
1935 data->heaps[i].size);
1936 if (ret)
1937 pr_err("memblock reserve of %x@%lx failed\n",
1938 data->heaps[i].size,
1939 data->heaps[i].base);
1940 }
1941}