blob: 7ef8c152dbfa0079831a23af1ad851bab102a013 [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.
Liam Markcc2d4bd2013-01-16 10:14:40 -08005 * Copyright (c) 2011-2013, The Linux Foundation. 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>
Mitchel Humpherysd88b8eb2012-09-04 17:00:29 -070036#include <linux/msm_ion.h>
Liam Markcc2d4bd2013-01-16 10:14:40 -080037#include <trace/events/kmem.h>
38
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070039
Laura Abbott8c017362011-09-22 20:59:12 -070040#include <mach/iommu_domains.h>
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070041#include "ion_priv.h"
42#define DEBUG
43
44/**
45 * struct ion_device - the metadata of the ion device node
46 * @dev: the actual misc device
47 * @buffers: an rb tree of all the existing buffers
48 * @lock: lock protecting the buffers & heaps trees
49 * @heaps: list of all the heaps in the system
50 * @user_clients: list of all the clients created from userspace
51 */
52struct ion_device {
53 struct miscdevice dev;
54 struct rb_root buffers;
55 struct mutex lock;
56 struct rb_root heaps;
57 long (*custom_ioctl) (struct ion_client *client, unsigned int cmd,
58 unsigned long arg);
Laura Abbottb14ed962012-01-30 14:18:08 -080059 struct rb_root clients;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070060 struct dentry *debug_root;
61};
62
63/**
64 * struct ion_client - a process/hw block local address space
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070065 * @node: node in the tree of all clients
66 * @dev: backpointer to ion device
67 * @handles: an rb tree of all the handles in this client
68 * @lock: lock protecting the tree of handles
69 * @heap_mask: mask of all supported heaps
70 * @name: used for debugging
71 * @task: used for debugging
72 *
73 * A client represents a list of buffers this client may access.
74 * The mutex stored here is used to protect both handles tree
75 * as well as the handles themselves, and should be held while modifying either.
76 */
77struct ion_client {
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070078 struct rb_node node;
79 struct ion_device *dev;
80 struct rb_root handles;
81 struct mutex lock;
82 unsigned int heap_mask;
Olav Haugan63e5f3b2012-01-11 16:42:37 -080083 char *name;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070084 struct task_struct *task;
85 pid_t pid;
86 struct dentry *debug_root;
87};
88
89/**
90 * ion_handle - a client local reference to a buffer
91 * @ref: reference count
92 * @client: back pointer to the client the buffer resides in
93 * @buffer: pointer to the buffer
94 * @node: node in the client's handle rbtree
95 * @kmap_cnt: count of times this client has mapped to kernel
96 * @dmap_cnt: count of times this client has mapped for dma
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070097 *
98 * Modifications to node, map_cnt or mapping should be protected by the
99 * lock in the client. Other fields are never changed after initialization.
100 */
101struct ion_handle {
102 struct kref ref;
103 struct ion_client *client;
104 struct ion_buffer *buffer;
105 struct rb_node node;
106 unsigned int kmap_cnt;
Laura Abbott8c017362011-09-22 20:59:12 -0700107 unsigned int iommu_map_cnt;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700108};
109
Olav Hauganb3676592012-03-02 15:02:25 -0800110static void ion_iommu_release(struct kref *kref);
111
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700112/* this function should only be called while dev->lock is held */
113static void ion_buffer_add(struct ion_device *dev,
114 struct ion_buffer *buffer)
115{
116 struct rb_node **p = &dev->buffers.rb_node;
117 struct rb_node *parent = NULL;
118 struct ion_buffer *entry;
119
120 while (*p) {
121 parent = *p;
122 entry = rb_entry(parent, struct ion_buffer, node);
123
124 if (buffer < entry) {
125 p = &(*p)->rb_left;
126 } else if (buffer > entry) {
127 p = &(*p)->rb_right;
128 } else {
129 pr_err("%s: buffer already found.", __func__);
130 BUG();
131 }
132 }
133
134 rb_link_node(&buffer->node, parent, p);
135 rb_insert_color(&buffer->node, &dev->buffers);
136}
137
Olav Haugan0fa9b602012-01-25 11:50:38 -0800138static void ion_iommu_add(struct ion_buffer *buffer,
Laura Abbott8c017362011-09-22 20:59:12 -0700139 struct ion_iommu_map *iommu)
140{
141 struct rb_node **p = &buffer->iommu_maps.rb_node;
142 struct rb_node *parent = NULL;
143 struct ion_iommu_map *entry;
144
145 while (*p) {
146 parent = *p;
147 entry = rb_entry(parent, struct ion_iommu_map, node);
148
149 if (iommu->key < entry->key) {
150 p = &(*p)->rb_left;
151 } else if (iommu->key > entry->key) {
152 p = &(*p)->rb_right;
153 } else {
154 pr_err("%s: buffer %p already has mapping for domain %d"
155 " and partition %d\n", __func__,
156 buffer,
157 iommu_map_domain(iommu),
158 iommu_map_partition(iommu));
159 BUG();
160 }
161 }
162
163 rb_link_node(&iommu->node, parent, p);
164 rb_insert_color(&iommu->node, &buffer->iommu_maps);
165
166}
167
168static struct ion_iommu_map *ion_iommu_lookup(struct ion_buffer *buffer,
169 unsigned int domain_no,
170 unsigned int partition_no)
171{
172 struct rb_node **p = &buffer->iommu_maps.rb_node;
173 struct rb_node *parent = NULL;
174 struct ion_iommu_map *entry;
175 uint64_t key = domain_no;
176 key = key << 32 | partition_no;
177
178 while (*p) {
179 parent = *p;
180 entry = rb_entry(parent, struct ion_iommu_map, node);
181
182 if (key < entry->key)
183 p = &(*p)->rb_left;
184 else if (key > entry->key)
185 p = &(*p)->rb_right;
186 else
187 return entry;
188 }
189
190 return NULL;
191}
192
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700193/* this function should only be called while dev->lock is held */
194static struct ion_buffer *ion_buffer_create(struct ion_heap *heap,
195 struct ion_device *dev,
196 unsigned long len,
197 unsigned long align,
198 unsigned long flags)
199{
200 struct ion_buffer *buffer;
Laura Abbottb14ed962012-01-30 14:18:08 -0800201 struct sg_table *table;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700202 int ret;
203
204 buffer = kzalloc(sizeof(struct ion_buffer), GFP_KERNEL);
205 if (!buffer)
206 return ERR_PTR(-ENOMEM);
207
208 buffer->heap = heap;
209 kref_init(&buffer->ref);
210
211 ret = heap->ops->allocate(heap, buffer, len, align, flags);
212 if (ret) {
213 kfree(buffer);
214 return ERR_PTR(ret);
215 }
Laura Abbottb14ed962012-01-30 14:18:08 -0800216
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700217 buffer->dev = dev;
218 buffer->size = len;
Hanumant Singh7d72bad2012-08-29 18:39:44 -0700219 buffer->flags = flags;
Laura Abbottb14ed962012-01-30 14:18:08 -0800220
221 table = buffer->heap->ops->map_dma(buffer->heap, buffer);
222 if (IS_ERR_OR_NULL(table)) {
223 heap->ops->free(buffer);
224 kfree(buffer);
225 return ERR_PTR(PTR_ERR(table));
226 }
227 buffer->sg_table = table;
228
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700229 mutex_init(&buffer->lock);
230 ion_buffer_add(dev, buffer);
231 return buffer;
232}
233
Olav Hauganb3676592012-03-02 15:02:25 -0800234/**
235 * Check for delayed IOMMU unmapping. Also unmap any outstanding
236 * mappings which would otherwise have been leaked.
237 */
238static void ion_iommu_delayed_unmap(struct ion_buffer *buffer)
239{
240 struct ion_iommu_map *iommu_map;
241 struct rb_node *node;
242 const struct rb_root *rb = &(buffer->iommu_maps);
243 unsigned long ref_count;
244 unsigned int delayed_unmap;
245
246 mutex_lock(&buffer->lock);
247
248 while ((node = rb_first(rb)) != 0) {
249 iommu_map = rb_entry(node, struct ion_iommu_map, node);
250 ref_count = atomic_read(&iommu_map->ref.refcount);
251 delayed_unmap = iommu_map->flags & ION_IOMMU_UNMAP_DELAYED;
252
253 if ((delayed_unmap && ref_count > 1) || !delayed_unmap) {
254 pr_err("%s: Virtual memory address leak in domain %u, partition %u\n",
255 __func__, iommu_map->domain_info[DI_DOMAIN_NUM],
256 iommu_map->domain_info[DI_PARTITION_NUM]);
257 }
258 /* set ref count to 1 to force release */
259 kref_init(&iommu_map->ref);
260 kref_put(&iommu_map->ref, ion_iommu_release);
261 }
262
263 mutex_unlock(&buffer->lock);
264}
265
Laura Abbott93619302012-10-11 11:51:40 -0700266static void ion_delayed_unsecure(struct ion_buffer *buffer)
267{
268 if (buffer->heap->ops->unsecure_buffer)
269 buffer->heap->ops->unsecure_buffer(buffer, 1);
270}
271
Rebecca Schultz Zavin0c38bfd2011-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
Laura Abbottb14ed962012-01-30 14:18:08 -0800277 if (WARN_ON(buffer->kmap_cnt > 0))
278 buffer->heap->ops->unmap_kernel(buffer->heap, buffer);
279
280 buffer->heap->ops->unmap_dma(buffer->heap, buffer);
281
Laura Abbott93619302012-10-11 11:51:40 -0700282 ion_delayed_unsecure(buffer);
Olav Hauganb3676592012-03-02 15:02:25 -0800283 ion_iommu_delayed_unmap(buffer);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700284 buffer->heap->ops->free(buffer);
285 mutex_lock(&dev->lock);
286 rb_erase(&buffer->node, &dev->buffers);
287 mutex_unlock(&dev->lock);
288 kfree(buffer);
289}
290
291static void ion_buffer_get(struct ion_buffer *buffer)
292{
293 kref_get(&buffer->ref);
294}
295
296static int ion_buffer_put(struct ion_buffer *buffer)
297{
298 return kref_put(&buffer->ref, ion_buffer_destroy);
299}
300
301static struct ion_handle *ion_handle_create(struct ion_client *client,
302 struct ion_buffer *buffer)
303{
304 struct ion_handle *handle;
305
306 handle = kzalloc(sizeof(struct ion_handle), GFP_KERNEL);
307 if (!handle)
308 return ERR_PTR(-ENOMEM);
309 kref_init(&handle->ref);
310 rb_init_node(&handle->node);
311 handle->client = client;
312 ion_buffer_get(buffer);
313 handle->buffer = buffer;
314
315 return handle;
316}
317
Laura Abbottb14ed962012-01-30 14:18:08 -0800318static void ion_handle_kmap_put(struct ion_handle *);
319
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700320static void ion_handle_destroy(struct kref *kref)
321{
322 struct ion_handle *handle = container_of(kref, struct ion_handle, ref);
Laura Abbottb14ed962012-01-30 14:18:08 -0800323 struct ion_client *client = handle->client;
324 struct ion_buffer *buffer = handle->buffer;
325
Laura Abbottb14ed962012-01-30 14:18:08 -0800326 mutex_lock(&buffer->lock);
327 while (handle->kmap_cnt)
328 ion_handle_kmap_put(handle);
329 mutex_unlock(&buffer->lock);
330
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700331 if (!RB_EMPTY_NODE(&handle->node))
Laura Abbottb14ed962012-01-30 14:18:08 -0800332 rb_erase(&handle->node, &client->handles);
Laura Abbottb14ed962012-01-30 14:18:08 -0800333
334 ion_buffer_put(buffer);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700335 kfree(handle);
336}
337
338struct ion_buffer *ion_handle_buffer(struct ion_handle *handle)
339{
340 return handle->buffer;
341}
342
343static void ion_handle_get(struct ion_handle *handle)
344{
345 kref_get(&handle->ref);
346}
347
348static int ion_handle_put(struct ion_handle *handle)
349{
350 return kref_put(&handle->ref, ion_handle_destroy);
351}
352
353static struct ion_handle *ion_handle_lookup(struct ion_client *client,
354 struct ion_buffer *buffer)
355{
356 struct rb_node *n;
357
358 for (n = rb_first(&client->handles); n; n = rb_next(n)) {
359 struct ion_handle *handle = rb_entry(n, struct ion_handle,
360 node);
361 if (handle->buffer == buffer)
362 return handle;
363 }
364 return NULL;
365}
366
367static bool ion_handle_validate(struct ion_client *client, struct ion_handle *handle)
368{
369 struct rb_node *n = client->handles.rb_node;
370
371 while (n) {
372 struct ion_handle *handle_node = rb_entry(n, struct ion_handle,
373 node);
374 if (handle < handle_node)
375 n = n->rb_left;
376 else if (handle > handle_node)
377 n = n->rb_right;
378 else
379 return true;
380 }
381 return false;
382}
383
384static void ion_handle_add(struct ion_client *client, struct ion_handle *handle)
385{
386 struct rb_node **p = &client->handles.rb_node;
387 struct rb_node *parent = NULL;
388 struct ion_handle *entry;
389
390 while (*p) {
391 parent = *p;
392 entry = rb_entry(parent, struct ion_handle, node);
393
394 if (handle < entry)
395 p = &(*p)->rb_left;
396 else if (handle > entry)
397 p = &(*p)->rb_right;
398 else
399 WARN(1, "%s: buffer already found.", __func__);
400 }
401
402 rb_link_node(&handle->node, parent, p);
403 rb_insert_color(&handle->node, &client->handles);
404}
405
406struct ion_handle *ion_alloc(struct ion_client *client, size_t len,
Hanumant Singh7d72bad2012-08-29 18:39:44 -0700407 size_t align, unsigned int heap_mask,
408 unsigned int flags)
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700409{
410 struct rb_node *n;
411 struct ion_handle *handle;
412 struct ion_device *dev = client->dev;
413 struct ion_buffer *buffer = NULL;
Olav Haugan0a852512012-01-09 10:20:55 -0800414 unsigned long secure_allocation = flags & ION_SECURE;
Olav Haugan35e2f2f2012-01-11 17:31:47 -0800415 const unsigned int MAX_DBG_STR_LEN = 64;
416 char dbg_str[MAX_DBG_STR_LEN];
417 unsigned int dbg_str_idx = 0;
418
419 dbg_str[0] = '\0';
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700420
421 /*
422 * traverse the list of heaps available in this system in priority
423 * order. If the heap type is supported by the client, and matches the
424 * request of the caller allocate from it. Repeat until allocate has
425 * succeeded or all heaps have been tried
426 */
Laura Abbottb14ed962012-01-30 14:18:08 -0800427 if (WARN_ON(!len))
428 return ERR_PTR(-EINVAL);
429
430 len = PAGE_ALIGN(len);
431
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700432 mutex_lock(&dev->lock);
433 for (n = rb_first(&dev->heaps); n != NULL; n = rb_next(n)) {
434 struct ion_heap *heap = rb_entry(n, struct ion_heap, node);
435 /* if the client doesn't support this heap type */
436 if (!((1 << heap->type) & client->heap_mask))
437 continue;
438 /* if the caller didn't specify this heap type */
Hanumant Singh7d72bad2012-08-29 18:39:44 -0700439 if (!((1 << heap->id) & heap_mask))
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700440 continue;
Olav Haugan0a852512012-01-09 10:20:55 -0800441 /* Do not allow un-secure heap if secure is specified */
Mitchel Humpherys362b52b2012-09-13 10:53:22 -0700442 if (secure_allocation &&
443 (heap->type != (enum ion_heap_type) ION_HEAP_TYPE_CP))
Olav Haugan0a852512012-01-09 10:20:55 -0800444 continue;
Liam Markcc2d4bd2013-01-16 10:14:40 -0800445 trace_ion_alloc_buffer_start(client->name, heap->name, len,
446 heap_mask, flags);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700447 buffer = ion_buffer_create(heap, dev, len, align, flags);
Liam Markcc2d4bd2013-01-16 10:14:40 -0800448 trace_ion_alloc_buffer_end(client->name, heap->name, len,
449 heap_mask, flags);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700450 if (!IS_ERR_OR_NULL(buffer))
451 break;
Liam Markcc2d4bd2013-01-16 10:14:40 -0800452
453 trace_ion_alloc_buffer_fallback(client->name, heap->name, len,
454 heap_mask, flags, PTR_ERR(buffer));
Olav Haugan35e2f2f2012-01-11 17:31:47 -0800455 if (dbg_str_idx < MAX_DBG_STR_LEN) {
456 unsigned int len_left = MAX_DBG_STR_LEN-dbg_str_idx-1;
457 int ret_value = snprintf(&dbg_str[dbg_str_idx],
458 len_left, "%s ", heap->name);
459 if (ret_value >= len_left) {
460 /* overflow */
461 dbg_str[MAX_DBG_STR_LEN-1] = '\0';
462 dbg_str_idx = MAX_DBG_STR_LEN;
463 } else if (ret_value >= 0) {
464 dbg_str_idx += ret_value;
465 } else {
466 /* error */
467 dbg_str[MAX_DBG_STR_LEN-1] = '\0';
468 }
469 }
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700470 }
471 mutex_unlock(&dev->lock);
472
Liam Markcc2d4bd2013-01-16 10:14:40 -0800473 if (buffer == NULL) {
474 trace_ion_alloc_buffer_fail(client->name, dbg_str, len,
475 heap_mask, flags, -ENODEV);
Laura Abbottb14ed962012-01-30 14:18:08 -0800476 return ERR_PTR(-ENODEV);
Liam Markcc2d4bd2013-01-16 10:14:40 -0800477 }
Laura Abbottb14ed962012-01-30 14:18:08 -0800478
479 if (IS_ERR(buffer)) {
Liam Markcc2d4bd2013-01-16 10:14:40 -0800480 trace_ion_alloc_buffer_fail(client->name, dbg_str, len,
481 heap_mask, flags, PTR_ERR(buffer));
Olav Haugan35e2f2f2012-01-11 17:31:47 -0800482 pr_debug("ION is unable to allocate 0x%x bytes (alignment: "
483 "0x%x) from heap(s) %sfor client %s with heap "
484 "mask 0x%x\n",
485 len, align, dbg_str, client->name, client->heap_mask);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700486 return ERR_PTR(PTR_ERR(buffer));
Olav Haugan35e2f2f2012-01-11 17:31:47 -0800487 }
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700488
489 handle = ion_handle_create(client, buffer);
490
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700491 /*
492 * ion_buffer_create will create a buffer with a ref_cnt of 1,
493 * and ion_handle_create will take a second reference, drop one here
494 */
495 ion_buffer_put(buffer);
496
Laura Abbottb14ed962012-01-30 14:18:08 -0800497 if (!IS_ERR(handle)) {
498 mutex_lock(&client->lock);
499 ion_handle_add(client, handle);
500 mutex_unlock(&client->lock);
501 }
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700502
Laura Abbottb14ed962012-01-30 14:18:08 -0800503
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700504 return handle;
505}
Olav Hauganbd2b6922012-01-25 09:28:55 -0800506EXPORT_SYMBOL(ion_alloc);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700507
508void ion_free(struct ion_client *client, struct ion_handle *handle)
509{
510 bool valid_handle;
511
512 BUG_ON(client != handle->client);
513
514 mutex_lock(&client->lock);
515 valid_handle = ion_handle_validate(client, handle);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700516 if (!valid_handle) {
Laura Abbottec149ff2012-01-26 13:33:11 -0800517 mutex_unlock(&client->lock);
Olav Haugan6ede5672012-04-19 10:20:22 -0700518 WARN(1, "%s: invalid handle passed to free.\n", __func__);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700519 return;
520 }
Laura Abbottb14ed962012-01-30 14:18:08 -0800521 ion_handle_put(handle);
Rebecca Schultz Zavinaad11cb2012-08-20 15:41:11 -0700522 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700523}
Olav Hauganbd2b6922012-01-25 09:28:55 -0800524EXPORT_SYMBOL(ion_free);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700525
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700526int ion_phys(struct ion_client *client, struct ion_handle *handle,
527 ion_phys_addr_t *addr, size_t *len)
528{
529 struct ion_buffer *buffer;
530 int ret;
531
532 mutex_lock(&client->lock);
533 if (!ion_handle_validate(client, handle)) {
534 mutex_unlock(&client->lock);
535 return -EINVAL;
536 }
537
538 buffer = handle->buffer;
539
540 if (!buffer->heap->ops->phys) {
541 pr_err("%s: ion_phys is not implemented by this heap.\n",
542 __func__);
543 mutex_unlock(&client->lock);
544 return -ENODEV;
545 }
546 mutex_unlock(&client->lock);
547 ret = buffer->heap->ops->phys(buffer->heap, buffer, addr, len);
548 return ret;
549}
Olav Hauganbd2b6922012-01-25 09:28:55 -0800550EXPORT_SYMBOL(ion_phys);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700551
Laura Abbottb14ed962012-01-30 14:18:08 -0800552static void *ion_buffer_kmap_get(struct ion_buffer *buffer)
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700553{
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700554 void *vaddr;
555
Laura Abbottb14ed962012-01-30 14:18:08 -0800556 if (buffer->kmap_cnt) {
557 buffer->kmap_cnt++;
558 return buffer->vaddr;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700559 }
Laura Abbottb14ed962012-01-30 14:18:08 -0800560 vaddr = buffer->heap->ops->map_kernel(buffer->heap, buffer);
561 if (IS_ERR_OR_NULL(vaddr))
562 return vaddr;
563 buffer->vaddr = vaddr;
564 buffer->kmap_cnt++;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700565 return vaddr;
566}
Laura Abbottb14ed962012-01-30 14:18:08 -0800567
568static void *ion_handle_kmap_get(struct ion_handle *handle)
569{
570 struct ion_buffer *buffer = handle->buffer;
571 void *vaddr;
572
573 if (handle->kmap_cnt) {
574 handle->kmap_cnt++;
575 return buffer->vaddr;
576 }
577 vaddr = ion_buffer_kmap_get(buffer);
578 if (IS_ERR_OR_NULL(vaddr))
579 return vaddr;
580 handle->kmap_cnt++;
581 return vaddr;
582}
583
584static void ion_buffer_kmap_put(struct ion_buffer *buffer)
585{
586 buffer->kmap_cnt--;
587 if (!buffer->kmap_cnt) {
588 buffer->heap->ops->unmap_kernel(buffer->heap, buffer);
589 buffer->vaddr = NULL;
590 }
591}
592
593static void ion_handle_kmap_put(struct ion_handle *handle)
594{
595 struct ion_buffer *buffer = handle->buffer;
596
597 handle->kmap_cnt--;
598 if (!handle->kmap_cnt)
599 ion_buffer_kmap_put(buffer);
600}
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700601
Olav Hauganb3676592012-03-02 15:02:25 -0800602static struct ion_iommu_map *__ion_iommu_map(struct ion_buffer *buffer,
Laura Abbott8c017362011-09-22 20:59:12 -0700603 int domain_num, int partition_num, unsigned long align,
604 unsigned long iova_length, unsigned long flags,
605 unsigned long *iova)
606{
607 struct ion_iommu_map *data;
608 int ret;
609
610 data = kmalloc(sizeof(*data), GFP_ATOMIC);
611
612 if (!data)
Olav Hauganb3676592012-03-02 15:02:25 -0800613 return ERR_PTR(-ENOMEM);
Laura Abbott8c017362011-09-22 20:59:12 -0700614
615 data->buffer = buffer;
616 iommu_map_domain(data) = domain_num;
617 iommu_map_partition(data) = partition_num;
618
619 ret = buffer->heap->ops->map_iommu(buffer, data,
620 domain_num,
621 partition_num,
622 align,
623 iova_length,
624 flags);
625
626 if (ret)
627 goto out;
628
629 kref_init(&data->ref);
630 *iova = data->iova_addr;
631
632 ion_iommu_add(buffer, data);
633
Olav Hauganb3676592012-03-02 15:02:25 -0800634 return data;
Laura Abbott8c017362011-09-22 20:59:12 -0700635
636out:
Laura Abbott8c017362011-09-22 20:59:12 -0700637 kfree(data);
Olav Hauganb3676592012-03-02 15:02:25 -0800638 return ERR_PTR(ret);
Laura Abbott8c017362011-09-22 20:59:12 -0700639}
640
641int ion_map_iommu(struct ion_client *client, struct ion_handle *handle,
642 int domain_num, int partition_num, unsigned long align,
643 unsigned long iova_length, unsigned long *iova,
644 unsigned long *buffer_size,
Olav Hauganb3676592012-03-02 15:02:25 -0800645 unsigned long flags, unsigned long iommu_flags)
Laura Abbott8c017362011-09-22 20:59:12 -0700646{
647 struct ion_buffer *buffer;
648 struct ion_iommu_map *iommu_map;
649 int ret = 0;
650
Olav Haugan79e9ffa2012-02-24 13:11:10 -0800651 if (ION_IS_CACHED(flags)) {
652 pr_err("%s: Cannot map iommu as cached.\n", __func__);
653 return -EINVAL;
654 }
655
Laura Abbott8c017362011-09-22 20:59:12 -0700656 mutex_lock(&client->lock);
657 if (!ion_handle_validate(client, handle)) {
658 pr_err("%s: invalid handle passed to map_kernel.\n",
659 __func__);
660 mutex_unlock(&client->lock);
661 return -EINVAL;
662 }
663
664 buffer = handle->buffer;
665 mutex_lock(&buffer->lock);
666
667 if (!handle->buffer->heap->ops->map_iommu) {
668 pr_err("%s: map_iommu is not implemented by this heap.\n",
669 __func__);
670 ret = -ENODEV;
671 goto out;
672 }
673
Laura Abbott8c017362011-09-22 20:59:12 -0700674 /*
675 * If clients don't want a custom iova length, just use whatever
676 * the buffer size is
677 */
678 if (!iova_length)
679 iova_length = buffer->size;
680
681 if (buffer->size > iova_length) {
682 pr_debug("%s: iova length %lx is not at least buffer size"
683 " %x\n", __func__, iova_length, buffer->size);
684 ret = -EINVAL;
685 goto out;
686 }
687
688 if (buffer->size & ~PAGE_MASK) {
689 pr_debug("%s: buffer size %x is not aligned to %lx", __func__,
690 buffer->size, PAGE_SIZE);
691 ret = -EINVAL;
692 goto out;
693 }
694
695 if (iova_length & ~PAGE_MASK) {
696 pr_debug("%s: iova_length %lx is not aligned to %lx", __func__,
697 iova_length, PAGE_SIZE);
698 ret = -EINVAL;
699 goto out;
700 }
701
702 iommu_map = ion_iommu_lookup(buffer, domain_num, partition_num);
Olav Hauganb3676592012-03-02 15:02:25 -0800703 if (!iommu_map) {
704 iommu_map = __ion_iommu_map(buffer, domain_num, partition_num,
705 align, iova_length, flags, iova);
Laura Abbottb14ed962012-01-30 14:18:08 -0800706 if (!IS_ERR_OR_NULL(iommu_map)) {
Olav Hauganb3676592012-03-02 15:02:25 -0800707 iommu_map->flags = iommu_flags;
708
709 if (iommu_map->flags & ION_IOMMU_UNMAP_DELAYED)
710 kref_get(&iommu_map->ref);
Laura Abbott11bca602012-09-14 12:48:18 -0700711 } else {
712 ret = PTR_ERR(iommu_map);
Olav Hauganb3676592012-03-02 15:02:25 -0800713 }
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);
Olav Hauganb3676592012-03-02 15:02:25 -0800719 ret = -EINVAL;
720 } else if (iommu_map->mapped_size != iova_length) {
Laura Abbott8c017362011-09-22 20:59:12 -0700721 pr_err("%s: handle %p is already mapped with length"
Olav Hauganb3676592012-03-02 15:02:25 -0800722 " %x, trying to map with length %lx\n",
Laura Abbott8c017362011-09-22 20:59:12 -0700723 __func__, handle, iommu_map->mapped_size,
724 iova_length);
Laura Abbott8c017362011-09-22 20:59:12 -0700725 ret = -EINVAL;
726 } else {
727 kref_get(&iommu_map->ref);
728 *iova = iommu_map->iova_addr;
729 }
730 }
Laura Abbottb14ed962012-01-30 14:18:08 -0800731 if (!ret)
732 buffer->iommu_map_cnt++;
Laura Abbott8c017362011-09-22 20:59:12 -0700733 *buffer_size = buffer->size;
734out:
735 mutex_unlock(&buffer->lock);
736 mutex_unlock(&client->lock);
737 return ret;
738}
739EXPORT_SYMBOL(ion_map_iommu);
740
741static void ion_iommu_release(struct kref *kref)
742{
743 struct ion_iommu_map *map = container_of(kref, struct ion_iommu_map,
744 ref);
745 struct ion_buffer *buffer = map->buffer;
746
747 rb_erase(&map->node, &buffer->iommu_maps);
748 buffer->heap->ops->unmap_iommu(map);
749 kfree(map);
750}
751
752void ion_unmap_iommu(struct ion_client *client, struct ion_handle *handle,
753 int domain_num, int partition_num)
754{
755 struct ion_iommu_map *iommu_map;
756 struct ion_buffer *buffer;
757
758 mutex_lock(&client->lock);
759 buffer = handle->buffer;
760
761 mutex_lock(&buffer->lock);
762
763 iommu_map = ion_iommu_lookup(buffer, domain_num, partition_num);
764
765 if (!iommu_map) {
766 WARN(1, "%s: (%d,%d) was never mapped for %p\n", __func__,
767 domain_num, partition_num, buffer);
768 goto out;
769 }
770
Laura Abbott8c017362011-09-22 20:59:12 -0700771 kref_put(&iommu_map->ref, ion_iommu_release);
772
Laura Abbottb14ed962012-01-30 14:18:08 -0800773 buffer->iommu_map_cnt--;
Laura Abbott8c017362011-09-22 20:59:12 -0700774out:
775 mutex_unlock(&buffer->lock);
776
777 mutex_unlock(&client->lock);
778
779}
780EXPORT_SYMBOL(ion_unmap_iommu);
781
Mitchel Humpherys911b4b72012-09-12 14:42:50 -0700782void *ion_map_kernel(struct ion_client *client, struct ion_handle *handle)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700783{
784 struct ion_buffer *buffer;
Laura Abbottb14ed962012-01-30 14:18:08 -0800785 void *vaddr;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700786
787 mutex_lock(&client->lock);
788 if (!ion_handle_validate(client, handle)) {
Laura Abbottb14ed962012-01-30 14:18:08 -0800789 pr_err("%s: invalid handle passed to map_kernel.\n",
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700790 __func__);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700791 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700792 return ERR_PTR(-EINVAL);
793 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700794
Laura Abbottb14ed962012-01-30 14:18:08 -0800795 buffer = handle->buffer;
796
797 if (!handle->buffer->heap->ops->map_kernel) {
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700798 pr_err("%s: map_kernel is not implemented by this heap.\n",
799 __func__);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700800 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700801 return ERR_PTR(-ENODEV);
802 }
Laura Abbott894fd582011-08-19 13:33:56 -0700803
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}
Olav Hauganbd453a92012-07-05 14:21:34 -0700810EXPORT_SYMBOL(ion_map_kernel);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700811
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700812void ion_unmap_kernel(struct ion_client *client, struct ion_handle *handle)
813{
814 struct ion_buffer *buffer;
815
816 mutex_lock(&client->lock);
817 buffer = handle->buffer;
818 mutex_lock(&buffer->lock);
Laura Abbottb14ed962012-01-30 14:18:08 -0800819 ion_handle_kmap_put(handle);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700820 mutex_unlock(&buffer->lock);
821 mutex_unlock(&client->lock);
822}
Olav Hauganbd453a92012-07-05 14:21:34 -0700823EXPORT_SYMBOL(ion_unmap_kernel);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700824
Olav Haugan41f85792012-02-08 15:28:05 -0800825int ion_do_cache_op(struct ion_client *client, struct ion_handle *handle,
Laura Abbottabcb6f72011-10-04 16:26:49 -0700826 void *uaddr, unsigned long offset, unsigned long len,
827 unsigned int cmd)
828{
829 struct ion_buffer *buffer;
Laura Abbottabcb6f72011-10-04 16:26:49 -0700830 int ret = -EINVAL;
831
832 mutex_lock(&client->lock);
833 if (!ion_handle_validate(client, handle)) {
834 pr_err("%s: invalid handle passed to do_cache_op.\n",
835 __func__);
836 mutex_unlock(&client->lock);
837 return -EINVAL;
838 }
839 buffer = handle->buffer;
840 mutex_lock(&buffer->lock);
841
Laura Abbottcbaa6682011-10-19 12:14:14 -0700842 if (!ION_IS_CACHED(buffer->flags)) {
Laura Abbottabcb6f72011-10-04 16:26:49 -0700843 ret = 0;
844 goto out;
845 }
846
847 if (!handle->buffer->heap->ops->cache_op) {
848 pr_err("%s: cache_op is not implemented by this heap.\n",
849 __func__);
850 ret = -ENODEV;
851 goto out;
852 }
853
Laura Abbottabcb6f72011-10-04 16:26:49 -0700854
855 ret = buffer->heap->ops->cache_op(buffer->heap, buffer, uaddr,
856 offset, len, cmd);
857
858out:
859 mutex_unlock(&buffer->lock);
860 mutex_unlock(&client->lock);
861 return ret;
862
863}
Olav Hauganbd453a92012-07-05 14:21:34 -0700864EXPORT_SYMBOL(ion_do_cache_op);
Laura Abbottabcb6f72011-10-04 16:26:49 -0700865
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700866static int ion_debug_client_show(struct seq_file *s, void *unused)
867{
868 struct ion_client *client = s->private;
869 struct rb_node *n;
Olav Haugan854c9e12012-05-16 16:34:28 -0700870 struct rb_node *n2;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700871
Olav Haugan854c9e12012-05-16 16:34:28 -0700872 seq_printf(s, "%16.16s: %16.16s : %16.16s : %12.12s : %12.12s : %s\n",
873 "heap_name", "size_in_bytes", "handle refcount",
874 "buffer", "physical", "[domain,partition] - virt");
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700875
876 mutex_lock(&client->lock);
877 for (n = rb_first(&client->handles); n; n = rb_next(n)) {
878 struct ion_handle *handle = rb_entry(n, struct ion_handle,
879 node);
880 enum ion_heap_type type = handle->buffer->heap->type;
881
Olav Haugan854c9e12012-05-16 16:34:28 -0700882 seq_printf(s, "%16.16s: %16x : %16d : %12p",
Laura Abbott68c80642011-10-21 17:32:27 -0700883 handle->buffer->heap->name,
884 handle->buffer->size,
885 atomic_read(&handle->ref.refcount),
886 handle->buffer);
Olav Haugan854c9e12012-05-16 16:34:28 -0700887
888 if (type == ION_HEAP_TYPE_SYSTEM_CONTIG ||
889 type == ION_HEAP_TYPE_CARVEOUT ||
Mitchel Humpherys362b52b2012-09-13 10:53:22 -0700890 type == (enum ion_heap_type) ION_HEAP_TYPE_CP)
Olav Haugan854c9e12012-05-16 16:34:28 -0700891 seq_printf(s, " : %12lx", handle->buffer->priv_phys);
892 else
893 seq_printf(s, " : %12s", "N/A");
894
895 for (n2 = rb_first(&handle->buffer->iommu_maps); n2;
896 n2 = rb_next(n2)) {
897 struct ion_iommu_map *imap =
898 rb_entry(n2, struct ion_iommu_map, node);
899 seq_printf(s, " : [%d,%d] - %8lx",
900 imap->domain_info[DI_DOMAIN_NUM],
901 imap->domain_info[DI_PARTITION_NUM],
902 imap->iova_addr);
903 }
904 seq_printf(s, "\n");
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700905 }
906 mutex_unlock(&client->lock);
907
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700908 return 0;
909}
910
911static int ion_debug_client_open(struct inode *inode, struct file *file)
912{
913 return single_open(file, ion_debug_client_show, inode->i_private);
914}
915
916static const struct file_operations debug_client_fops = {
917 .open = ion_debug_client_open,
918 .read = seq_read,
919 .llseek = seq_lseek,
920 .release = single_release,
921};
922
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700923struct ion_client *ion_client_create(struct ion_device *dev,
924 unsigned int heap_mask,
925 const char *name)
926{
927 struct ion_client *client;
928 struct task_struct *task;
929 struct rb_node **p;
930 struct rb_node *parent = NULL;
931 struct ion_client *entry;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700932 pid_t pid;
Olav Haugane8a31972012-05-16 13:11:41 -0700933 unsigned int name_len;
934
935 if (!name) {
936 pr_err("%s: Name cannot be null\n", __func__);
937 return ERR_PTR(-EINVAL);
938 }
939 name_len = strnlen(name, 64);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700940
941 get_task_struct(current->group_leader);
942 task_lock(current->group_leader);
943 pid = task_pid_nr(current->group_leader);
944 /* don't bother to store task struct for kernel threads,
945 they can't be killed anyway */
946 if (current->group_leader->flags & PF_KTHREAD) {
947 put_task_struct(current->group_leader);
948 task = NULL;
949 } else {
950 task = current->group_leader;
951 }
952 task_unlock(current->group_leader);
953
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700954 client = kzalloc(sizeof(struct ion_client), GFP_KERNEL);
955 if (!client) {
Laura Abbottb14ed962012-01-30 14:18:08 -0800956 if (task)
957 put_task_struct(current->group_leader);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700958 return ERR_PTR(-ENOMEM);
959 }
960
961 client->dev = dev;
962 client->handles = RB_ROOT;
963 mutex_init(&client->lock);
Olav Haugan63e5f3b2012-01-11 16:42:37 -0800964
Olav Haugan6625c7d12012-01-24 13:50:43 -0800965 client->name = kzalloc(name_len+1, GFP_KERNEL);
Olav Haugan63e5f3b2012-01-11 16:42:37 -0800966 if (!client->name) {
967 put_task_struct(current->group_leader);
968 kfree(client);
969 return ERR_PTR(-ENOMEM);
970 } else {
Olav Haugan6625c7d12012-01-24 13:50:43 -0800971 strlcpy(client->name, name, name_len+1);
Olav Haugan63e5f3b2012-01-11 16:42:37 -0800972 }
973
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700974 client->heap_mask = heap_mask;
975 client->task = task;
976 client->pid = pid;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700977
978 mutex_lock(&dev->lock);
Laura Abbottb14ed962012-01-30 14:18:08 -0800979 p = &dev->clients.rb_node;
980 while (*p) {
981 parent = *p;
982 entry = rb_entry(parent, struct ion_client, node);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700983
Laura Abbottb14ed962012-01-30 14:18:08 -0800984 if (client < entry)
985 p = &(*p)->rb_left;
986 else if (client > entry)
987 p = &(*p)->rb_right;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700988 }
Laura Abbottb14ed962012-01-30 14:18:08 -0800989 rb_link_node(&client->node, parent, p);
990 rb_insert_color(&client->node, &dev->clients);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700991
Laura Abbotteed86032011-12-05 15:32:36 -0800992
993 client->debug_root = debugfs_create_file(name, 0664,
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700994 dev->debug_root, client,
995 &debug_client_fops);
996 mutex_unlock(&dev->lock);
997
998 return client;
999}
1000
Laura Abbottb14ed962012-01-30 14:18:08 -08001001void ion_client_destroy(struct ion_client *client)
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001002{
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001003 struct ion_device *dev = client->dev;
1004 struct rb_node *n;
1005
1006 pr_debug("%s: %d\n", __func__, __LINE__);
1007 while ((n = rb_first(&client->handles))) {
1008 struct ion_handle *handle = rb_entry(n, struct ion_handle,
1009 node);
1010 ion_handle_destroy(&handle->ref);
1011 }
1012 mutex_lock(&dev->lock);
Laura Abbottb14ed962012-01-30 14:18:08 -08001013 if (client->task)
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001014 put_task_struct(client->task);
Laura Abbottb14ed962012-01-30 14:18:08 -08001015 rb_erase(&client->node, &dev->clients);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001016 debugfs_remove_recursive(client->debug_root);
1017 mutex_unlock(&dev->lock);
1018
Olav Haugan63e5f3b2012-01-11 16:42:37 -08001019 kfree(client->name);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001020 kfree(client);
1021}
Olav Hauganbd453a92012-07-05 14:21:34 -07001022EXPORT_SYMBOL(ion_client_destroy);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001023
Laura Abbott273dd8e2011-10-12 14:26:33 -07001024int ion_handle_get_flags(struct ion_client *client, struct ion_handle *handle,
1025 unsigned long *flags)
Rebecca Schultz Zavin46d71332012-05-07 16:06:32 -07001026{
1027 struct ion_buffer *buffer;
Rebecca Schultz Zavin46d71332012-05-07 16:06:32 -07001028
1029 mutex_lock(&client->lock);
1030 if (!ion_handle_validate(client, handle)) {
Laura Abbott273dd8e2011-10-12 14:26:33 -07001031 pr_err("%s: invalid handle passed to %s.\n",
1032 __func__, __func__);
Rebecca Schultz Zavin46d71332012-05-07 16:06:32 -07001033 mutex_unlock(&client->lock);
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001034 return -EINVAL;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001035 }
Laura Abbott273dd8e2011-10-12 14:26:33 -07001036 buffer = handle->buffer;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001037 mutex_lock(&buffer->lock);
Laura Abbott273dd8e2011-10-12 14:26:33 -07001038 *flags = buffer->flags;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001039 mutex_unlock(&buffer->lock);
Laura Abbott273dd8e2011-10-12 14:26:33 -07001040 mutex_unlock(&client->lock);
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001041
Laura Abbott273dd8e2011-10-12 14:26:33 -07001042 return 0;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001043}
Laura Abbott273dd8e2011-10-12 14:26:33 -07001044EXPORT_SYMBOL(ion_handle_get_flags);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001045
Laura Abbott8c017362011-09-22 20:59:12 -07001046int ion_handle_get_size(struct ion_client *client, struct ion_handle *handle,
1047 unsigned long *size)
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001048{
Laura Abbott8c017362011-09-22 20:59:12 -07001049 struct ion_buffer *buffer;
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001050
Laura Abbott8c017362011-09-22 20:59:12 -07001051 mutex_lock(&client->lock);
1052 if (!ion_handle_validate(client, handle)) {
1053 pr_err("%s: invalid handle passed to %s.\n",
1054 __func__, __func__);
1055 mutex_unlock(&client->lock);
1056 return -EINVAL;
Rebecca Schultz Zavinbe4a1ee2012-04-26 20:44:10 -07001057 }
Laura Abbott8c017362011-09-22 20:59:12 -07001058 buffer = handle->buffer;
Rebecca Schultz Zavinbe4a1ee2012-04-26 20:44:10 -07001059 mutex_lock(&buffer->lock);
Laura Abbott8c017362011-09-22 20:59:12 -07001060 *size = buffer->size;
Rebecca Schultz Zavinbe4a1ee2012-04-26 20:44:10 -07001061 mutex_unlock(&buffer->lock);
Laura Abbott8c017362011-09-22 20:59:12 -07001062 mutex_unlock(&client->lock);
1063
1064 return 0;
1065}
1066EXPORT_SYMBOL(ion_handle_get_size);
1067
Laura Abbottb14ed962012-01-30 14:18:08 -08001068struct sg_table *ion_sg_table(struct ion_client *client,
1069 struct ion_handle *handle)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001070{
Laura Abbottb14ed962012-01-30 14:18:08 -08001071 struct ion_buffer *buffer;
1072 struct sg_table *table;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001073
Laura Abbottb14ed962012-01-30 14:18:08 -08001074 mutex_lock(&client->lock);
1075 if (!ion_handle_validate(client, handle)) {
1076 pr_err("%s: invalid handle passed to map_dma.\n",
1077 __func__);
1078 mutex_unlock(&client->lock);
1079 return ERR_PTR(-EINVAL);
1080 }
1081 buffer = handle->buffer;
1082 table = buffer->sg_table;
1083 mutex_unlock(&client->lock);
1084 return table;
1085}
Olav Hauganbd453a92012-07-05 14:21:34 -07001086EXPORT_SYMBOL(ion_sg_table);
Laura Abbottb14ed962012-01-30 14:18:08 -08001087
1088static struct sg_table *ion_map_dma_buf(struct dma_buf_attachment *attachment,
1089 enum dma_data_direction direction)
1090{
1091 struct dma_buf *dmabuf = attachment->dmabuf;
1092 struct ion_buffer *buffer = dmabuf->priv;
1093
1094 return buffer->sg_table;
1095}
1096
1097static void ion_unmap_dma_buf(struct dma_buf_attachment *attachment,
1098 struct sg_table *table,
1099 enum dma_data_direction direction)
1100{
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001101}
1102
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001103static void ion_vma_open(struct vm_area_struct *vma)
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001104{
Laura Abbottb14ed962012-01-30 14:18:08 -08001105 struct ion_buffer *buffer = vma->vm_private_data;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001106
1107 pr_debug("%s: %d\n", __func__, __LINE__);
Laura Abbottb14ed962012-01-30 14:18:08 -08001108
Rebecca Schultz Zavinbe4a1ee2012-04-26 20:44:10 -07001109 mutex_lock(&buffer->lock);
Laura Abbott77168502011-12-05 11:06:24 -08001110 buffer->umap_cnt++;
Rebecca Schultz Zavinbe4a1ee2012-04-26 20:44:10 -07001111 mutex_unlock(&buffer->lock);
1112}
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001113
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001114static void ion_vma_close(struct vm_area_struct *vma)
1115{
Laura Abbottb14ed962012-01-30 14:18:08 -08001116 struct ion_buffer *buffer = vma->vm_private_data;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001117
1118 pr_debug("%s: %d\n", __func__, __LINE__);
Laura Abbottb14ed962012-01-30 14:18:08 -08001119
Laura Abbott77168502011-12-05 11:06:24 -08001120 mutex_lock(&buffer->lock);
1121 buffer->umap_cnt--;
1122 mutex_unlock(&buffer->lock);
Laura Abbotta6835092011-11-14 15:27:02 -08001123
1124 if (buffer->heap->ops->unmap_user)
1125 buffer->heap->ops->unmap_user(buffer->heap, buffer);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001126}
1127
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001128static struct vm_operations_struct ion_vm_ops = {
1129 .open = ion_vma_open,
1130 .close = ion_vma_close,
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001131};
1132
Laura Abbottb14ed962012-01-30 14:18:08 -08001133static int ion_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma)
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001134{
Laura Abbottb14ed962012-01-30 14:18:08 -08001135 struct ion_buffer *buffer = dmabuf->priv;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001136 int ret;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001137
Laura Abbottb14ed962012-01-30 14:18:08 -08001138 if (!buffer->heap->ops->map_user) {
1139 pr_err("%s: this heap does not define a method for mapping "
1140 "to userspace\n", __func__);
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001141 return -EINVAL;
1142 }
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001143
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001144 mutex_lock(&buffer->lock);
1145 /* now map it to userspace */
Laura Abbottb14ed962012-01-30 14:18:08 -08001146 ret = buffer->heap->ops->map_user(buffer->heap, buffer, vma);
Laura Abbotte8bc7aa2011-12-09 14:49:33 -08001147
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001148 if (ret) {
Laura Abbottb14ed962012-01-30 14:18:08 -08001149 mutex_unlock(&buffer->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001150 pr_err("%s: failure mapping buffer to userspace\n",
1151 __func__);
Laura Abbottb14ed962012-01-30 14:18:08 -08001152 } else {
1153 buffer->umap_cnt++;
1154 mutex_unlock(&buffer->lock);
1155
1156 vma->vm_ops = &ion_vm_ops;
1157 /*
1158 * move the buffer into the vm_private_data so we can access it
1159 * from vma_open/close
1160 */
1161 vma->vm_private_data = buffer;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001162 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001163 return ret;
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001164}
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001165
Laura Abbottb14ed962012-01-30 14:18:08 -08001166static void ion_dma_buf_release(struct dma_buf *dmabuf)
1167{
1168 struct ion_buffer *buffer = dmabuf->priv;
1169 ion_buffer_put(buffer);
1170}
1171
1172static void *ion_dma_buf_kmap(struct dma_buf *dmabuf, unsigned long offset)
1173{
1174 struct ion_buffer *buffer = dmabuf->priv;
1175 return buffer->vaddr + offset;
1176}
1177
1178static void ion_dma_buf_kunmap(struct dma_buf *dmabuf, unsigned long offset,
1179 void *ptr)
1180{
1181 return;
1182}
1183
1184static int ion_dma_buf_begin_cpu_access(struct dma_buf *dmabuf, size_t start,
1185 size_t len,
1186 enum dma_data_direction direction)
1187{
1188 struct ion_buffer *buffer = dmabuf->priv;
1189 void *vaddr;
1190
1191 if (!buffer->heap->ops->map_kernel) {
1192 pr_err("%s: map kernel is not implemented by this heap.\n",
1193 __func__);
1194 return -ENODEV;
1195 }
1196
1197 mutex_lock(&buffer->lock);
1198 vaddr = ion_buffer_kmap_get(buffer);
1199 mutex_unlock(&buffer->lock);
1200 if (IS_ERR(vaddr))
1201 return PTR_ERR(vaddr);
1202 if (!vaddr)
1203 return -ENOMEM;
1204 return 0;
1205}
1206
1207static void ion_dma_buf_end_cpu_access(struct dma_buf *dmabuf, size_t start,
1208 size_t len,
1209 enum dma_data_direction direction)
1210{
1211 struct ion_buffer *buffer = dmabuf->priv;
1212
1213 mutex_lock(&buffer->lock);
1214 ion_buffer_kmap_put(buffer);
1215 mutex_unlock(&buffer->lock);
1216}
1217
1218struct dma_buf_ops dma_buf_ops = {
1219 .map_dma_buf = ion_map_dma_buf,
1220 .unmap_dma_buf = ion_unmap_dma_buf,
1221 .mmap = ion_mmap,
1222 .release = ion_dma_buf_release,
1223 .begin_cpu_access = ion_dma_buf_begin_cpu_access,
1224 .end_cpu_access = ion_dma_buf_end_cpu_access,
1225 .kmap_atomic = ion_dma_buf_kmap,
1226 .kunmap_atomic = ion_dma_buf_kunmap,
1227 .kmap = ion_dma_buf_kmap,
1228 .kunmap = ion_dma_buf_kunmap,
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001229};
1230
Laura Abbottb14ed962012-01-30 14:18:08 -08001231int ion_share_dma_buf(struct ion_client *client, struct ion_handle *handle)
1232{
1233 struct ion_buffer *buffer;
1234 struct dma_buf *dmabuf;
1235 bool valid_handle;
1236 int fd;
1237
1238 mutex_lock(&client->lock);
1239 valid_handle = ion_handle_validate(client, handle);
1240 mutex_unlock(&client->lock);
1241 if (!valid_handle) {
Olav Haugan0df59942012-07-05 14:27:30 -07001242 WARN(1, "%s: invalid handle passed to share.\n", __func__);
Laura Abbottb14ed962012-01-30 14:18:08 -08001243 return -EINVAL;
1244 }
1245
1246 buffer = handle->buffer;
1247 ion_buffer_get(buffer);
1248 dmabuf = dma_buf_export(buffer, &dma_buf_ops, buffer->size, O_RDWR);
1249 if (IS_ERR(dmabuf)) {
1250 ion_buffer_put(buffer);
1251 return PTR_ERR(dmabuf);
1252 }
1253 fd = dma_buf_fd(dmabuf, O_CLOEXEC);
Laura Abbottc2641f72012-08-01 18:06:18 -07001254 if (fd < 0)
Laura Abbottb14ed962012-01-30 14:18:08 -08001255 dma_buf_put(dmabuf);
Laura Abbottc2641f72012-08-01 18:06:18 -07001256
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001257 return fd;
Laura Abbottb14ed962012-01-30 14:18:08 -08001258}
Olav Hauganbd453a92012-07-05 14:21:34 -07001259EXPORT_SYMBOL(ion_share_dma_buf);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001260
Laura Abbottb14ed962012-01-30 14:18:08 -08001261struct ion_handle *ion_import_dma_buf(struct ion_client *client, int fd)
1262{
1263 struct dma_buf *dmabuf;
1264 struct ion_buffer *buffer;
1265 struct ion_handle *handle;
1266
1267 dmabuf = dma_buf_get(fd);
1268 if (IS_ERR_OR_NULL(dmabuf))
1269 return ERR_PTR(PTR_ERR(dmabuf));
1270 /* if this memory came from ion */
1271
1272 if (dmabuf->ops != &dma_buf_ops) {
1273 pr_err("%s: can not import dmabuf from another exporter\n",
1274 __func__);
1275 dma_buf_put(dmabuf);
1276 return ERR_PTR(-EINVAL);
1277 }
1278 buffer = dmabuf->priv;
1279
1280 mutex_lock(&client->lock);
1281 /* if a handle exists for this buffer just take a reference to it */
1282 handle = ion_handle_lookup(client, buffer);
1283 if (!IS_ERR_OR_NULL(handle)) {
1284 ion_handle_get(handle);
1285 goto end;
1286 }
1287 handle = ion_handle_create(client, buffer);
1288 if (IS_ERR_OR_NULL(handle))
1289 goto end;
1290 ion_handle_add(client, handle);
1291end:
1292 mutex_unlock(&client->lock);
1293 dma_buf_put(dmabuf);
1294 return handle;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001295}
Olav Hauganbd453a92012-07-05 14:21:34 -07001296EXPORT_SYMBOL(ion_import_dma_buf);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001297
1298static long ion_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1299{
1300 struct ion_client *client = filp->private_data;
1301
1302 switch (cmd) {
1303 case ION_IOC_ALLOC:
1304 {
1305 struct ion_allocation_data data;
1306
1307 if (copy_from_user(&data, (void __user *)arg, sizeof(data)))
1308 return -EFAULT;
1309 data.handle = ion_alloc(client, data.len, data.align,
Hanumant Singh7d72bad2012-08-29 18:39:44 -07001310 data.heap_mask, data.flags);
KyongHo Cho9ae7e012011-09-07 11:27:07 +09001311
Laura Abbottb14ed962012-01-30 14:18:08 -08001312 if (IS_ERR(data.handle))
1313 return PTR_ERR(data.handle);
KyongHo Cho9ae7e012011-09-07 11:27:07 +09001314
Laura Abbottb14ed962012-01-30 14:18:08 -08001315 if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
1316 ion_free(client, data.handle);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001317 return -EFAULT;
Laura Abbottb14ed962012-01-30 14:18:08 -08001318 }
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001319 break;
1320 }
1321 case ION_IOC_FREE:
1322 {
1323 struct ion_handle_data data;
1324 bool valid;
1325
1326 if (copy_from_user(&data, (void __user *)arg,
1327 sizeof(struct ion_handle_data)))
1328 return -EFAULT;
1329 mutex_lock(&client->lock);
1330 valid = ion_handle_validate(client, data.handle);
1331 mutex_unlock(&client->lock);
1332 if (!valid)
1333 return -EINVAL;
1334 ion_free(client, data.handle);
1335 break;
1336 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001337 case ION_IOC_MAP:
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001338 case ION_IOC_SHARE:
1339 {
1340 struct ion_fd_data data;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001341 if (copy_from_user(&data, (void __user *)arg, sizeof(data)))
1342 return -EFAULT;
Laura Abbottb14ed962012-01-30 14:18:08 -08001343
Laura Abbottb14ed962012-01-30 14:18:08 -08001344 data.fd = ion_share_dma_buf(client, data.handle);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001345 if (copy_to_user((void __user *)arg, &data, sizeof(data)))
1346 return -EFAULT;
Olav Hauganc2d2cf52012-05-15 14:40:11 -07001347 if (data.fd < 0)
1348 return data.fd;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001349 break;
1350 }
1351 case ION_IOC_IMPORT:
1352 {
1353 struct ion_fd_data data;
Olav Hauganc2d2cf52012-05-15 14:40:11 -07001354 int ret = 0;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001355 if (copy_from_user(&data, (void __user *)arg,
1356 sizeof(struct ion_fd_data)))
1357 return -EFAULT;
Laura Abbottb14ed962012-01-30 14:18:08 -08001358 data.handle = ion_import_dma_buf(client, data.fd);
Olav Haugan865e97f2012-05-15 14:40:11 -07001359 if (IS_ERR(data.handle)) {
1360 ret = PTR_ERR(data.handle);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001361 data.handle = NULL;
Olav Haugan865e97f2012-05-15 14:40:11 -07001362 }
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001363 if (copy_to_user((void __user *)arg, &data,
1364 sizeof(struct ion_fd_data)))
1365 return -EFAULT;
Olav Hauganc2d2cf52012-05-15 14:40:11 -07001366 if (ret < 0)
1367 return ret;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001368 break;
1369 }
1370 case ION_IOC_CUSTOM:
1371 {
1372 struct ion_device *dev = client->dev;
1373 struct ion_custom_data data;
1374
1375 if (!dev->custom_ioctl)
1376 return -ENOTTY;
1377 if (copy_from_user(&data, (void __user *)arg,
1378 sizeof(struct ion_custom_data)))
1379 return -EFAULT;
1380 return dev->custom_ioctl(client, data.cmd, data.arg);
1381 }
Laura Abbottabcb6f72011-10-04 16:26:49 -07001382 case ION_IOC_CLEAN_CACHES:
Mitchel Humpherysd88b8eb2012-09-04 17:00:29 -07001383 return client->dev->custom_ioctl(client,
1384 ION_IOC_CLEAN_CACHES, arg);
Laura Abbottabcb6f72011-10-04 16:26:49 -07001385 case ION_IOC_INV_CACHES:
Mitchel Humpherysd88b8eb2012-09-04 17:00:29 -07001386 return client->dev->custom_ioctl(client,
1387 ION_IOC_INV_CACHES, arg);
Laura Abbottabcb6f72011-10-04 16:26:49 -07001388 case ION_IOC_CLEAN_INV_CACHES:
Mitchel Humpherysd88b8eb2012-09-04 17:00:29 -07001389 return client->dev->custom_ioctl(client,
1390 ION_IOC_CLEAN_INV_CACHES, arg);
Laura Abbott273dd8e2011-10-12 14:26:33 -07001391 case ION_IOC_GET_FLAGS:
Mitchel Humpherysd88b8eb2012-09-04 17:00:29 -07001392 return client->dev->custom_ioctl(client,
1393 ION_IOC_GET_FLAGS, arg);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001394 default:
1395 return -ENOTTY;
1396 }
1397 return 0;
1398}
1399
1400static int ion_release(struct inode *inode, struct file *file)
1401{
1402 struct ion_client *client = file->private_data;
1403
1404 pr_debug("%s: %d\n", __func__, __LINE__);
Laura Abbottb14ed962012-01-30 14:18:08 -08001405 ion_client_destroy(client);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001406 return 0;
1407}
1408
1409static int ion_open(struct inode *inode, struct file *file)
1410{
1411 struct miscdevice *miscdev = file->private_data;
1412 struct ion_device *dev = container_of(miscdev, struct ion_device, dev);
1413 struct ion_client *client;
Laura Abbotteed86032011-12-05 15:32:36 -08001414 char debug_name[64];
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001415
1416 pr_debug("%s: %d\n", __func__, __LINE__);
Laura Abbotteed86032011-12-05 15:32:36 -08001417 snprintf(debug_name, 64, "%u", task_pid_nr(current->group_leader));
1418 client = ion_client_create(dev, -1, debug_name);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001419 if (IS_ERR_OR_NULL(client))
1420 return PTR_ERR(client);
1421 file->private_data = client;
1422
1423 return 0;
1424}
1425
1426static const struct file_operations ion_fops = {
1427 .owner = THIS_MODULE,
1428 .open = ion_open,
1429 .release = ion_release,
1430 .unlocked_ioctl = ion_ioctl,
1431};
1432
1433static size_t ion_debug_heap_total(struct ion_client *client,
Laura Abbott3647ac32011-10-31 14:09:53 -07001434 enum ion_heap_ids id)
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001435{
1436 size_t size = 0;
1437 struct rb_node *n;
1438
1439 mutex_lock(&client->lock);
1440 for (n = rb_first(&client->handles); n; n = rb_next(n)) {
1441 struct ion_handle *handle = rb_entry(n,
1442 struct ion_handle,
1443 node);
Laura Abbott3647ac32011-10-31 14:09:53 -07001444 if (handle->buffer->heap->id == id)
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001445 size += handle->buffer->size;
1446 }
1447 mutex_unlock(&client->lock);
1448 return size;
1449}
1450
Olav Haugan0671b9a2012-05-25 11:58:56 -07001451/**
1452 * Searches through a clients handles to find if the buffer is owned
1453 * by this client. Used for debug output.
1454 * @param client pointer to candidate owner of buffer
1455 * @param buf pointer to buffer that we are trying to find the owner of
1456 * @return 1 if found, 0 otherwise
1457 */
1458static int ion_debug_find_buffer_owner(const struct ion_client *client,
1459 const struct ion_buffer *buf)
1460{
1461 struct rb_node *n;
1462
1463 for (n = rb_first(&client->handles); n; n = rb_next(n)) {
1464 const struct ion_handle *handle = rb_entry(n,
1465 const struct ion_handle,
1466 node);
1467 if (handle->buffer == buf)
1468 return 1;
1469 }
1470 return 0;
1471}
1472
1473/**
1474 * Adds mem_map_data pointer to the tree of mem_map
1475 * Used for debug output.
1476 * @param mem_map The mem_map tree
1477 * @param data The new data to add to the tree
1478 */
1479static void ion_debug_mem_map_add(struct rb_root *mem_map,
1480 struct mem_map_data *data)
1481{
1482 struct rb_node **p = &mem_map->rb_node;
1483 struct rb_node *parent = NULL;
1484 struct mem_map_data *entry;
1485
1486 while (*p) {
1487 parent = *p;
1488 entry = rb_entry(parent, struct mem_map_data, node);
1489
1490 if (data->addr < entry->addr) {
1491 p = &(*p)->rb_left;
1492 } else if (data->addr > entry->addr) {
1493 p = &(*p)->rb_right;
1494 } else {
1495 pr_err("%s: mem_map_data already found.", __func__);
1496 BUG();
1497 }
1498 }
1499 rb_link_node(&data->node, parent, p);
1500 rb_insert_color(&data->node, mem_map);
1501}
1502
1503/**
1504 * Search for an owner of a buffer by iterating over all ION clients.
1505 * @param dev ion device containing pointers to all the clients.
1506 * @param buffer pointer to buffer we are trying to find the owner of.
1507 * @return name of owner.
1508 */
1509const char *ion_debug_locate_owner(const struct ion_device *dev,
1510 const struct ion_buffer *buffer)
1511{
1512 struct rb_node *j;
1513 const char *client_name = NULL;
1514
Laura Abbottb14ed962012-01-30 14:18:08 -08001515 for (j = rb_first(&dev->clients); j && !client_name;
Olav Haugan0671b9a2012-05-25 11:58:56 -07001516 j = rb_next(j)) {
1517 struct ion_client *client = rb_entry(j, struct ion_client,
1518 node);
1519 if (ion_debug_find_buffer_owner(client, buffer))
1520 client_name = client->name;
1521 }
1522 return client_name;
1523}
1524
1525/**
1526 * Create a mem_map of the heap.
1527 * @param s seq_file to log error message to.
1528 * @param heap The heap to create mem_map for.
1529 * @param mem_map The mem map to be created.
1530 */
1531void ion_debug_mem_map_create(struct seq_file *s, struct ion_heap *heap,
1532 struct rb_root *mem_map)
1533{
1534 struct ion_device *dev = heap->dev;
1535 struct rb_node *n;
1536
1537 for (n = rb_first(&dev->buffers); n; n = rb_next(n)) {
1538 struct ion_buffer *buffer =
1539 rb_entry(n, struct ion_buffer, node);
1540 if (buffer->heap->id == heap->id) {
1541 struct mem_map_data *data =
1542 kzalloc(sizeof(*data), GFP_KERNEL);
1543 if (!data) {
1544 seq_printf(s, "ERROR: out of memory. "
1545 "Part of memory map will not be logged\n");
1546 break;
1547 }
1548 data->addr = buffer->priv_phys;
1549 data->addr_end = buffer->priv_phys + buffer->size-1;
1550 data->size = buffer->size;
1551 data->client_name = ion_debug_locate_owner(dev, buffer);
1552 ion_debug_mem_map_add(mem_map, data);
1553 }
1554 }
1555}
1556
1557/**
1558 * Free the memory allocated by ion_debug_mem_map_create
1559 * @param mem_map The mem map to free.
1560 */
1561static void ion_debug_mem_map_destroy(struct rb_root *mem_map)
1562{
1563 if (mem_map) {
1564 struct rb_node *n;
1565 while ((n = rb_first(mem_map)) != 0) {
1566 struct mem_map_data *data =
1567 rb_entry(n, struct mem_map_data, node);
1568 rb_erase(&data->node, mem_map);
1569 kfree(data);
1570 }
1571 }
1572}
1573
1574/**
1575 * Print heap debug information.
1576 * @param s seq_file to log message to.
1577 * @param heap pointer to heap that we will print debug information for.
1578 */
1579static void ion_heap_print_debug(struct seq_file *s, struct ion_heap *heap)
1580{
1581 if (heap->ops->print_debug) {
1582 struct rb_root mem_map = RB_ROOT;
1583 ion_debug_mem_map_create(s, heap, &mem_map);
1584 heap->ops->print_debug(heap, s, &mem_map);
1585 ion_debug_mem_map_destroy(&mem_map);
1586 }
1587}
1588
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001589static int ion_debug_heap_show(struct seq_file *s, void *unused)
1590{
1591 struct ion_heap *heap = s->private;
1592 struct ion_device *dev = heap->dev;
1593 struct rb_node *n;
1594
Olav Haugane4900b52012-05-25 11:58:03 -07001595 mutex_lock(&dev->lock);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001596 seq_printf(s, "%16.s %16.s %16.s\n", "client", "pid", "size");
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001597
Laura Abbottb14ed962012-01-30 14:18:08 -08001598 for (n = rb_first(&dev->clients); n; n = rb_next(n)) {
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001599 struct ion_client *client = rb_entry(n, struct ion_client,
1600 node);
Laura Abbott3647ac32011-10-31 14:09:53 -07001601 size_t size = ion_debug_heap_total(client, heap->id);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001602 if (!size)
1603 continue;
Laura Abbottb14ed962012-01-30 14:18:08 -08001604 if (client->task) {
1605 char task_comm[TASK_COMM_LEN];
1606
1607 get_task_comm(task_comm, client->task);
1608 seq_printf(s, "%16.s %16u %16u\n", task_comm,
1609 client->pid, size);
1610 } else {
1611 seq_printf(s, "%16.s %16u %16u\n", client->name,
1612 client->pid, size);
1613 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001614 }
Olav Haugan0671b9a2012-05-25 11:58:56 -07001615 ion_heap_print_debug(s, heap);
Olav Haugane4900b52012-05-25 11:58:03 -07001616 mutex_unlock(&dev->lock);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001617 return 0;
1618}
1619
1620static int ion_debug_heap_open(struct inode *inode, struct file *file)
1621{
1622 return single_open(file, ion_debug_heap_show, inode->i_private);
1623}
1624
1625static const struct file_operations debug_heap_fops = {
1626 .open = ion_debug_heap_open,
1627 .read = seq_read,
1628 .llseek = seq_lseek,
1629 .release = single_release,
1630};
1631
1632void ion_device_add_heap(struct ion_device *dev, struct ion_heap *heap)
1633{
1634 struct rb_node **p = &dev->heaps.rb_node;
1635 struct rb_node *parent = NULL;
1636 struct ion_heap *entry;
1637
Laura Abbottb14ed962012-01-30 14:18:08 -08001638 if (!heap->ops->allocate || !heap->ops->free || !heap->ops->map_dma ||
1639 !heap->ops->unmap_dma)
1640 pr_err("%s: can not add heap with invalid ops struct.\n",
1641 __func__);
1642
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001643 heap->dev = dev;
1644 mutex_lock(&dev->lock);
1645 while (*p) {
1646 parent = *p;
1647 entry = rb_entry(parent, struct ion_heap, node);
1648
1649 if (heap->id < entry->id) {
1650 p = &(*p)->rb_left;
1651 } else if (heap->id > entry->id ) {
1652 p = &(*p)->rb_right;
1653 } else {
1654 pr_err("%s: can not insert multiple heaps with "
1655 "id %d\n", __func__, heap->id);
1656 goto end;
1657 }
1658 }
1659
1660 rb_link_node(&heap->node, parent, p);
1661 rb_insert_color(&heap->node, &dev->heaps);
1662 debugfs_create_file(heap->name, 0664, dev->debug_root, heap,
1663 &debug_heap_fops);
1664end:
1665 mutex_unlock(&dev->lock);
1666}
1667
Laura Abbott93619302012-10-11 11:51:40 -07001668int ion_secure_handle(struct ion_client *client, struct ion_handle *handle,
1669 int version, void *data, int flags)
1670{
1671 int ret = -EINVAL;
1672 struct ion_heap *heap;
1673 struct ion_buffer *buffer;
1674
1675 mutex_lock(&client->lock);
1676 if (!ion_handle_validate(client, handle)) {
1677 WARN(1, "%s: invalid handle passed to secure.\n", __func__);
1678 goto out_unlock;
1679 }
1680
1681 buffer = handle->buffer;
1682 heap = buffer->heap;
1683
1684 if (heap->type != (enum ion_heap_type) ION_HEAP_TYPE_CP) {
1685 pr_err("%s: cannot secure buffer from non secure heap\n",
1686 __func__);
1687 goto out_unlock;
1688 }
1689
1690 BUG_ON(!buffer->heap->ops->secure_buffer);
1691 /*
1692 * Protect the handle via the client lock to ensure we aren't
1693 * racing with free
1694 */
1695 ret = buffer->heap->ops->secure_buffer(buffer, version, data, flags);
1696
1697out_unlock:
1698 mutex_unlock(&client->lock);
1699 return ret;
1700}
1701
1702int ion_unsecure_handle(struct ion_client *client, struct ion_handle *handle)
1703{
1704 int ret = -EINVAL;
1705 struct ion_heap *heap;
1706 struct ion_buffer *buffer;
1707
1708 mutex_lock(&client->lock);
1709 if (!ion_handle_validate(client, handle)) {
1710 WARN(1, "%s: invalid handle passed to secure.\n", __func__);
1711 goto out_unlock;
1712 }
1713
1714 buffer = handle->buffer;
1715 heap = buffer->heap;
1716
1717 if (heap->type != (enum ion_heap_type) ION_HEAP_TYPE_CP) {
1718 pr_err("%s: cannot secure buffer from non secure heap\n",
1719 __func__);
1720 goto out_unlock;
1721 }
1722
1723 BUG_ON(!buffer->heap->ops->unsecure_buffer);
1724 /*
1725 * Protect the handle via the client lock to ensure we aren't
1726 * racing with free
1727 */
1728 ret = buffer->heap->ops->unsecure_buffer(buffer, 0);
1729
1730out_unlock:
1731 mutex_unlock(&client->lock);
1732 return ret;
1733}
1734
Laura Abbott7e446482012-06-13 15:59:39 -07001735int ion_secure_heap(struct ion_device *dev, int heap_id, int version,
1736 void *data)
Olav Haugan0a852512012-01-09 10:20:55 -08001737{
1738 struct rb_node *n;
1739 int ret_val = 0;
1740
1741 /*
1742 * traverse the list of heaps available in this system
1743 * and find the heap that is specified.
1744 */
1745 mutex_lock(&dev->lock);
1746 for (n = rb_first(&dev->heaps); n != NULL; n = rb_next(n)) {
1747 struct ion_heap *heap = rb_entry(n, struct ion_heap, node);
Mitchel Humpherys362b52b2012-09-13 10:53:22 -07001748 if (heap->type != (enum ion_heap_type) ION_HEAP_TYPE_CP)
Olav Haugan0a852512012-01-09 10:20:55 -08001749 continue;
1750 if (ION_HEAP(heap->id) != heap_id)
1751 continue;
1752 if (heap->ops->secure_heap)
Laura Abbott7e446482012-06-13 15:59:39 -07001753 ret_val = heap->ops->secure_heap(heap, version, data);
Olav Haugan0a852512012-01-09 10:20:55 -08001754 else
1755 ret_val = -EINVAL;
1756 break;
1757 }
1758 mutex_unlock(&dev->lock);
1759 return ret_val;
1760}
Olav Hauganbd453a92012-07-05 14:21:34 -07001761EXPORT_SYMBOL(ion_secure_heap);
Olav Haugan0a852512012-01-09 10:20:55 -08001762
Laura Abbott7e446482012-06-13 15:59:39 -07001763int ion_unsecure_heap(struct ion_device *dev, int heap_id, int version,
1764 void *data)
Olav Haugan0a852512012-01-09 10:20:55 -08001765{
1766 struct rb_node *n;
1767 int ret_val = 0;
1768
1769 /*
1770 * traverse the list of heaps available in this system
1771 * and find the heap that is specified.
1772 */
1773 mutex_lock(&dev->lock);
1774 for (n = rb_first(&dev->heaps); n != NULL; n = rb_next(n)) {
1775 struct ion_heap *heap = rb_entry(n, struct ion_heap, node);
Mitchel Humpherys362b52b2012-09-13 10:53:22 -07001776 if (heap->type != (enum ion_heap_type) ION_HEAP_TYPE_CP)
Olav Haugan0a852512012-01-09 10:20:55 -08001777 continue;
1778 if (ION_HEAP(heap->id) != heap_id)
1779 continue;
1780 if (heap->ops->secure_heap)
Laura Abbott7e446482012-06-13 15:59:39 -07001781 ret_val = heap->ops->unsecure_heap(heap, version, data);
Olav Haugan0a852512012-01-09 10:20:55 -08001782 else
1783 ret_val = -EINVAL;
1784 break;
1785 }
1786 mutex_unlock(&dev->lock);
1787 return ret_val;
1788}
Olav Hauganbd453a92012-07-05 14:21:34 -07001789EXPORT_SYMBOL(ion_unsecure_heap);
Olav Haugan0a852512012-01-09 10:20:55 -08001790
Laura Abbott404f8242011-10-31 14:22:53 -07001791static int ion_debug_leak_show(struct seq_file *s, void *unused)
1792{
1793 struct ion_device *dev = s->private;
1794 struct rb_node *n;
1795 struct rb_node *n2;
1796
1797 /* mark all buffers as 1 */
1798 seq_printf(s, "%16.s %16.s %16.s %16.s\n", "buffer", "heap", "size",
1799 "ref cnt");
1800 mutex_lock(&dev->lock);
1801 for (n = rb_first(&dev->buffers); n; n = rb_next(n)) {
1802 struct ion_buffer *buf = rb_entry(n, struct ion_buffer,
1803 node);
1804
1805 buf->marked = 1;
1806 }
1807
1808 /* now see which buffers we can access */
Laura Abbottb14ed962012-01-30 14:18:08 -08001809 for (n = rb_first(&dev->clients); n; n = rb_next(n)) {
Laura Abbott404f8242011-10-31 14:22:53 -07001810 struct ion_client *client = rb_entry(n, struct ion_client,
1811 node);
1812
1813 mutex_lock(&client->lock);
1814 for (n2 = rb_first(&client->handles); n2; n2 = rb_next(n2)) {
1815 struct ion_handle *handle = rb_entry(n2,
1816 struct ion_handle, node);
1817
1818 handle->buffer->marked = 0;
1819
1820 }
1821 mutex_unlock(&client->lock);
1822
1823 }
1824
Laura Abbott404f8242011-10-31 14:22:53 -07001825 /* And anyone still marked as a 1 means a leaked handle somewhere */
1826 for (n = rb_first(&dev->buffers); n; n = rb_next(n)) {
1827 struct ion_buffer *buf = rb_entry(n, struct ion_buffer,
1828 node);
1829
1830 if (buf->marked == 1)
1831 seq_printf(s, "%16.x %16.s %16.x %16.d\n",
1832 (int)buf, buf->heap->name, buf->size,
1833 atomic_read(&buf->ref.refcount));
1834 }
1835 mutex_unlock(&dev->lock);
1836 return 0;
1837}
1838
1839static int ion_debug_leak_open(struct inode *inode, struct file *file)
1840{
1841 return single_open(file, ion_debug_leak_show, inode->i_private);
1842}
1843
1844static const struct file_operations debug_leak_fops = {
1845 .open = ion_debug_leak_open,
1846 .read = seq_read,
1847 .llseek = seq_lseek,
1848 .release = single_release,
1849};
1850
1851
1852
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001853struct ion_device *ion_device_create(long (*custom_ioctl)
1854 (struct ion_client *client,
1855 unsigned int cmd,
1856 unsigned long arg))
1857{
1858 struct ion_device *idev;
1859 int ret;
1860
1861 idev = kzalloc(sizeof(struct ion_device), GFP_KERNEL);
1862 if (!idev)
1863 return ERR_PTR(-ENOMEM);
1864
1865 idev->dev.minor = MISC_DYNAMIC_MINOR;
1866 idev->dev.name = "ion";
1867 idev->dev.fops = &ion_fops;
1868 idev->dev.parent = NULL;
1869 ret = misc_register(&idev->dev);
1870 if (ret) {
1871 pr_err("ion: failed to register misc device.\n");
1872 return ERR_PTR(ret);
1873 }
1874
1875 idev->debug_root = debugfs_create_dir("ion", NULL);
1876 if (IS_ERR_OR_NULL(idev->debug_root))
1877 pr_err("ion: failed to create debug files.\n");
1878
1879 idev->custom_ioctl = custom_ioctl;
1880 idev->buffers = RB_ROOT;
1881 mutex_init(&idev->lock);
1882 idev->heaps = RB_ROOT;
Laura Abbottb14ed962012-01-30 14:18:08 -08001883 idev->clients = RB_ROOT;
Laura Abbott404f8242011-10-31 14:22:53 -07001884 debugfs_create_file("check_leaked_fds", 0664, idev->debug_root, idev,
1885 &debug_leak_fops);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001886 return idev;
1887}
1888
1889void ion_device_destroy(struct ion_device *dev)
1890{
1891 misc_deregister(&dev->dev);
1892 /* XXX need to free the heaps and clients ? */
1893 kfree(dev);
1894}
Laura Abbottb14ed962012-01-30 14:18:08 -08001895
1896void __init ion_reserve(struct ion_platform_data *data)
1897{
1898 int i, ret;
1899
1900 for (i = 0; i < data->nr; i++) {
1901 if (data->heaps[i].size == 0)
1902 continue;
1903 ret = memblock_reserve(data->heaps[i].base,
1904 data->heaps[i].size);
1905 if (ret)
1906 pr_err("memblock reserve of %x@%lx failed\n",
1907 data->heaps[i].size,
1908 data->heaps[i].base);
1909 }
1910}