blob: b3df752eb8dd1263b40a0c20802d31c6073a5491 [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>
Mitchel Humpherysd88b8eb2012-09-04 17:00:29 -070036#include <linux/msm_ion.h>
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070037
Laura Abbott8c017362011-09-22 20:59:12 -070038#include <mach/iommu_domains.h>
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070039#include "ion_priv.h"
40#define DEBUG
41
42/**
43 * struct ion_device - the metadata of the ion device node
44 * @dev: the actual misc device
45 * @buffers: an rb tree of all the existing buffers
46 * @lock: lock protecting the buffers & heaps trees
47 * @heaps: list of all the heaps in the system
48 * @user_clients: list of all the clients created from userspace
49 */
50struct ion_device {
51 struct miscdevice dev;
52 struct rb_root buffers;
53 struct mutex lock;
54 struct rb_root heaps;
55 long (*custom_ioctl) (struct ion_client *client, unsigned int cmd,
56 unsigned long arg);
Laura Abbottb14ed962012-01-30 14:18:08 -080057 struct rb_root clients;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070058 struct dentry *debug_root;
59};
60
61/**
62 * struct ion_client - a process/hw block local address space
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070063 * @node: node in the tree of all clients
64 * @dev: backpointer to ion device
65 * @handles: an rb tree of all the handles in this client
66 * @lock: lock protecting the tree of handles
67 * @heap_mask: mask of all supported heaps
68 * @name: used for debugging
69 * @task: used for debugging
70 *
71 * A client represents a list of buffers this client may access.
72 * The mutex stored here is used to protect both handles tree
73 * as well as the handles themselves, and should be held while modifying either.
74 */
75struct ion_client {
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070076 struct rb_node node;
77 struct ion_device *dev;
78 struct rb_root handles;
79 struct mutex lock;
80 unsigned int heap_mask;
Olav Haugan63e5f3b2012-01-11 16:42:37 -080081 char *name;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070082 struct task_struct *task;
83 pid_t pid;
84 struct dentry *debug_root;
85};
86
87/**
88 * ion_handle - a client local reference to a buffer
89 * @ref: reference count
90 * @client: back pointer to the client the buffer resides in
91 * @buffer: pointer to the buffer
92 * @node: node in the client's handle rbtree
93 * @kmap_cnt: count of times this client has mapped to kernel
94 * @dmap_cnt: count of times this client has mapped for dma
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070095 *
96 * Modifications to node, map_cnt or mapping should be protected by the
97 * lock in the client. Other fields are never changed after initialization.
98 */
99struct ion_handle {
100 struct kref ref;
101 struct ion_client *client;
102 struct ion_buffer *buffer;
103 struct rb_node node;
104 unsigned int kmap_cnt;
Laura Abbott8c017362011-09-22 20:59:12 -0700105 unsigned int iommu_map_cnt;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700106};
107
Olav Hauganb3676592012-03-02 15:02:25 -0800108static void ion_iommu_release(struct kref *kref);
109
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700110/* this function should only be called while dev->lock is held */
111static void ion_buffer_add(struct ion_device *dev,
112 struct ion_buffer *buffer)
113{
114 struct rb_node **p = &dev->buffers.rb_node;
115 struct rb_node *parent = NULL;
116 struct ion_buffer *entry;
117
118 while (*p) {
119 parent = *p;
120 entry = rb_entry(parent, struct ion_buffer, node);
121
122 if (buffer < entry) {
123 p = &(*p)->rb_left;
124 } else if (buffer > entry) {
125 p = &(*p)->rb_right;
126 } else {
127 pr_err("%s: buffer already found.", __func__);
128 BUG();
129 }
130 }
131
132 rb_link_node(&buffer->node, parent, p);
133 rb_insert_color(&buffer->node, &dev->buffers);
134}
135
Olav Haugan0fa9b602012-01-25 11:50:38 -0800136static void ion_iommu_add(struct ion_buffer *buffer,
Laura Abbott8c017362011-09-22 20:59:12 -0700137 struct ion_iommu_map *iommu)
138{
139 struct rb_node **p = &buffer->iommu_maps.rb_node;
140 struct rb_node *parent = NULL;
141 struct ion_iommu_map *entry;
142
143 while (*p) {
144 parent = *p;
145 entry = rb_entry(parent, struct ion_iommu_map, node);
146
147 if (iommu->key < entry->key) {
148 p = &(*p)->rb_left;
149 } else if (iommu->key > entry->key) {
150 p = &(*p)->rb_right;
151 } else {
152 pr_err("%s: buffer %p already has mapping for domain %d"
153 " and partition %d\n", __func__,
154 buffer,
155 iommu_map_domain(iommu),
156 iommu_map_partition(iommu));
157 BUG();
158 }
159 }
160
161 rb_link_node(&iommu->node, parent, p);
162 rb_insert_color(&iommu->node, &buffer->iommu_maps);
163
164}
165
166static struct ion_iommu_map *ion_iommu_lookup(struct ion_buffer *buffer,
167 unsigned int domain_no,
168 unsigned int partition_no)
169{
170 struct rb_node **p = &buffer->iommu_maps.rb_node;
171 struct rb_node *parent = NULL;
172 struct ion_iommu_map *entry;
173 uint64_t key = domain_no;
174 key = key << 32 | partition_no;
175
176 while (*p) {
177 parent = *p;
178 entry = rb_entry(parent, struct ion_iommu_map, node);
179
180 if (key < entry->key)
181 p = &(*p)->rb_left;
182 else if (key > entry->key)
183 p = &(*p)->rb_right;
184 else
185 return entry;
186 }
187
188 return NULL;
189}
190
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700191/* this function should only be called while dev->lock is held */
192static struct ion_buffer *ion_buffer_create(struct ion_heap *heap,
193 struct ion_device *dev,
194 unsigned long len,
195 unsigned long align,
196 unsigned long flags)
197{
198 struct ion_buffer *buffer;
Laura Abbottb14ed962012-01-30 14:18:08 -0800199 struct sg_table *table;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700200 int ret;
201
202 buffer = kzalloc(sizeof(struct ion_buffer), GFP_KERNEL);
203 if (!buffer)
204 return ERR_PTR(-ENOMEM);
205
206 buffer->heap = heap;
207 kref_init(&buffer->ref);
208
209 ret = heap->ops->allocate(heap, buffer, len, align, flags);
210 if (ret) {
211 kfree(buffer);
212 return ERR_PTR(ret);
213 }
Laura Abbottb14ed962012-01-30 14:18:08 -0800214
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700215 buffer->dev = dev;
216 buffer->size = len;
Hanumant Singh7d72bad2012-08-29 18:39:44 -0700217 buffer->flags = flags;
Laura Abbottb14ed962012-01-30 14:18:08 -0800218
219 table = buffer->heap->ops->map_dma(buffer->heap, buffer);
220 if (IS_ERR_OR_NULL(table)) {
221 heap->ops->free(buffer);
222 kfree(buffer);
223 return ERR_PTR(PTR_ERR(table));
224 }
225 buffer->sg_table = table;
226
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700227 mutex_init(&buffer->lock);
228 ion_buffer_add(dev, buffer);
229 return buffer;
230}
231
Olav Hauganb3676592012-03-02 15:02:25 -0800232/**
233 * Check for delayed IOMMU unmapping. Also unmap any outstanding
234 * mappings which would otherwise have been leaked.
235 */
236static void ion_iommu_delayed_unmap(struct ion_buffer *buffer)
237{
238 struct ion_iommu_map *iommu_map;
239 struct rb_node *node;
240 const struct rb_root *rb = &(buffer->iommu_maps);
241 unsigned long ref_count;
242 unsigned int delayed_unmap;
243
244 mutex_lock(&buffer->lock);
245
246 while ((node = rb_first(rb)) != 0) {
247 iommu_map = rb_entry(node, struct ion_iommu_map, node);
248 ref_count = atomic_read(&iommu_map->ref.refcount);
249 delayed_unmap = iommu_map->flags & ION_IOMMU_UNMAP_DELAYED;
250
251 if ((delayed_unmap && ref_count > 1) || !delayed_unmap) {
252 pr_err("%s: Virtual memory address leak in domain %u, partition %u\n",
253 __func__, iommu_map->domain_info[DI_DOMAIN_NUM],
254 iommu_map->domain_info[DI_PARTITION_NUM]);
255 }
256 /* set ref count to 1 to force release */
257 kref_init(&iommu_map->ref);
258 kref_put(&iommu_map->ref, ion_iommu_release);
259 }
260
261 mutex_unlock(&buffer->lock);
262}
263
Laura Abbott93619302012-10-11 11:51:40 -0700264static void ion_delayed_unsecure(struct ion_buffer *buffer)
265{
266 if (buffer->heap->ops->unsecure_buffer)
267 buffer->heap->ops->unsecure_buffer(buffer, 1);
268}
269
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700270static void ion_buffer_destroy(struct kref *kref)
271{
272 struct ion_buffer *buffer = container_of(kref, struct ion_buffer, ref);
273 struct ion_device *dev = buffer->dev;
274
Laura Abbottb14ed962012-01-30 14:18:08 -0800275 if (WARN_ON(buffer->kmap_cnt > 0))
276 buffer->heap->ops->unmap_kernel(buffer->heap, buffer);
277
278 buffer->heap->ops->unmap_dma(buffer->heap, buffer);
279
Laura Abbott93619302012-10-11 11:51:40 -0700280 ion_delayed_unsecure(buffer);
Olav Hauganb3676592012-03-02 15:02:25 -0800281 ion_iommu_delayed_unmap(buffer);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700282 buffer->heap->ops->free(buffer);
283 mutex_lock(&dev->lock);
284 rb_erase(&buffer->node, &dev->buffers);
285 mutex_unlock(&dev->lock);
286 kfree(buffer);
287}
288
289static void ion_buffer_get(struct ion_buffer *buffer)
290{
291 kref_get(&buffer->ref);
292}
293
294static int ion_buffer_put(struct ion_buffer *buffer)
295{
296 return kref_put(&buffer->ref, ion_buffer_destroy);
297}
298
299static struct ion_handle *ion_handle_create(struct ion_client *client,
300 struct ion_buffer *buffer)
301{
302 struct ion_handle *handle;
303
304 handle = kzalloc(sizeof(struct ion_handle), GFP_KERNEL);
305 if (!handle)
306 return ERR_PTR(-ENOMEM);
307 kref_init(&handle->ref);
308 rb_init_node(&handle->node);
309 handle->client = client;
310 ion_buffer_get(buffer);
311 handle->buffer = buffer;
312
313 return handle;
314}
315
Laura Abbottb14ed962012-01-30 14:18:08 -0800316static void ion_handle_kmap_put(struct ion_handle *);
317
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700318static void ion_handle_destroy(struct kref *kref)
319{
320 struct ion_handle *handle = container_of(kref, struct ion_handle, ref);
Laura Abbottb14ed962012-01-30 14:18:08 -0800321 struct ion_client *client = handle->client;
322 struct ion_buffer *buffer = handle->buffer;
323
Laura Abbottb14ed962012-01-30 14:18:08 -0800324 mutex_lock(&buffer->lock);
325 while (handle->kmap_cnt)
326 ion_handle_kmap_put(handle);
327 mutex_unlock(&buffer->lock);
328
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700329 if (!RB_EMPTY_NODE(&handle->node))
Laura Abbottb14ed962012-01-30 14:18:08 -0800330 rb_erase(&handle->node, &client->handles);
Laura Abbottb14ed962012-01-30 14:18:08 -0800331
332 ion_buffer_put(buffer);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700333 kfree(handle);
334}
335
336struct ion_buffer *ion_handle_buffer(struct ion_handle *handle)
337{
338 return handle->buffer;
339}
340
341static void ion_handle_get(struct ion_handle *handle)
342{
343 kref_get(&handle->ref);
344}
345
346static int ion_handle_put(struct ion_handle *handle)
347{
348 return kref_put(&handle->ref, ion_handle_destroy);
349}
350
351static struct ion_handle *ion_handle_lookup(struct ion_client *client,
352 struct ion_buffer *buffer)
353{
354 struct rb_node *n;
355
356 for (n = rb_first(&client->handles); n; n = rb_next(n)) {
357 struct ion_handle *handle = rb_entry(n, struct ion_handle,
358 node);
359 if (handle->buffer == buffer)
360 return handle;
361 }
362 return NULL;
363}
364
365static bool ion_handle_validate(struct ion_client *client, struct ion_handle *handle)
366{
367 struct rb_node *n = client->handles.rb_node;
368
369 while (n) {
370 struct ion_handle *handle_node = rb_entry(n, struct ion_handle,
371 node);
372 if (handle < handle_node)
373 n = n->rb_left;
374 else if (handle > handle_node)
375 n = n->rb_right;
376 else
377 return true;
378 }
379 return false;
380}
381
382static void ion_handle_add(struct ion_client *client, struct ion_handle *handle)
383{
384 struct rb_node **p = &client->handles.rb_node;
385 struct rb_node *parent = NULL;
386 struct ion_handle *entry;
387
388 while (*p) {
389 parent = *p;
390 entry = rb_entry(parent, struct ion_handle, node);
391
392 if (handle < entry)
393 p = &(*p)->rb_left;
394 else if (handle > entry)
395 p = &(*p)->rb_right;
396 else
397 WARN(1, "%s: buffer already found.", __func__);
398 }
399
400 rb_link_node(&handle->node, parent, p);
401 rb_insert_color(&handle->node, &client->handles);
402}
403
404struct ion_handle *ion_alloc(struct ion_client *client, size_t len,
Hanumant Singh7d72bad2012-08-29 18:39:44 -0700405 size_t align, unsigned int heap_mask,
406 unsigned int flags)
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700407{
408 struct rb_node *n;
409 struct ion_handle *handle;
410 struct ion_device *dev = client->dev;
411 struct ion_buffer *buffer = NULL;
Olav Haugan0a852512012-01-09 10:20:55 -0800412 unsigned long secure_allocation = flags & ION_SECURE;
Olav Haugan35e2f2f2012-01-11 17:31:47 -0800413 const unsigned int MAX_DBG_STR_LEN = 64;
414 char dbg_str[MAX_DBG_STR_LEN];
415 unsigned int dbg_str_idx = 0;
416
417 dbg_str[0] = '\0';
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700418
419 /*
420 * traverse the list of heaps available in this system in priority
421 * order. If the heap type is supported by the client, and matches the
422 * request of the caller allocate from it. Repeat until allocate has
423 * succeeded or all heaps have been tried
424 */
Laura Abbottb14ed962012-01-30 14:18:08 -0800425 if (WARN_ON(!len))
426 return ERR_PTR(-EINVAL);
427
428 len = PAGE_ALIGN(len);
429
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700430 mutex_lock(&dev->lock);
431 for (n = rb_first(&dev->heaps); n != NULL; n = rb_next(n)) {
432 struct ion_heap *heap = rb_entry(n, struct ion_heap, node);
433 /* if the client doesn't support this heap type */
434 if (!((1 << heap->type) & client->heap_mask))
435 continue;
436 /* if the caller didn't specify this heap type */
Hanumant Singh7d72bad2012-08-29 18:39:44 -0700437 if (!((1 << heap->id) & heap_mask))
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700438 continue;
Olav Haugan0a852512012-01-09 10:20:55 -0800439 /* Do not allow un-secure heap if secure is specified */
Mitchel Humpherys362b52b2012-09-13 10:53:22 -0700440 if (secure_allocation &&
441 (heap->type != (enum ion_heap_type) ION_HEAP_TYPE_CP))
Olav Haugan0a852512012-01-09 10:20:55 -0800442 continue;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700443 buffer = ion_buffer_create(heap, dev, len, align, flags);
444 if (!IS_ERR_OR_NULL(buffer))
445 break;
Olav Haugan35e2f2f2012-01-11 17:31:47 -0800446 if (dbg_str_idx < MAX_DBG_STR_LEN) {
447 unsigned int len_left = MAX_DBG_STR_LEN-dbg_str_idx-1;
448 int ret_value = snprintf(&dbg_str[dbg_str_idx],
449 len_left, "%s ", heap->name);
450 if (ret_value >= len_left) {
451 /* overflow */
452 dbg_str[MAX_DBG_STR_LEN-1] = '\0';
453 dbg_str_idx = MAX_DBG_STR_LEN;
454 } else if (ret_value >= 0) {
455 dbg_str_idx += ret_value;
456 } else {
457 /* error */
458 dbg_str[MAX_DBG_STR_LEN-1] = '\0';
459 }
460 }
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700461 }
462 mutex_unlock(&dev->lock);
463
Laura Abbottb14ed962012-01-30 14:18:08 -0800464 if (buffer == NULL)
465 return ERR_PTR(-ENODEV);
466
467 if (IS_ERR(buffer)) {
Olav Haugan35e2f2f2012-01-11 17:31:47 -0800468 pr_debug("ION is unable to allocate 0x%x bytes (alignment: "
469 "0x%x) from heap(s) %sfor client %s with heap "
470 "mask 0x%x\n",
471 len, align, dbg_str, client->name, client->heap_mask);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700472 return ERR_PTR(PTR_ERR(buffer));
Olav Haugan35e2f2f2012-01-11 17:31:47 -0800473 }
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700474
475 handle = ion_handle_create(client, buffer);
476
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700477 /*
478 * ion_buffer_create will create a buffer with a ref_cnt of 1,
479 * and ion_handle_create will take a second reference, drop one here
480 */
481 ion_buffer_put(buffer);
482
Laura Abbottb14ed962012-01-30 14:18:08 -0800483 if (!IS_ERR(handle)) {
484 mutex_lock(&client->lock);
485 ion_handle_add(client, handle);
486 mutex_unlock(&client->lock);
487 }
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700488
Laura Abbottb14ed962012-01-30 14:18:08 -0800489
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700490 return handle;
491}
Olav Hauganbd2b6922012-01-25 09:28:55 -0800492EXPORT_SYMBOL(ion_alloc);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700493
494void ion_free(struct ion_client *client, struct ion_handle *handle)
495{
496 bool valid_handle;
497
498 BUG_ON(client != handle->client);
499
500 mutex_lock(&client->lock);
501 valid_handle = ion_handle_validate(client, handle);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700502 if (!valid_handle) {
Laura Abbottec149ff2012-01-26 13:33:11 -0800503 mutex_unlock(&client->lock);
Olav Haugan6ede5672012-04-19 10:20:22 -0700504 WARN(1, "%s: invalid handle passed to free.\n", __func__);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700505 return;
506 }
Laura Abbottb14ed962012-01-30 14:18:08 -0800507 ion_handle_put(handle);
Rebecca Schultz Zavinaad11cb2012-08-20 15:41:11 -0700508 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700509}
Olav Hauganbd2b6922012-01-25 09:28:55 -0800510EXPORT_SYMBOL(ion_free);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700511
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700512int ion_phys(struct ion_client *client, struct ion_handle *handle,
513 ion_phys_addr_t *addr, size_t *len)
514{
515 struct ion_buffer *buffer;
516 int ret;
517
518 mutex_lock(&client->lock);
519 if (!ion_handle_validate(client, handle)) {
520 mutex_unlock(&client->lock);
521 return -EINVAL;
522 }
523
524 buffer = handle->buffer;
525
526 if (!buffer->heap->ops->phys) {
527 pr_err("%s: ion_phys is not implemented by this heap.\n",
528 __func__);
529 mutex_unlock(&client->lock);
530 return -ENODEV;
531 }
532 mutex_unlock(&client->lock);
533 ret = buffer->heap->ops->phys(buffer->heap, buffer, addr, len);
534 return ret;
535}
Olav Hauganbd2b6922012-01-25 09:28:55 -0800536EXPORT_SYMBOL(ion_phys);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700537
Laura Abbottb14ed962012-01-30 14:18:08 -0800538static void *ion_buffer_kmap_get(struct ion_buffer *buffer)
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700539{
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700540 void *vaddr;
541
Laura Abbottb14ed962012-01-30 14:18:08 -0800542 if (buffer->kmap_cnt) {
543 buffer->kmap_cnt++;
544 return buffer->vaddr;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700545 }
Laura Abbottb14ed962012-01-30 14:18:08 -0800546 vaddr = buffer->heap->ops->map_kernel(buffer->heap, buffer);
547 if (IS_ERR_OR_NULL(vaddr))
548 return vaddr;
549 buffer->vaddr = vaddr;
550 buffer->kmap_cnt++;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700551 return vaddr;
552}
Laura Abbottb14ed962012-01-30 14:18:08 -0800553
554static void *ion_handle_kmap_get(struct ion_handle *handle)
555{
556 struct ion_buffer *buffer = handle->buffer;
557 void *vaddr;
558
559 if (handle->kmap_cnt) {
560 handle->kmap_cnt++;
561 return buffer->vaddr;
562 }
563 vaddr = ion_buffer_kmap_get(buffer);
564 if (IS_ERR_OR_NULL(vaddr))
565 return vaddr;
566 handle->kmap_cnt++;
567 return vaddr;
568}
569
570static void ion_buffer_kmap_put(struct ion_buffer *buffer)
571{
572 buffer->kmap_cnt--;
573 if (!buffer->kmap_cnt) {
574 buffer->heap->ops->unmap_kernel(buffer->heap, buffer);
575 buffer->vaddr = NULL;
576 }
577}
578
579static void ion_handle_kmap_put(struct ion_handle *handle)
580{
581 struct ion_buffer *buffer = handle->buffer;
582
583 handle->kmap_cnt--;
584 if (!handle->kmap_cnt)
585 ion_buffer_kmap_put(buffer);
586}
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700587
Olav Hauganb3676592012-03-02 15:02:25 -0800588static struct ion_iommu_map *__ion_iommu_map(struct ion_buffer *buffer,
Laura Abbott8c017362011-09-22 20:59:12 -0700589 int domain_num, int partition_num, unsigned long align,
590 unsigned long iova_length, unsigned long flags,
591 unsigned long *iova)
592{
593 struct ion_iommu_map *data;
594 int ret;
595
596 data = kmalloc(sizeof(*data), GFP_ATOMIC);
597
598 if (!data)
Olav Hauganb3676592012-03-02 15:02:25 -0800599 return ERR_PTR(-ENOMEM);
Laura Abbott8c017362011-09-22 20:59:12 -0700600
601 data->buffer = buffer;
602 iommu_map_domain(data) = domain_num;
603 iommu_map_partition(data) = partition_num;
604
605 ret = buffer->heap->ops->map_iommu(buffer, data,
606 domain_num,
607 partition_num,
608 align,
609 iova_length,
610 flags);
611
612 if (ret)
613 goto out;
614
615 kref_init(&data->ref);
616 *iova = data->iova_addr;
617
618 ion_iommu_add(buffer, data);
619
Olav Hauganb3676592012-03-02 15:02:25 -0800620 return data;
Laura Abbott8c017362011-09-22 20:59:12 -0700621
622out:
Laura Abbott8c017362011-09-22 20:59:12 -0700623 kfree(data);
Olav Hauganb3676592012-03-02 15:02:25 -0800624 return ERR_PTR(ret);
Laura Abbott8c017362011-09-22 20:59:12 -0700625}
626
627int ion_map_iommu(struct ion_client *client, struct ion_handle *handle,
628 int domain_num, int partition_num, unsigned long align,
629 unsigned long iova_length, unsigned long *iova,
630 unsigned long *buffer_size,
Olav Hauganb3676592012-03-02 15:02:25 -0800631 unsigned long flags, unsigned long iommu_flags)
Laura Abbott8c017362011-09-22 20:59:12 -0700632{
633 struct ion_buffer *buffer;
634 struct ion_iommu_map *iommu_map;
635 int ret = 0;
636
Olav Haugan79e9ffa2012-02-24 13:11:10 -0800637 if (ION_IS_CACHED(flags)) {
638 pr_err("%s: Cannot map iommu as cached.\n", __func__);
639 return -EINVAL;
640 }
641
Laura Abbott8c017362011-09-22 20:59:12 -0700642 mutex_lock(&client->lock);
643 if (!ion_handle_validate(client, handle)) {
644 pr_err("%s: invalid handle passed to map_kernel.\n",
645 __func__);
646 mutex_unlock(&client->lock);
647 return -EINVAL;
648 }
649
650 buffer = handle->buffer;
651 mutex_lock(&buffer->lock);
652
653 if (!handle->buffer->heap->ops->map_iommu) {
654 pr_err("%s: map_iommu is not implemented by this heap.\n",
655 __func__);
656 ret = -ENODEV;
657 goto out;
658 }
659
Laura Abbott8c017362011-09-22 20:59:12 -0700660 /*
661 * If clients don't want a custom iova length, just use whatever
662 * the buffer size is
663 */
664 if (!iova_length)
665 iova_length = buffer->size;
666
667 if (buffer->size > iova_length) {
668 pr_debug("%s: iova length %lx is not at least buffer size"
669 " %x\n", __func__, iova_length, buffer->size);
670 ret = -EINVAL;
671 goto out;
672 }
673
674 if (buffer->size & ~PAGE_MASK) {
675 pr_debug("%s: buffer size %x is not aligned to %lx", __func__,
676 buffer->size, PAGE_SIZE);
677 ret = -EINVAL;
678 goto out;
679 }
680
681 if (iova_length & ~PAGE_MASK) {
682 pr_debug("%s: iova_length %lx is not aligned to %lx", __func__,
683 iova_length, PAGE_SIZE);
684 ret = -EINVAL;
685 goto out;
686 }
687
688 iommu_map = ion_iommu_lookup(buffer, domain_num, partition_num);
Olav Hauganb3676592012-03-02 15:02:25 -0800689 if (!iommu_map) {
690 iommu_map = __ion_iommu_map(buffer, domain_num, partition_num,
691 align, iova_length, flags, iova);
Laura Abbottb14ed962012-01-30 14:18:08 -0800692 if (!IS_ERR_OR_NULL(iommu_map)) {
Olav Hauganb3676592012-03-02 15:02:25 -0800693 iommu_map->flags = iommu_flags;
694
695 if (iommu_map->flags & ION_IOMMU_UNMAP_DELAYED)
696 kref_get(&iommu_map->ref);
Laura Abbott11bca602012-09-14 12:48:18 -0700697 } else {
698 ret = PTR_ERR(iommu_map);
Olav Hauganb3676592012-03-02 15:02:25 -0800699 }
Laura Abbott8c017362011-09-22 20:59:12 -0700700 } else {
Olav Hauganb3676592012-03-02 15:02:25 -0800701 if (iommu_map->flags != iommu_flags) {
702 pr_err("%s: handle %p is already mapped with iommu flags %lx, trying to map with flags %lx\n",
703 __func__, handle,
704 iommu_map->flags, iommu_flags);
Olav Hauganb3676592012-03-02 15:02:25 -0800705 ret = -EINVAL;
706 } else if (iommu_map->mapped_size != iova_length) {
Laura Abbott8c017362011-09-22 20:59:12 -0700707 pr_err("%s: handle %p is already mapped with length"
Olav Hauganb3676592012-03-02 15:02:25 -0800708 " %x, trying to map with length %lx\n",
Laura Abbott8c017362011-09-22 20:59:12 -0700709 __func__, handle, iommu_map->mapped_size,
710 iova_length);
Laura Abbott8c017362011-09-22 20:59:12 -0700711 ret = -EINVAL;
712 } else {
713 kref_get(&iommu_map->ref);
714 *iova = iommu_map->iova_addr;
715 }
716 }
Laura Abbottb14ed962012-01-30 14:18:08 -0800717 if (!ret)
718 buffer->iommu_map_cnt++;
Laura Abbott8c017362011-09-22 20:59:12 -0700719 *buffer_size = buffer->size;
720out:
721 mutex_unlock(&buffer->lock);
722 mutex_unlock(&client->lock);
723 return ret;
724}
725EXPORT_SYMBOL(ion_map_iommu);
726
727static void ion_iommu_release(struct kref *kref)
728{
729 struct ion_iommu_map *map = container_of(kref, struct ion_iommu_map,
730 ref);
731 struct ion_buffer *buffer = map->buffer;
732
733 rb_erase(&map->node, &buffer->iommu_maps);
734 buffer->heap->ops->unmap_iommu(map);
735 kfree(map);
736}
737
738void ion_unmap_iommu(struct ion_client *client, struct ion_handle *handle,
739 int domain_num, int partition_num)
740{
741 struct ion_iommu_map *iommu_map;
742 struct ion_buffer *buffer;
743
744 mutex_lock(&client->lock);
745 buffer = handle->buffer;
746
747 mutex_lock(&buffer->lock);
748
749 iommu_map = ion_iommu_lookup(buffer, domain_num, partition_num);
750
751 if (!iommu_map) {
752 WARN(1, "%s: (%d,%d) was never mapped for %p\n", __func__,
753 domain_num, partition_num, buffer);
754 goto out;
755 }
756
Laura Abbott8c017362011-09-22 20:59:12 -0700757 kref_put(&iommu_map->ref, ion_iommu_release);
758
Laura Abbottb14ed962012-01-30 14:18:08 -0800759 buffer->iommu_map_cnt--;
Laura Abbott8c017362011-09-22 20:59:12 -0700760out:
761 mutex_unlock(&buffer->lock);
762
763 mutex_unlock(&client->lock);
764
765}
766EXPORT_SYMBOL(ion_unmap_iommu);
767
Mitchel Humpherys911b4b72012-09-12 14:42:50 -0700768void *ion_map_kernel(struct ion_client *client, struct ion_handle *handle)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700769{
770 struct ion_buffer *buffer;
Laura Abbottb14ed962012-01-30 14:18:08 -0800771 void *vaddr;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700772
773 mutex_lock(&client->lock);
774 if (!ion_handle_validate(client, handle)) {
Laura Abbottb14ed962012-01-30 14:18:08 -0800775 pr_err("%s: invalid handle passed to map_kernel.\n",
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700776 __func__);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700777 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700778 return ERR_PTR(-EINVAL);
779 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700780
Laura Abbottb14ed962012-01-30 14:18:08 -0800781 buffer = handle->buffer;
782
783 if (!handle->buffer->heap->ops->map_kernel) {
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700784 pr_err("%s: map_kernel is not implemented by this heap.\n",
785 __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(-ENODEV);
788 }
Laura Abbott894fd582011-08-19 13:33:56 -0700789
Laura Abbottb14ed962012-01-30 14:18:08 -0800790 mutex_lock(&buffer->lock);
791 vaddr = ion_handle_kmap_get(handle);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700792 mutex_unlock(&buffer->lock);
793 mutex_unlock(&client->lock);
Laura Abbottb14ed962012-01-30 14:18:08 -0800794 return vaddr;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700795}
Olav Hauganbd453a92012-07-05 14:21:34 -0700796EXPORT_SYMBOL(ion_map_kernel);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700797
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700798void ion_unmap_kernel(struct ion_client *client, struct ion_handle *handle)
799{
800 struct ion_buffer *buffer;
801
802 mutex_lock(&client->lock);
803 buffer = handle->buffer;
804 mutex_lock(&buffer->lock);
Laura Abbottb14ed962012-01-30 14:18:08 -0800805 ion_handle_kmap_put(handle);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700806 mutex_unlock(&buffer->lock);
807 mutex_unlock(&client->lock);
808}
Olav Hauganbd453a92012-07-05 14:21:34 -0700809EXPORT_SYMBOL(ion_unmap_kernel);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700810
Olav Haugan41f85792012-02-08 15:28:05 -0800811int ion_do_cache_op(struct ion_client *client, struct ion_handle *handle,
Laura Abbottabcb6f72011-10-04 16:26:49 -0700812 void *uaddr, unsigned long offset, unsigned long len,
813 unsigned int cmd)
814{
815 struct ion_buffer *buffer;
Laura Abbottabcb6f72011-10-04 16:26:49 -0700816 int ret = -EINVAL;
817
818 mutex_lock(&client->lock);
819 if (!ion_handle_validate(client, handle)) {
820 pr_err("%s: invalid handle passed to do_cache_op.\n",
821 __func__);
822 mutex_unlock(&client->lock);
823 return -EINVAL;
824 }
825 buffer = handle->buffer;
826 mutex_lock(&buffer->lock);
827
Laura Abbottcbaa6682011-10-19 12:14:14 -0700828 if (!ION_IS_CACHED(buffer->flags)) {
Laura Abbottabcb6f72011-10-04 16:26:49 -0700829 ret = 0;
830 goto out;
831 }
832
833 if (!handle->buffer->heap->ops->cache_op) {
834 pr_err("%s: cache_op is not implemented by this heap.\n",
835 __func__);
836 ret = -ENODEV;
837 goto out;
838 }
839
Laura Abbottabcb6f72011-10-04 16:26:49 -0700840
841 ret = buffer->heap->ops->cache_op(buffer->heap, buffer, uaddr,
842 offset, len, cmd);
843
844out:
845 mutex_unlock(&buffer->lock);
846 mutex_unlock(&client->lock);
847 return ret;
848
849}
Olav Hauganbd453a92012-07-05 14:21:34 -0700850EXPORT_SYMBOL(ion_do_cache_op);
Laura Abbottabcb6f72011-10-04 16:26:49 -0700851
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700852static int ion_debug_client_show(struct seq_file *s, void *unused)
853{
854 struct ion_client *client = s->private;
855 struct rb_node *n;
Olav Haugan854c9e12012-05-16 16:34:28 -0700856 struct rb_node *n2;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700857
Olav Haugan854c9e12012-05-16 16:34:28 -0700858 seq_printf(s, "%16.16s: %16.16s : %16.16s : %12.12s : %12.12s : %s\n",
859 "heap_name", "size_in_bytes", "handle refcount",
860 "buffer", "physical", "[domain,partition] - virt");
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700861
862 mutex_lock(&client->lock);
863 for (n = rb_first(&client->handles); n; n = rb_next(n)) {
864 struct ion_handle *handle = rb_entry(n, struct ion_handle,
865 node);
866 enum ion_heap_type type = handle->buffer->heap->type;
867
Olav Haugan854c9e12012-05-16 16:34:28 -0700868 seq_printf(s, "%16.16s: %16x : %16d : %12p",
Laura Abbott68c80642011-10-21 17:32:27 -0700869 handle->buffer->heap->name,
870 handle->buffer->size,
871 atomic_read(&handle->ref.refcount),
872 handle->buffer);
Olav Haugan854c9e12012-05-16 16:34:28 -0700873
874 if (type == ION_HEAP_TYPE_SYSTEM_CONTIG ||
875 type == ION_HEAP_TYPE_CARVEOUT ||
Mitchel Humpherys362b52b2012-09-13 10:53:22 -0700876 type == (enum ion_heap_type) ION_HEAP_TYPE_CP)
Olav Haugan854c9e12012-05-16 16:34:28 -0700877 seq_printf(s, " : %12lx", handle->buffer->priv_phys);
878 else
879 seq_printf(s, " : %12s", "N/A");
880
881 for (n2 = rb_first(&handle->buffer->iommu_maps); n2;
882 n2 = rb_next(n2)) {
883 struct ion_iommu_map *imap =
884 rb_entry(n2, struct ion_iommu_map, node);
885 seq_printf(s, " : [%d,%d] - %8lx",
886 imap->domain_info[DI_DOMAIN_NUM],
887 imap->domain_info[DI_PARTITION_NUM],
888 imap->iova_addr);
889 }
890 seq_printf(s, "\n");
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700891 }
892 mutex_unlock(&client->lock);
893
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700894 return 0;
895}
896
897static int ion_debug_client_open(struct inode *inode, struct file *file)
898{
899 return single_open(file, ion_debug_client_show, inode->i_private);
900}
901
902static const struct file_operations debug_client_fops = {
903 .open = ion_debug_client_open,
904 .read = seq_read,
905 .llseek = seq_lseek,
906 .release = single_release,
907};
908
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700909struct ion_client *ion_client_create(struct ion_device *dev,
910 unsigned int heap_mask,
911 const char *name)
912{
913 struct ion_client *client;
914 struct task_struct *task;
915 struct rb_node **p;
916 struct rb_node *parent = NULL;
917 struct ion_client *entry;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700918 pid_t pid;
Olav Haugane8a31972012-05-16 13:11:41 -0700919 unsigned int name_len;
920
921 if (!name) {
922 pr_err("%s: Name cannot be null\n", __func__);
923 return ERR_PTR(-EINVAL);
924 }
925 name_len = strnlen(name, 64);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700926
927 get_task_struct(current->group_leader);
928 task_lock(current->group_leader);
929 pid = task_pid_nr(current->group_leader);
930 /* don't bother to store task struct for kernel threads,
931 they can't be killed anyway */
932 if (current->group_leader->flags & PF_KTHREAD) {
933 put_task_struct(current->group_leader);
934 task = NULL;
935 } else {
936 task = current->group_leader;
937 }
938 task_unlock(current->group_leader);
939
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700940 client = kzalloc(sizeof(struct ion_client), GFP_KERNEL);
941 if (!client) {
Laura Abbottb14ed962012-01-30 14:18:08 -0800942 if (task)
943 put_task_struct(current->group_leader);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700944 return ERR_PTR(-ENOMEM);
945 }
946
947 client->dev = dev;
948 client->handles = RB_ROOT;
949 mutex_init(&client->lock);
Olav Haugan63e5f3b2012-01-11 16:42:37 -0800950
Olav Haugan6625c7d12012-01-24 13:50:43 -0800951 client->name = kzalloc(name_len+1, GFP_KERNEL);
Olav Haugan63e5f3b2012-01-11 16:42:37 -0800952 if (!client->name) {
953 put_task_struct(current->group_leader);
954 kfree(client);
955 return ERR_PTR(-ENOMEM);
956 } else {
Olav Haugan6625c7d12012-01-24 13:50:43 -0800957 strlcpy(client->name, name, name_len+1);
Olav Haugan63e5f3b2012-01-11 16:42:37 -0800958 }
959
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700960 client->heap_mask = heap_mask;
961 client->task = task;
962 client->pid = pid;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700963
964 mutex_lock(&dev->lock);
Laura Abbottb14ed962012-01-30 14:18:08 -0800965 p = &dev->clients.rb_node;
966 while (*p) {
967 parent = *p;
968 entry = rb_entry(parent, struct ion_client, node);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700969
Laura Abbottb14ed962012-01-30 14:18:08 -0800970 if (client < entry)
971 p = &(*p)->rb_left;
972 else if (client > entry)
973 p = &(*p)->rb_right;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700974 }
Laura Abbottb14ed962012-01-30 14:18:08 -0800975 rb_link_node(&client->node, parent, p);
976 rb_insert_color(&client->node, &dev->clients);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700977
Laura Abbotteed86032011-12-05 15:32:36 -0800978
979 client->debug_root = debugfs_create_file(name, 0664,
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700980 dev->debug_root, client,
981 &debug_client_fops);
982 mutex_unlock(&dev->lock);
983
984 return client;
985}
986
Laura Abbottb14ed962012-01-30 14:18:08 -0800987void ion_client_destroy(struct ion_client *client)
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700988{
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700989 struct ion_device *dev = client->dev;
990 struct rb_node *n;
991
992 pr_debug("%s: %d\n", __func__, __LINE__);
993 while ((n = rb_first(&client->handles))) {
994 struct ion_handle *handle = rb_entry(n, struct ion_handle,
995 node);
996 ion_handle_destroy(&handle->ref);
997 }
998 mutex_lock(&dev->lock);
Laura Abbottb14ed962012-01-30 14:18:08 -0800999 if (client->task)
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001000 put_task_struct(client->task);
Laura Abbottb14ed962012-01-30 14:18:08 -08001001 rb_erase(&client->node, &dev->clients);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001002 debugfs_remove_recursive(client->debug_root);
1003 mutex_unlock(&dev->lock);
1004
Olav Haugan63e5f3b2012-01-11 16:42:37 -08001005 kfree(client->name);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001006 kfree(client);
1007}
Olav Hauganbd453a92012-07-05 14:21:34 -07001008EXPORT_SYMBOL(ion_client_destroy);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001009
Laura Abbott273dd8e2011-10-12 14:26:33 -07001010int ion_handle_get_flags(struct ion_client *client, struct ion_handle *handle,
1011 unsigned long *flags)
Rebecca Schultz Zavin46d71332012-05-07 16:06:32 -07001012{
1013 struct ion_buffer *buffer;
Rebecca Schultz Zavin46d71332012-05-07 16:06:32 -07001014
1015 mutex_lock(&client->lock);
1016 if (!ion_handle_validate(client, handle)) {
Laura Abbott273dd8e2011-10-12 14:26:33 -07001017 pr_err("%s: invalid handle passed to %s.\n",
1018 __func__, __func__);
Rebecca Schultz Zavin46d71332012-05-07 16:06:32 -07001019 mutex_unlock(&client->lock);
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001020 return -EINVAL;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001021 }
Laura Abbott273dd8e2011-10-12 14:26:33 -07001022 buffer = handle->buffer;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001023 mutex_lock(&buffer->lock);
Laura Abbott273dd8e2011-10-12 14:26:33 -07001024 *flags = buffer->flags;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001025 mutex_unlock(&buffer->lock);
Laura Abbott273dd8e2011-10-12 14:26:33 -07001026 mutex_unlock(&client->lock);
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001027
Laura Abbott273dd8e2011-10-12 14:26:33 -07001028 return 0;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001029}
Laura Abbott273dd8e2011-10-12 14:26:33 -07001030EXPORT_SYMBOL(ion_handle_get_flags);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001031
Laura Abbott8c017362011-09-22 20:59:12 -07001032int ion_handle_get_size(struct ion_client *client, struct ion_handle *handle,
1033 unsigned long *size)
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001034{
Laura Abbott8c017362011-09-22 20:59:12 -07001035 struct ion_buffer *buffer;
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001036
Laura Abbott8c017362011-09-22 20:59:12 -07001037 mutex_lock(&client->lock);
1038 if (!ion_handle_validate(client, handle)) {
1039 pr_err("%s: invalid handle passed to %s.\n",
1040 __func__, __func__);
1041 mutex_unlock(&client->lock);
1042 return -EINVAL;
Rebecca Schultz Zavinbe4a1ee2012-04-26 20:44:10 -07001043 }
Laura Abbott8c017362011-09-22 20:59:12 -07001044 buffer = handle->buffer;
Rebecca Schultz Zavinbe4a1ee2012-04-26 20:44:10 -07001045 mutex_lock(&buffer->lock);
Laura Abbott8c017362011-09-22 20:59:12 -07001046 *size = buffer->size;
Rebecca Schultz Zavinbe4a1ee2012-04-26 20:44:10 -07001047 mutex_unlock(&buffer->lock);
Laura Abbott8c017362011-09-22 20:59:12 -07001048 mutex_unlock(&client->lock);
1049
1050 return 0;
1051}
1052EXPORT_SYMBOL(ion_handle_get_size);
1053
Laura Abbottb14ed962012-01-30 14:18:08 -08001054struct sg_table *ion_sg_table(struct ion_client *client,
1055 struct ion_handle *handle)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001056{
Laura Abbottb14ed962012-01-30 14:18:08 -08001057 struct ion_buffer *buffer;
1058 struct sg_table *table;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001059
Laura Abbottb14ed962012-01-30 14:18:08 -08001060 mutex_lock(&client->lock);
1061 if (!ion_handle_validate(client, handle)) {
1062 pr_err("%s: invalid handle passed to map_dma.\n",
1063 __func__);
1064 mutex_unlock(&client->lock);
1065 return ERR_PTR(-EINVAL);
1066 }
1067 buffer = handle->buffer;
1068 table = buffer->sg_table;
1069 mutex_unlock(&client->lock);
1070 return table;
1071}
Olav Hauganbd453a92012-07-05 14:21:34 -07001072EXPORT_SYMBOL(ion_sg_table);
Laura Abbottb14ed962012-01-30 14:18:08 -08001073
1074static struct sg_table *ion_map_dma_buf(struct dma_buf_attachment *attachment,
1075 enum dma_data_direction direction)
1076{
1077 struct dma_buf *dmabuf = attachment->dmabuf;
1078 struct ion_buffer *buffer = dmabuf->priv;
1079
1080 return buffer->sg_table;
1081}
1082
1083static void ion_unmap_dma_buf(struct dma_buf_attachment *attachment,
1084 struct sg_table *table,
1085 enum dma_data_direction direction)
1086{
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001087}
1088
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001089static void ion_vma_open(struct vm_area_struct *vma)
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001090{
Laura Abbottb14ed962012-01-30 14:18:08 -08001091 struct ion_buffer *buffer = vma->vm_private_data;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001092
1093 pr_debug("%s: %d\n", __func__, __LINE__);
Laura Abbottb14ed962012-01-30 14:18:08 -08001094
Rebecca Schultz Zavinbe4a1ee2012-04-26 20:44:10 -07001095 mutex_lock(&buffer->lock);
Laura Abbott77168502011-12-05 11:06:24 -08001096 buffer->umap_cnt++;
Rebecca Schultz Zavinbe4a1ee2012-04-26 20:44:10 -07001097 mutex_unlock(&buffer->lock);
1098}
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001099
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001100static void ion_vma_close(struct vm_area_struct *vma)
1101{
Laura Abbottb14ed962012-01-30 14:18:08 -08001102 struct ion_buffer *buffer = vma->vm_private_data;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001103
1104 pr_debug("%s: %d\n", __func__, __LINE__);
Laura Abbottb14ed962012-01-30 14:18:08 -08001105
Laura Abbott77168502011-12-05 11:06:24 -08001106 mutex_lock(&buffer->lock);
1107 buffer->umap_cnt--;
1108 mutex_unlock(&buffer->lock);
Laura Abbotta6835092011-11-14 15:27:02 -08001109
1110 if (buffer->heap->ops->unmap_user)
1111 buffer->heap->ops->unmap_user(buffer->heap, buffer);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001112}
1113
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001114static struct vm_operations_struct ion_vm_ops = {
1115 .open = ion_vma_open,
1116 .close = ion_vma_close,
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001117};
1118
Laura Abbottb14ed962012-01-30 14:18:08 -08001119static int ion_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma)
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001120{
Laura Abbottb14ed962012-01-30 14:18:08 -08001121 struct ion_buffer *buffer = dmabuf->priv;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001122 int ret;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001123
Laura Abbottb14ed962012-01-30 14:18:08 -08001124 if (!buffer->heap->ops->map_user) {
1125 pr_err("%s: this heap does not define a method for mapping "
1126 "to userspace\n", __func__);
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001127 return -EINVAL;
1128 }
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001129
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001130 mutex_lock(&buffer->lock);
1131 /* now map it to userspace */
Laura Abbottb14ed962012-01-30 14:18:08 -08001132 ret = buffer->heap->ops->map_user(buffer->heap, buffer, vma);
Laura Abbotte8bc7aa2011-12-09 14:49:33 -08001133
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001134 if (ret) {
Laura Abbottb14ed962012-01-30 14:18:08 -08001135 mutex_unlock(&buffer->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001136 pr_err("%s: failure mapping buffer to userspace\n",
1137 __func__);
Laura Abbottb14ed962012-01-30 14:18:08 -08001138 } else {
1139 buffer->umap_cnt++;
1140 mutex_unlock(&buffer->lock);
1141
1142 vma->vm_ops = &ion_vm_ops;
1143 /*
1144 * move the buffer into the vm_private_data so we can access it
1145 * from vma_open/close
1146 */
1147 vma->vm_private_data = buffer;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001148 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001149 return ret;
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001150}
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001151
Laura Abbottb14ed962012-01-30 14:18:08 -08001152static void ion_dma_buf_release(struct dma_buf *dmabuf)
1153{
1154 struct ion_buffer *buffer = dmabuf->priv;
1155 ion_buffer_put(buffer);
1156}
1157
1158static void *ion_dma_buf_kmap(struct dma_buf *dmabuf, unsigned long offset)
1159{
1160 struct ion_buffer *buffer = dmabuf->priv;
1161 return buffer->vaddr + offset;
1162}
1163
1164static void ion_dma_buf_kunmap(struct dma_buf *dmabuf, unsigned long offset,
1165 void *ptr)
1166{
1167 return;
1168}
1169
1170static int ion_dma_buf_begin_cpu_access(struct dma_buf *dmabuf, size_t start,
1171 size_t len,
1172 enum dma_data_direction direction)
1173{
1174 struct ion_buffer *buffer = dmabuf->priv;
1175 void *vaddr;
1176
1177 if (!buffer->heap->ops->map_kernel) {
1178 pr_err("%s: map kernel is not implemented by this heap.\n",
1179 __func__);
1180 return -ENODEV;
1181 }
1182
1183 mutex_lock(&buffer->lock);
1184 vaddr = ion_buffer_kmap_get(buffer);
1185 mutex_unlock(&buffer->lock);
1186 if (IS_ERR(vaddr))
1187 return PTR_ERR(vaddr);
1188 if (!vaddr)
1189 return -ENOMEM;
1190 return 0;
1191}
1192
1193static void ion_dma_buf_end_cpu_access(struct dma_buf *dmabuf, size_t start,
1194 size_t len,
1195 enum dma_data_direction direction)
1196{
1197 struct ion_buffer *buffer = dmabuf->priv;
1198
1199 mutex_lock(&buffer->lock);
1200 ion_buffer_kmap_put(buffer);
1201 mutex_unlock(&buffer->lock);
1202}
1203
1204struct dma_buf_ops dma_buf_ops = {
1205 .map_dma_buf = ion_map_dma_buf,
1206 .unmap_dma_buf = ion_unmap_dma_buf,
1207 .mmap = ion_mmap,
1208 .release = ion_dma_buf_release,
1209 .begin_cpu_access = ion_dma_buf_begin_cpu_access,
1210 .end_cpu_access = ion_dma_buf_end_cpu_access,
1211 .kmap_atomic = ion_dma_buf_kmap,
1212 .kunmap_atomic = ion_dma_buf_kunmap,
1213 .kmap = ion_dma_buf_kmap,
1214 .kunmap = ion_dma_buf_kunmap,
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001215};
1216
Laura Abbottb14ed962012-01-30 14:18:08 -08001217int ion_share_dma_buf(struct ion_client *client, struct ion_handle *handle)
1218{
1219 struct ion_buffer *buffer;
1220 struct dma_buf *dmabuf;
1221 bool valid_handle;
1222 int fd;
1223
1224 mutex_lock(&client->lock);
1225 valid_handle = ion_handle_validate(client, handle);
1226 mutex_unlock(&client->lock);
1227 if (!valid_handle) {
Olav Haugan0df59942012-07-05 14:27:30 -07001228 WARN(1, "%s: invalid handle passed to share.\n", __func__);
Laura Abbottb14ed962012-01-30 14:18:08 -08001229 return -EINVAL;
1230 }
1231
1232 buffer = handle->buffer;
1233 ion_buffer_get(buffer);
1234 dmabuf = dma_buf_export(buffer, &dma_buf_ops, buffer->size, O_RDWR);
1235 if (IS_ERR(dmabuf)) {
1236 ion_buffer_put(buffer);
1237 return PTR_ERR(dmabuf);
1238 }
1239 fd = dma_buf_fd(dmabuf, O_CLOEXEC);
Laura Abbottc2641f72012-08-01 18:06:18 -07001240 if (fd < 0)
Laura Abbottb14ed962012-01-30 14:18:08 -08001241 dma_buf_put(dmabuf);
Laura Abbottc2641f72012-08-01 18:06:18 -07001242
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001243 return fd;
Laura Abbottb14ed962012-01-30 14:18:08 -08001244}
Olav Hauganbd453a92012-07-05 14:21:34 -07001245EXPORT_SYMBOL(ion_share_dma_buf);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001246
Laura Abbottb14ed962012-01-30 14:18:08 -08001247struct ion_handle *ion_import_dma_buf(struct ion_client *client, int fd)
1248{
1249 struct dma_buf *dmabuf;
1250 struct ion_buffer *buffer;
1251 struct ion_handle *handle;
1252
1253 dmabuf = dma_buf_get(fd);
1254 if (IS_ERR_OR_NULL(dmabuf))
1255 return ERR_PTR(PTR_ERR(dmabuf));
1256 /* if this memory came from ion */
1257
1258 if (dmabuf->ops != &dma_buf_ops) {
1259 pr_err("%s: can not import dmabuf from another exporter\n",
1260 __func__);
1261 dma_buf_put(dmabuf);
1262 return ERR_PTR(-EINVAL);
1263 }
1264 buffer = dmabuf->priv;
1265
1266 mutex_lock(&client->lock);
1267 /* if a handle exists for this buffer just take a reference to it */
1268 handle = ion_handle_lookup(client, buffer);
1269 if (!IS_ERR_OR_NULL(handle)) {
1270 ion_handle_get(handle);
1271 goto end;
1272 }
1273 handle = ion_handle_create(client, buffer);
1274 if (IS_ERR_OR_NULL(handle))
1275 goto end;
1276 ion_handle_add(client, handle);
1277end:
1278 mutex_unlock(&client->lock);
1279 dma_buf_put(dmabuf);
1280 return handle;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001281}
Olav Hauganbd453a92012-07-05 14:21:34 -07001282EXPORT_SYMBOL(ion_import_dma_buf);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001283
1284static long ion_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1285{
1286 struct ion_client *client = filp->private_data;
1287
1288 switch (cmd) {
1289 case ION_IOC_ALLOC:
1290 {
1291 struct ion_allocation_data data;
1292
1293 if (copy_from_user(&data, (void __user *)arg, sizeof(data)))
1294 return -EFAULT;
1295 data.handle = ion_alloc(client, data.len, data.align,
Hanumant Singh7d72bad2012-08-29 18:39:44 -07001296 data.heap_mask, data.flags);
KyongHo Cho9ae7e012011-09-07 11:27:07 +09001297
Laura Abbottb14ed962012-01-30 14:18:08 -08001298 if (IS_ERR(data.handle))
1299 return PTR_ERR(data.handle);
KyongHo Cho9ae7e012011-09-07 11:27:07 +09001300
Laura Abbottb14ed962012-01-30 14:18:08 -08001301 if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
1302 ion_free(client, data.handle);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001303 return -EFAULT;
Laura Abbottb14ed962012-01-30 14:18:08 -08001304 }
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001305 break;
1306 }
1307 case ION_IOC_FREE:
1308 {
1309 struct ion_handle_data data;
1310 bool valid;
1311
1312 if (copy_from_user(&data, (void __user *)arg,
1313 sizeof(struct ion_handle_data)))
1314 return -EFAULT;
1315 mutex_lock(&client->lock);
1316 valid = ion_handle_validate(client, data.handle);
1317 mutex_unlock(&client->lock);
1318 if (!valid)
1319 return -EINVAL;
1320 ion_free(client, data.handle);
1321 break;
1322 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001323 case ION_IOC_MAP:
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001324 case ION_IOC_SHARE:
1325 {
1326 struct ion_fd_data data;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001327 if (copy_from_user(&data, (void __user *)arg, sizeof(data)))
1328 return -EFAULT;
Laura Abbottb14ed962012-01-30 14:18:08 -08001329
Laura Abbottb14ed962012-01-30 14:18:08 -08001330 data.fd = ion_share_dma_buf(client, data.handle);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001331 if (copy_to_user((void __user *)arg, &data, sizeof(data)))
1332 return -EFAULT;
Olav Hauganc2d2cf52012-05-15 14:40:11 -07001333 if (data.fd < 0)
1334 return data.fd;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001335 break;
1336 }
1337 case ION_IOC_IMPORT:
1338 {
1339 struct ion_fd_data data;
Olav Hauganc2d2cf52012-05-15 14:40:11 -07001340 int ret = 0;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001341 if (copy_from_user(&data, (void __user *)arg,
1342 sizeof(struct ion_fd_data)))
1343 return -EFAULT;
Laura Abbottb14ed962012-01-30 14:18:08 -08001344 data.handle = ion_import_dma_buf(client, data.fd);
Olav Haugan865e97f2012-05-15 14:40:11 -07001345 if (IS_ERR(data.handle)) {
1346 ret = PTR_ERR(data.handle);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001347 data.handle = NULL;
Olav Haugan865e97f2012-05-15 14:40:11 -07001348 }
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001349 if (copy_to_user((void __user *)arg, &data,
1350 sizeof(struct ion_fd_data)))
1351 return -EFAULT;
Olav Hauganc2d2cf52012-05-15 14:40:11 -07001352 if (ret < 0)
1353 return ret;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001354 break;
1355 }
1356 case ION_IOC_CUSTOM:
1357 {
1358 struct ion_device *dev = client->dev;
1359 struct ion_custom_data data;
1360
1361 if (!dev->custom_ioctl)
1362 return -ENOTTY;
1363 if (copy_from_user(&data, (void __user *)arg,
1364 sizeof(struct ion_custom_data)))
1365 return -EFAULT;
1366 return dev->custom_ioctl(client, data.cmd, data.arg);
1367 }
Laura Abbottabcb6f72011-10-04 16:26:49 -07001368 case ION_IOC_CLEAN_CACHES:
Mitchel Humpherysd88b8eb2012-09-04 17:00:29 -07001369 return client->dev->custom_ioctl(client,
1370 ION_IOC_CLEAN_CACHES, arg);
Laura Abbottabcb6f72011-10-04 16:26:49 -07001371 case ION_IOC_INV_CACHES:
Mitchel Humpherysd88b8eb2012-09-04 17:00:29 -07001372 return client->dev->custom_ioctl(client,
1373 ION_IOC_INV_CACHES, arg);
Laura Abbottabcb6f72011-10-04 16:26:49 -07001374 case ION_IOC_CLEAN_INV_CACHES:
Mitchel Humpherysd88b8eb2012-09-04 17:00:29 -07001375 return client->dev->custom_ioctl(client,
1376 ION_IOC_CLEAN_INV_CACHES, arg);
Laura Abbott273dd8e2011-10-12 14:26:33 -07001377 case ION_IOC_GET_FLAGS:
Mitchel Humpherysd88b8eb2012-09-04 17:00:29 -07001378 return client->dev->custom_ioctl(client,
1379 ION_IOC_GET_FLAGS, arg);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001380 default:
1381 return -ENOTTY;
1382 }
1383 return 0;
1384}
1385
1386static int ion_release(struct inode *inode, struct file *file)
1387{
1388 struct ion_client *client = file->private_data;
1389
1390 pr_debug("%s: %d\n", __func__, __LINE__);
Laura Abbottb14ed962012-01-30 14:18:08 -08001391 ion_client_destroy(client);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001392 return 0;
1393}
1394
1395static int ion_open(struct inode *inode, struct file *file)
1396{
1397 struct miscdevice *miscdev = file->private_data;
1398 struct ion_device *dev = container_of(miscdev, struct ion_device, dev);
1399 struct ion_client *client;
Laura Abbotteed86032011-12-05 15:32:36 -08001400 char debug_name[64];
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001401
1402 pr_debug("%s: %d\n", __func__, __LINE__);
Laura Abbotteed86032011-12-05 15:32:36 -08001403 snprintf(debug_name, 64, "%u", task_pid_nr(current->group_leader));
1404 client = ion_client_create(dev, -1, debug_name);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001405 if (IS_ERR_OR_NULL(client))
1406 return PTR_ERR(client);
1407 file->private_data = client;
1408
1409 return 0;
1410}
1411
1412static const struct file_operations ion_fops = {
1413 .owner = THIS_MODULE,
1414 .open = ion_open,
1415 .release = ion_release,
1416 .unlocked_ioctl = ion_ioctl,
1417};
1418
1419static size_t ion_debug_heap_total(struct ion_client *client,
Laura Abbott3647ac32011-10-31 14:09:53 -07001420 enum ion_heap_ids id)
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001421{
1422 size_t size = 0;
1423 struct rb_node *n;
1424
1425 mutex_lock(&client->lock);
1426 for (n = rb_first(&client->handles); n; n = rb_next(n)) {
1427 struct ion_handle *handle = rb_entry(n,
1428 struct ion_handle,
1429 node);
Laura Abbott3647ac32011-10-31 14:09:53 -07001430 if (handle->buffer->heap->id == id)
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001431 size += handle->buffer->size;
1432 }
1433 mutex_unlock(&client->lock);
1434 return size;
1435}
1436
Olav Haugan0671b9a2012-05-25 11:58:56 -07001437/**
1438 * Searches through a clients handles to find if the buffer is owned
1439 * by this client. Used for debug output.
1440 * @param client pointer to candidate owner of buffer
1441 * @param buf pointer to buffer that we are trying to find the owner of
1442 * @return 1 if found, 0 otherwise
1443 */
1444static int ion_debug_find_buffer_owner(const struct ion_client *client,
1445 const struct ion_buffer *buf)
1446{
1447 struct rb_node *n;
1448
1449 for (n = rb_first(&client->handles); n; n = rb_next(n)) {
1450 const struct ion_handle *handle = rb_entry(n,
1451 const struct ion_handle,
1452 node);
1453 if (handle->buffer == buf)
1454 return 1;
1455 }
1456 return 0;
1457}
1458
1459/**
1460 * Adds mem_map_data pointer to the tree of mem_map
1461 * Used for debug output.
1462 * @param mem_map The mem_map tree
1463 * @param data The new data to add to the tree
1464 */
1465static void ion_debug_mem_map_add(struct rb_root *mem_map,
1466 struct mem_map_data *data)
1467{
1468 struct rb_node **p = &mem_map->rb_node;
1469 struct rb_node *parent = NULL;
1470 struct mem_map_data *entry;
1471
1472 while (*p) {
1473 parent = *p;
1474 entry = rb_entry(parent, struct mem_map_data, node);
1475
1476 if (data->addr < entry->addr) {
1477 p = &(*p)->rb_left;
1478 } else if (data->addr > entry->addr) {
1479 p = &(*p)->rb_right;
1480 } else {
1481 pr_err("%s: mem_map_data already found.", __func__);
1482 BUG();
1483 }
1484 }
1485 rb_link_node(&data->node, parent, p);
1486 rb_insert_color(&data->node, mem_map);
1487}
1488
1489/**
1490 * Search for an owner of a buffer by iterating over all ION clients.
1491 * @param dev ion device containing pointers to all the clients.
1492 * @param buffer pointer to buffer we are trying to find the owner of.
1493 * @return name of owner.
1494 */
1495const char *ion_debug_locate_owner(const struct ion_device *dev,
1496 const struct ion_buffer *buffer)
1497{
1498 struct rb_node *j;
1499 const char *client_name = NULL;
1500
Laura Abbottb14ed962012-01-30 14:18:08 -08001501 for (j = rb_first(&dev->clients); j && !client_name;
Olav Haugan0671b9a2012-05-25 11:58:56 -07001502 j = rb_next(j)) {
1503 struct ion_client *client = rb_entry(j, struct ion_client,
1504 node);
1505 if (ion_debug_find_buffer_owner(client, buffer))
1506 client_name = client->name;
1507 }
1508 return client_name;
1509}
1510
1511/**
1512 * Create a mem_map of the heap.
1513 * @param s seq_file to log error message to.
1514 * @param heap The heap to create mem_map for.
1515 * @param mem_map The mem map to be created.
1516 */
1517void ion_debug_mem_map_create(struct seq_file *s, struct ion_heap *heap,
1518 struct rb_root *mem_map)
1519{
1520 struct ion_device *dev = heap->dev;
1521 struct rb_node *n;
1522
1523 for (n = rb_first(&dev->buffers); n; n = rb_next(n)) {
1524 struct ion_buffer *buffer =
1525 rb_entry(n, struct ion_buffer, node);
1526 if (buffer->heap->id == heap->id) {
1527 struct mem_map_data *data =
1528 kzalloc(sizeof(*data), GFP_KERNEL);
1529 if (!data) {
1530 seq_printf(s, "ERROR: out of memory. "
1531 "Part of memory map will not be logged\n");
1532 break;
1533 }
1534 data->addr = buffer->priv_phys;
1535 data->addr_end = buffer->priv_phys + buffer->size-1;
1536 data->size = buffer->size;
1537 data->client_name = ion_debug_locate_owner(dev, buffer);
1538 ion_debug_mem_map_add(mem_map, data);
1539 }
1540 }
1541}
1542
1543/**
1544 * Free the memory allocated by ion_debug_mem_map_create
1545 * @param mem_map The mem map to free.
1546 */
1547static void ion_debug_mem_map_destroy(struct rb_root *mem_map)
1548{
1549 if (mem_map) {
1550 struct rb_node *n;
1551 while ((n = rb_first(mem_map)) != 0) {
1552 struct mem_map_data *data =
1553 rb_entry(n, struct mem_map_data, node);
1554 rb_erase(&data->node, mem_map);
1555 kfree(data);
1556 }
1557 }
1558}
1559
1560/**
1561 * Print heap debug information.
1562 * @param s seq_file to log message to.
1563 * @param heap pointer to heap that we will print debug information for.
1564 */
1565static void ion_heap_print_debug(struct seq_file *s, struct ion_heap *heap)
1566{
1567 if (heap->ops->print_debug) {
1568 struct rb_root mem_map = RB_ROOT;
1569 ion_debug_mem_map_create(s, heap, &mem_map);
1570 heap->ops->print_debug(heap, s, &mem_map);
1571 ion_debug_mem_map_destroy(&mem_map);
1572 }
1573}
1574
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001575static int ion_debug_heap_show(struct seq_file *s, void *unused)
1576{
1577 struct ion_heap *heap = s->private;
1578 struct ion_device *dev = heap->dev;
1579 struct rb_node *n;
1580
Olav Haugane4900b52012-05-25 11:58:03 -07001581 mutex_lock(&dev->lock);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001582 seq_printf(s, "%16.s %16.s %16.s\n", "client", "pid", "size");
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001583
Laura Abbottb14ed962012-01-30 14:18:08 -08001584 for (n = rb_first(&dev->clients); n; n = rb_next(n)) {
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001585 struct ion_client *client = rb_entry(n, struct ion_client,
1586 node);
Laura Abbott3647ac32011-10-31 14:09:53 -07001587 size_t size = ion_debug_heap_total(client, heap->id);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001588 if (!size)
1589 continue;
Laura Abbottb14ed962012-01-30 14:18:08 -08001590 if (client->task) {
1591 char task_comm[TASK_COMM_LEN];
1592
1593 get_task_comm(task_comm, client->task);
1594 seq_printf(s, "%16.s %16u %16u\n", task_comm,
1595 client->pid, size);
1596 } else {
1597 seq_printf(s, "%16.s %16u %16u\n", client->name,
1598 client->pid, size);
1599 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001600 }
Olav Haugan0671b9a2012-05-25 11:58:56 -07001601 ion_heap_print_debug(s, heap);
Olav Haugane4900b52012-05-25 11:58:03 -07001602 mutex_unlock(&dev->lock);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001603 return 0;
1604}
1605
1606static int ion_debug_heap_open(struct inode *inode, struct file *file)
1607{
1608 return single_open(file, ion_debug_heap_show, inode->i_private);
1609}
1610
1611static const struct file_operations debug_heap_fops = {
1612 .open = ion_debug_heap_open,
1613 .read = seq_read,
1614 .llseek = seq_lseek,
1615 .release = single_release,
1616};
1617
1618void ion_device_add_heap(struct ion_device *dev, struct ion_heap *heap)
1619{
1620 struct rb_node **p = &dev->heaps.rb_node;
1621 struct rb_node *parent = NULL;
1622 struct ion_heap *entry;
1623
Laura Abbottb14ed962012-01-30 14:18:08 -08001624 if (!heap->ops->allocate || !heap->ops->free || !heap->ops->map_dma ||
1625 !heap->ops->unmap_dma)
1626 pr_err("%s: can not add heap with invalid ops struct.\n",
1627 __func__);
1628
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001629 heap->dev = dev;
1630 mutex_lock(&dev->lock);
1631 while (*p) {
1632 parent = *p;
1633 entry = rb_entry(parent, struct ion_heap, node);
1634
1635 if (heap->id < entry->id) {
1636 p = &(*p)->rb_left;
1637 } else if (heap->id > entry->id ) {
1638 p = &(*p)->rb_right;
1639 } else {
1640 pr_err("%s: can not insert multiple heaps with "
1641 "id %d\n", __func__, heap->id);
1642 goto end;
1643 }
1644 }
1645
1646 rb_link_node(&heap->node, parent, p);
1647 rb_insert_color(&heap->node, &dev->heaps);
1648 debugfs_create_file(heap->name, 0664, dev->debug_root, heap,
1649 &debug_heap_fops);
1650end:
1651 mutex_unlock(&dev->lock);
1652}
1653
Laura Abbott93619302012-10-11 11:51:40 -07001654int ion_secure_handle(struct ion_client *client, struct ion_handle *handle,
1655 int version, void *data, int flags)
1656{
1657 int ret = -EINVAL;
1658 struct ion_heap *heap;
1659 struct ion_buffer *buffer;
1660
1661 mutex_lock(&client->lock);
1662 if (!ion_handle_validate(client, handle)) {
1663 WARN(1, "%s: invalid handle passed to secure.\n", __func__);
1664 goto out_unlock;
1665 }
1666
1667 buffer = handle->buffer;
1668 heap = buffer->heap;
1669
1670 if (heap->type != (enum ion_heap_type) ION_HEAP_TYPE_CP) {
1671 pr_err("%s: cannot secure buffer from non secure heap\n",
1672 __func__);
1673 goto out_unlock;
1674 }
1675
1676 BUG_ON(!buffer->heap->ops->secure_buffer);
1677 /*
1678 * Protect the handle via the client lock to ensure we aren't
1679 * racing with free
1680 */
1681 ret = buffer->heap->ops->secure_buffer(buffer, version, data, flags);
1682
1683out_unlock:
1684 mutex_unlock(&client->lock);
1685 return ret;
1686}
1687
1688int ion_unsecure_handle(struct ion_client *client, struct ion_handle *handle)
1689{
1690 int ret = -EINVAL;
1691 struct ion_heap *heap;
1692 struct ion_buffer *buffer;
1693
1694 mutex_lock(&client->lock);
1695 if (!ion_handle_validate(client, handle)) {
1696 WARN(1, "%s: invalid handle passed to secure.\n", __func__);
1697 goto out_unlock;
1698 }
1699
1700 buffer = handle->buffer;
1701 heap = buffer->heap;
1702
1703 if (heap->type != (enum ion_heap_type) ION_HEAP_TYPE_CP) {
1704 pr_err("%s: cannot secure buffer from non secure heap\n",
1705 __func__);
1706 goto out_unlock;
1707 }
1708
1709 BUG_ON(!buffer->heap->ops->unsecure_buffer);
1710 /*
1711 * Protect the handle via the client lock to ensure we aren't
1712 * racing with free
1713 */
1714 ret = buffer->heap->ops->unsecure_buffer(buffer, 0);
1715
1716out_unlock:
1717 mutex_unlock(&client->lock);
1718 return ret;
1719}
1720
Laura Abbott7e446482012-06-13 15:59:39 -07001721int ion_secure_heap(struct ion_device *dev, int heap_id, int version,
1722 void *data)
Olav Haugan0a852512012-01-09 10:20:55 -08001723{
1724 struct rb_node *n;
1725 int ret_val = 0;
1726
1727 /*
1728 * traverse the list of heaps available in this system
1729 * and find the heap that is specified.
1730 */
1731 mutex_lock(&dev->lock);
1732 for (n = rb_first(&dev->heaps); n != NULL; n = rb_next(n)) {
1733 struct ion_heap *heap = rb_entry(n, struct ion_heap, node);
Mitchel Humpherys362b52b2012-09-13 10:53:22 -07001734 if (heap->type != (enum ion_heap_type) ION_HEAP_TYPE_CP)
Olav Haugan0a852512012-01-09 10:20:55 -08001735 continue;
1736 if (ION_HEAP(heap->id) != heap_id)
1737 continue;
1738 if (heap->ops->secure_heap)
Laura Abbott7e446482012-06-13 15:59:39 -07001739 ret_val = heap->ops->secure_heap(heap, version, data);
Olav Haugan0a852512012-01-09 10:20:55 -08001740 else
1741 ret_val = -EINVAL;
1742 break;
1743 }
1744 mutex_unlock(&dev->lock);
1745 return ret_val;
1746}
Olav Hauganbd453a92012-07-05 14:21:34 -07001747EXPORT_SYMBOL(ion_secure_heap);
Olav Haugan0a852512012-01-09 10:20:55 -08001748
Laura Abbott7e446482012-06-13 15:59:39 -07001749int ion_unsecure_heap(struct ion_device *dev, int heap_id, int version,
1750 void *data)
Olav Haugan0a852512012-01-09 10:20:55 -08001751{
1752 struct rb_node *n;
1753 int ret_val = 0;
1754
1755 /*
1756 * traverse the list of heaps available in this system
1757 * and find the heap that is specified.
1758 */
1759 mutex_lock(&dev->lock);
1760 for (n = rb_first(&dev->heaps); n != NULL; n = rb_next(n)) {
1761 struct ion_heap *heap = rb_entry(n, struct ion_heap, node);
Mitchel Humpherys362b52b2012-09-13 10:53:22 -07001762 if (heap->type != (enum ion_heap_type) ION_HEAP_TYPE_CP)
Olav Haugan0a852512012-01-09 10:20:55 -08001763 continue;
1764 if (ION_HEAP(heap->id) != heap_id)
1765 continue;
1766 if (heap->ops->secure_heap)
Laura Abbott7e446482012-06-13 15:59:39 -07001767 ret_val = heap->ops->unsecure_heap(heap, version, data);
Olav Haugan0a852512012-01-09 10:20:55 -08001768 else
1769 ret_val = -EINVAL;
1770 break;
1771 }
1772 mutex_unlock(&dev->lock);
1773 return ret_val;
1774}
Olav Hauganbd453a92012-07-05 14:21:34 -07001775EXPORT_SYMBOL(ion_unsecure_heap);
Olav Haugan0a852512012-01-09 10:20:55 -08001776
Laura Abbott404f8242011-10-31 14:22:53 -07001777static int ion_debug_leak_show(struct seq_file *s, void *unused)
1778{
1779 struct ion_device *dev = s->private;
1780 struct rb_node *n;
1781 struct rb_node *n2;
1782
1783 /* mark all buffers as 1 */
1784 seq_printf(s, "%16.s %16.s %16.s %16.s\n", "buffer", "heap", "size",
1785 "ref cnt");
1786 mutex_lock(&dev->lock);
1787 for (n = rb_first(&dev->buffers); n; n = rb_next(n)) {
1788 struct ion_buffer *buf = rb_entry(n, struct ion_buffer,
1789 node);
1790
1791 buf->marked = 1;
1792 }
1793
1794 /* now see which buffers we can access */
Laura Abbottb14ed962012-01-30 14:18:08 -08001795 for (n = rb_first(&dev->clients); n; n = rb_next(n)) {
Laura Abbott404f8242011-10-31 14:22:53 -07001796 struct ion_client *client = rb_entry(n, struct ion_client,
1797 node);
1798
1799 mutex_lock(&client->lock);
1800 for (n2 = rb_first(&client->handles); n2; n2 = rb_next(n2)) {
1801 struct ion_handle *handle = rb_entry(n2,
1802 struct ion_handle, node);
1803
1804 handle->buffer->marked = 0;
1805
1806 }
1807 mutex_unlock(&client->lock);
1808
1809 }
1810
Laura Abbott404f8242011-10-31 14:22:53 -07001811 /* And anyone still marked as a 1 means a leaked handle somewhere */
1812 for (n = rb_first(&dev->buffers); n; n = rb_next(n)) {
1813 struct ion_buffer *buf = rb_entry(n, struct ion_buffer,
1814 node);
1815
1816 if (buf->marked == 1)
1817 seq_printf(s, "%16.x %16.s %16.x %16.d\n",
1818 (int)buf, buf->heap->name, buf->size,
1819 atomic_read(&buf->ref.refcount));
1820 }
1821 mutex_unlock(&dev->lock);
1822 return 0;
1823}
1824
1825static int ion_debug_leak_open(struct inode *inode, struct file *file)
1826{
1827 return single_open(file, ion_debug_leak_show, inode->i_private);
1828}
1829
1830static const struct file_operations debug_leak_fops = {
1831 .open = ion_debug_leak_open,
1832 .read = seq_read,
1833 .llseek = seq_lseek,
1834 .release = single_release,
1835};
1836
1837
1838
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001839struct ion_device *ion_device_create(long (*custom_ioctl)
1840 (struct ion_client *client,
1841 unsigned int cmd,
1842 unsigned long arg))
1843{
1844 struct ion_device *idev;
1845 int ret;
1846
1847 idev = kzalloc(sizeof(struct ion_device), GFP_KERNEL);
1848 if (!idev)
1849 return ERR_PTR(-ENOMEM);
1850
1851 idev->dev.minor = MISC_DYNAMIC_MINOR;
1852 idev->dev.name = "ion";
1853 idev->dev.fops = &ion_fops;
1854 idev->dev.parent = NULL;
1855 ret = misc_register(&idev->dev);
1856 if (ret) {
1857 pr_err("ion: failed to register misc device.\n");
1858 return ERR_PTR(ret);
1859 }
1860
1861 idev->debug_root = debugfs_create_dir("ion", NULL);
1862 if (IS_ERR_OR_NULL(idev->debug_root))
1863 pr_err("ion: failed to create debug files.\n");
1864
1865 idev->custom_ioctl = custom_ioctl;
1866 idev->buffers = RB_ROOT;
1867 mutex_init(&idev->lock);
1868 idev->heaps = RB_ROOT;
Laura Abbottb14ed962012-01-30 14:18:08 -08001869 idev->clients = RB_ROOT;
Laura Abbott404f8242011-10-31 14:22:53 -07001870 debugfs_create_file("check_leaked_fds", 0664, idev->debug_root, idev,
1871 &debug_leak_fops);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001872 return idev;
1873}
1874
1875void ion_device_destroy(struct ion_device *dev)
1876{
1877 misc_deregister(&dev->dev);
1878 /* XXX need to free the heaps and clients ? */
1879 kfree(dev);
1880}
Laura Abbottb14ed962012-01-30 14:18:08 -08001881
1882void __init ion_reserve(struct ion_platform_data *data)
1883{
1884 int i, ret;
1885
1886 for (i = 0; i < data->nr; i++) {
1887 if (data->heaps[i].size == 0)
1888 continue;
1889 ret = memblock_reserve(data->heaps[i].base,
1890 data->heaps[i].size);
1891 if (ret)
1892 pr_err("memblock reserve of %x@%lx failed\n",
1893 data->heaps[i].size,
1894 data->heaps[i].base);
1895 }
1896}