blob: 12137d7149e3b2181db06cabb2485763daa14903 [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 Humpherysfd02cfb2012-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 Singh2ac41c92012-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
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700264static void ion_buffer_destroy(struct kref *kref)
265{
266 struct ion_buffer *buffer = container_of(kref, struct ion_buffer, ref);
267 struct ion_device *dev = buffer->dev;
268
Laura Abbottb14ed962012-01-30 14:18:08 -0800269 if (WARN_ON(buffer->kmap_cnt > 0))
270 buffer->heap->ops->unmap_kernel(buffer->heap, buffer);
271
272 buffer->heap->ops->unmap_dma(buffer->heap, buffer);
273
Olav Hauganb3676592012-03-02 15:02:25 -0800274 ion_iommu_delayed_unmap(buffer);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700275 buffer->heap->ops->free(buffer);
276 mutex_lock(&dev->lock);
277 rb_erase(&buffer->node, &dev->buffers);
278 mutex_unlock(&dev->lock);
279 kfree(buffer);
280}
281
282static void ion_buffer_get(struct ion_buffer *buffer)
283{
284 kref_get(&buffer->ref);
285}
286
287static int ion_buffer_put(struct ion_buffer *buffer)
288{
289 return kref_put(&buffer->ref, ion_buffer_destroy);
290}
291
292static struct ion_handle *ion_handle_create(struct ion_client *client,
293 struct ion_buffer *buffer)
294{
295 struct ion_handle *handle;
296
297 handle = kzalloc(sizeof(struct ion_handle), GFP_KERNEL);
298 if (!handle)
299 return ERR_PTR(-ENOMEM);
300 kref_init(&handle->ref);
301 rb_init_node(&handle->node);
302 handle->client = client;
303 ion_buffer_get(buffer);
304 handle->buffer = buffer;
305
306 return handle;
307}
308
Laura Abbottb14ed962012-01-30 14:18:08 -0800309static void ion_handle_kmap_put(struct ion_handle *);
310
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700311static void ion_handle_destroy(struct kref *kref)
312{
313 struct ion_handle *handle = container_of(kref, struct ion_handle, ref);
Laura Abbottb14ed962012-01-30 14:18:08 -0800314 struct ion_client *client = handle->client;
315 struct ion_buffer *buffer = handle->buffer;
316
Laura Abbottb14ed962012-01-30 14:18:08 -0800317 mutex_lock(&buffer->lock);
318 while (handle->kmap_cnt)
319 ion_handle_kmap_put(handle);
320 mutex_unlock(&buffer->lock);
321
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700322 if (!RB_EMPTY_NODE(&handle->node))
Laura Abbottb14ed962012-01-30 14:18:08 -0800323 rb_erase(&handle->node, &client->handles);
Laura Abbottb14ed962012-01-30 14:18:08 -0800324
325 ion_buffer_put(buffer);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700326 kfree(handle);
327}
328
329struct ion_buffer *ion_handle_buffer(struct ion_handle *handle)
330{
331 return handle->buffer;
332}
333
334static void ion_handle_get(struct ion_handle *handle)
335{
336 kref_get(&handle->ref);
337}
338
339static int ion_handle_put(struct ion_handle *handle)
340{
341 return kref_put(&handle->ref, ion_handle_destroy);
342}
343
344static struct ion_handle *ion_handle_lookup(struct ion_client *client,
345 struct ion_buffer *buffer)
346{
347 struct rb_node *n;
348
349 for (n = rb_first(&client->handles); n; n = rb_next(n)) {
350 struct ion_handle *handle = rb_entry(n, struct ion_handle,
351 node);
352 if (handle->buffer == buffer)
353 return handle;
354 }
355 return NULL;
356}
357
358static bool ion_handle_validate(struct ion_client *client, struct ion_handle *handle)
359{
360 struct rb_node *n = client->handles.rb_node;
361
362 while (n) {
363 struct ion_handle *handle_node = rb_entry(n, struct ion_handle,
364 node);
365 if (handle < handle_node)
366 n = n->rb_left;
367 else if (handle > handle_node)
368 n = n->rb_right;
369 else
370 return true;
371 }
372 return false;
373}
374
375static void ion_handle_add(struct ion_client *client, struct ion_handle *handle)
376{
377 struct rb_node **p = &client->handles.rb_node;
378 struct rb_node *parent = NULL;
379 struct ion_handle *entry;
380
381 while (*p) {
382 parent = *p;
383 entry = rb_entry(parent, struct ion_handle, node);
384
385 if (handle < entry)
386 p = &(*p)->rb_left;
387 else if (handle > entry)
388 p = &(*p)->rb_right;
389 else
390 WARN(1, "%s: buffer already found.", __func__);
391 }
392
393 rb_link_node(&handle->node, parent, p);
394 rb_insert_color(&handle->node, &client->handles);
395}
396
397struct ion_handle *ion_alloc(struct ion_client *client, size_t len,
Hanumant Singh2ac41c92012-08-29 18:39:44 -0700398 size_t align, unsigned int heap_mask,
399 unsigned int flags)
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700400{
401 struct rb_node *n;
402 struct ion_handle *handle;
403 struct ion_device *dev = client->dev;
404 struct ion_buffer *buffer = NULL;
Olav Haugan0a852512012-01-09 10:20:55 -0800405 unsigned long secure_allocation = flags & ION_SECURE;
Olav Haugan35e2f2f2012-01-11 17:31:47 -0800406 const unsigned int MAX_DBG_STR_LEN = 64;
407 char dbg_str[MAX_DBG_STR_LEN];
408 unsigned int dbg_str_idx = 0;
409
410 dbg_str[0] = '\0';
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700411
412 /*
413 * traverse the list of heaps available in this system in priority
414 * order. If the heap type is supported by the client, and matches the
415 * request of the caller allocate from it. Repeat until allocate has
416 * succeeded or all heaps have been tried
417 */
Laura Abbottb14ed962012-01-30 14:18:08 -0800418 if (WARN_ON(!len))
419 return ERR_PTR(-EINVAL);
420
421 len = PAGE_ALIGN(len);
422
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700423 mutex_lock(&dev->lock);
424 for (n = rb_first(&dev->heaps); n != NULL; n = rb_next(n)) {
425 struct ion_heap *heap = rb_entry(n, struct ion_heap, node);
426 /* if the client doesn't support this heap type */
427 if (!((1 << heap->type) & client->heap_mask))
428 continue;
429 /* if the caller didn't specify this heap type */
Hanumant Singh2ac41c92012-08-29 18:39:44 -0700430 if (!((1 << heap->id) & heap_mask))
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700431 continue;
Olav Haugan0a852512012-01-09 10:20:55 -0800432 /* Do not allow un-secure heap if secure is specified */
433 if (secure_allocation && (heap->type != ION_HEAP_TYPE_CP))
434 continue;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700435 buffer = ion_buffer_create(heap, dev, len, align, flags);
436 if (!IS_ERR_OR_NULL(buffer))
437 break;
Olav Haugan35e2f2f2012-01-11 17:31:47 -0800438 if (dbg_str_idx < MAX_DBG_STR_LEN) {
439 unsigned int len_left = MAX_DBG_STR_LEN-dbg_str_idx-1;
440 int ret_value = snprintf(&dbg_str[dbg_str_idx],
441 len_left, "%s ", heap->name);
442 if (ret_value >= len_left) {
443 /* overflow */
444 dbg_str[MAX_DBG_STR_LEN-1] = '\0';
445 dbg_str_idx = MAX_DBG_STR_LEN;
446 } else if (ret_value >= 0) {
447 dbg_str_idx += ret_value;
448 } else {
449 /* error */
450 dbg_str[MAX_DBG_STR_LEN-1] = '\0';
451 }
452 }
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700453 }
454 mutex_unlock(&dev->lock);
455
Laura Abbottb14ed962012-01-30 14:18:08 -0800456 if (buffer == NULL)
457 return ERR_PTR(-ENODEV);
458
459 if (IS_ERR(buffer)) {
Olav Haugan35e2f2f2012-01-11 17:31:47 -0800460 pr_debug("ION is unable to allocate 0x%x bytes (alignment: "
461 "0x%x) from heap(s) %sfor client %s with heap "
462 "mask 0x%x\n",
463 len, align, dbg_str, client->name, client->heap_mask);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700464 return ERR_PTR(PTR_ERR(buffer));
Olav Haugan35e2f2f2012-01-11 17:31:47 -0800465 }
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700466
467 handle = ion_handle_create(client, buffer);
468
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700469 /*
470 * ion_buffer_create will create a buffer with a ref_cnt of 1,
471 * and ion_handle_create will take a second reference, drop one here
472 */
473 ion_buffer_put(buffer);
474
Laura Abbottb14ed962012-01-30 14:18:08 -0800475 if (!IS_ERR(handle)) {
476 mutex_lock(&client->lock);
477 ion_handle_add(client, handle);
478 mutex_unlock(&client->lock);
479 }
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700480
Laura Abbottb14ed962012-01-30 14:18:08 -0800481
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700482 return handle;
483}
Olav Hauganbd2b6922012-01-25 09:28:55 -0800484EXPORT_SYMBOL(ion_alloc);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700485
486void ion_free(struct ion_client *client, struct ion_handle *handle)
487{
488 bool valid_handle;
489
490 BUG_ON(client != handle->client);
491
492 mutex_lock(&client->lock);
493 valid_handle = ion_handle_validate(client, handle);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700494 if (!valid_handle) {
Laura Abbottec149ff2012-01-26 13:33:11 -0800495 mutex_unlock(&client->lock);
Olav Haugan6ede5672012-04-19 10:20:22 -0700496 WARN(1, "%s: invalid handle passed to free.\n", __func__);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700497 return;
498 }
Laura Abbottb14ed962012-01-30 14:18:08 -0800499 ion_handle_put(handle);
Ajay Dudani6ca95312012-08-20 15:41:11 -0700500 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700501}
Olav Hauganbd2b6922012-01-25 09:28:55 -0800502EXPORT_SYMBOL(ion_free);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700503
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700504int ion_phys(struct ion_client *client, struct ion_handle *handle,
505 ion_phys_addr_t *addr, size_t *len)
506{
507 struct ion_buffer *buffer;
508 int ret;
509
510 mutex_lock(&client->lock);
511 if (!ion_handle_validate(client, handle)) {
512 mutex_unlock(&client->lock);
513 return -EINVAL;
514 }
515
516 buffer = handle->buffer;
517
518 if (!buffer->heap->ops->phys) {
519 pr_err("%s: ion_phys is not implemented by this heap.\n",
520 __func__);
521 mutex_unlock(&client->lock);
522 return -ENODEV;
523 }
524 mutex_unlock(&client->lock);
525 ret = buffer->heap->ops->phys(buffer->heap, buffer, addr, len);
526 return ret;
527}
Olav Hauganbd2b6922012-01-25 09:28:55 -0800528EXPORT_SYMBOL(ion_phys);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700529
Laura Abbottb14ed962012-01-30 14:18:08 -0800530static void *ion_buffer_kmap_get(struct ion_buffer *buffer)
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700531{
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700532 void *vaddr;
533
Laura Abbottb14ed962012-01-30 14:18:08 -0800534 if (buffer->kmap_cnt) {
535 buffer->kmap_cnt++;
536 return buffer->vaddr;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700537 }
Laura Abbottb14ed962012-01-30 14:18:08 -0800538 vaddr = buffer->heap->ops->map_kernel(buffer->heap, buffer);
539 if (IS_ERR_OR_NULL(vaddr))
540 return vaddr;
541 buffer->vaddr = vaddr;
542 buffer->kmap_cnt++;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700543 return vaddr;
544}
Laura Abbottb14ed962012-01-30 14:18:08 -0800545
546static void *ion_handle_kmap_get(struct ion_handle *handle)
547{
548 struct ion_buffer *buffer = handle->buffer;
549 void *vaddr;
550
551 if (handle->kmap_cnt) {
552 handle->kmap_cnt++;
553 return buffer->vaddr;
554 }
555 vaddr = ion_buffer_kmap_get(buffer);
556 if (IS_ERR_OR_NULL(vaddr))
557 return vaddr;
558 handle->kmap_cnt++;
559 return vaddr;
560}
561
562static void ion_buffer_kmap_put(struct ion_buffer *buffer)
563{
564 buffer->kmap_cnt--;
565 if (!buffer->kmap_cnt) {
566 buffer->heap->ops->unmap_kernel(buffer->heap, buffer);
567 buffer->vaddr = NULL;
568 }
569}
570
571static void ion_handle_kmap_put(struct ion_handle *handle)
572{
573 struct ion_buffer *buffer = handle->buffer;
574
575 handle->kmap_cnt--;
576 if (!handle->kmap_cnt)
577 ion_buffer_kmap_put(buffer);
578}
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700579
Olav Hauganb3676592012-03-02 15:02:25 -0800580static struct ion_iommu_map *__ion_iommu_map(struct ion_buffer *buffer,
Laura Abbott8c017362011-09-22 20:59:12 -0700581 int domain_num, int partition_num, unsigned long align,
582 unsigned long iova_length, unsigned long flags,
583 unsigned long *iova)
584{
585 struct ion_iommu_map *data;
586 int ret;
587
588 data = kmalloc(sizeof(*data), GFP_ATOMIC);
589
590 if (!data)
Olav Hauganb3676592012-03-02 15:02:25 -0800591 return ERR_PTR(-ENOMEM);
Laura Abbott8c017362011-09-22 20:59:12 -0700592
593 data->buffer = buffer;
594 iommu_map_domain(data) = domain_num;
595 iommu_map_partition(data) = partition_num;
596
597 ret = buffer->heap->ops->map_iommu(buffer, data,
598 domain_num,
599 partition_num,
600 align,
601 iova_length,
602 flags);
603
604 if (ret)
605 goto out;
606
607 kref_init(&data->ref);
608 *iova = data->iova_addr;
609
610 ion_iommu_add(buffer, data);
611
Olav Hauganb3676592012-03-02 15:02:25 -0800612 return data;
Laura Abbott8c017362011-09-22 20:59:12 -0700613
614out:
Laura Abbott8c017362011-09-22 20:59:12 -0700615 kfree(data);
Olav Hauganb3676592012-03-02 15:02:25 -0800616 return ERR_PTR(ret);
Laura Abbott8c017362011-09-22 20:59:12 -0700617}
618
619int ion_map_iommu(struct ion_client *client, struct ion_handle *handle,
620 int domain_num, int partition_num, unsigned long align,
621 unsigned long iova_length, unsigned long *iova,
622 unsigned long *buffer_size,
Olav Hauganb3676592012-03-02 15:02:25 -0800623 unsigned long flags, unsigned long iommu_flags)
Laura Abbott8c017362011-09-22 20:59:12 -0700624{
625 struct ion_buffer *buffer;
626 struct ion_iommu_map *iommu_map;
627 int ret = 0;
628
Olav Haugan79e9ffa2012-02-24 13:11:10 -0800629 if (ION_IS_CACHED(flags)) {
630 pr_err("%s: Cannot map iommu as cached.\n", __func__);
631 return -EINVAL;
632 }
633
Laura Abbott8c017362011-09-22 20:59:12 -0700634 mutex_lock(&client->lock);
635 if (!ion_handle_validate(client, handle)) {
636 pr_err("%s: invalid handle passed to map_kernel.\n",
637 __func__);
638 mutex_unlock(&client->lock);
639 return -EINVAL;
640 }
641
642 buffer = handle->buffer;
643 mutex_lock(&buffer->lock);
644
645 if (!handle->buffer->heap->ops->map_iommu) {
646 pr_err("%s: map_iommu is not implemented by this heap.\n",
647 __func__);
648 ret = -ENODEV;
649 goto out;
650 }
651
Laura Abbott8c017362011-09-22 20:59:12 -0700652 /*
653 * If clients don't want a custom iova length, just use whatever
654 * the buffer size is
655 */
656 if (!iova_length)
657 iova_length = buffer->size;
658
659 if (buffer->size > iova_length) {
660 pr_debug("%s: iova length %lx is not at least buffer size"
661 " %x\n", __func__, iova_length, buffer->size);
662 ret = -EINVAL;
663 goto out;
664 }
665
666 if (buffer->size & ~PAGE_MASK) {
667 pr_debug("%s: buffer size %x is not aligned to %lx", __func__,
668 buffer->size, PAGE_SIZE);
669 ret = -EINVAL;
670 goto out;
671 }
672
673 if (iova_length & ~PAGE_MASK) {
674 pr_debug("%s: iova_length %lx is not aligned to %lx", __func__,
675 iova_length, PAGE_SIZE);
676 ret = -EINVAL;
677 goto out;
678 }
679
680 iommu_map = ion_iommu_lookup(buffer, domain_num, partition_num);
Olav Hauganb3676592012-03-02 15:02:25 -0800681 if (!iommu_map) {
682 iommu_map = __ion_iommu_map(buffer, domain_num, partition_num,
683 align, iova_length, flags, iova);
Laura Abbottb14ed962012-01-30 14:18:08 -0800684 if (!IS_ERR_OR_NULL(iommu_map)) {
Olav Hauganb3676592012-03-02 15:02:25 -0800685 iommu_map->flags = iommu_flags;
686
687 if (iommu_map->flags & ION_IOMMU_UNMAP_DELAYED)
688 kref_get(&iommu_map->ref);
689 }
Laura Abbott8c017362011-09-22 20:59:12 -0700690 } else {
Olav Hauganb3676592012-03-02 15:02:25 -0800691 if (iommu_map->flags != iommu_flags) {
692 pr_err("%s: handle %p is already mapped with iommu flags %lx, trying to map with flags %lx\n",
693 __func__, handle,
694 iommu_map->flags, iommu_flags);
Olav Hauganb3676592012-03-02 15:02:25 -0800695 ret = -EINVAL;
696 } else if (iommu_map->mapped_size != iova_length) {
Laura Abbott8c017362011-09-22 20:59:12 -0700697 pr_err("%s: handle %p is already mapped with length"
Olav Hauganb3676592012-03-02 15:02:25 -0800698 " %x, trying to map with length %lx\n",
Laura Abbott8c017362011-09-22 20:59:12 -0700699 __func__, handle, iommu_map->mapped_size,
700 iova_length);
Laura Abbott8c017362011-09-22 20:59:12 -0700701 ret = -EINVAL;
702 } else {
703 kref_get(&iommu_map->ref);
704 *iova = iommu_map->iova_addr;
705 }
706 }
Laura Abbottb14ed962012-01-30 14:18:08 -0800707 if (!ret)
708 buffer->iommu_map_cnt++;
Laura Abbott8c017362011-09-22 20:59:12 -0700709 *buffer_size = buffer->size;
710out:
711 mutex_unlock(&buffer->lock);
712 mutex_unlock(&client->lock);
713 return ret;
714}
715EXPORT_SYMBOL(ion_map_iommu);
716
717static void ion_iommu_release(struct kref *kref)
718{
719 struct ion_iommu_map *map = container_of(kref, struct ion_iommu_map,
720 ref);
721 struct ion_buffer *buffer = map->buffer;
722
723 rb_erase(&map->node, &buffer->iommu_maps);
724 buffer->heap->ops->unmap_iommu(map);
725 kfree(map);
726}
727
728void ion_unmap_iommu(struct ion_client *client, struct ion_handle *handle,
729 int domain_num, int partition_num)
730{
731 struct ion_iommu_map *iommu_map;
732 struct ion_buffer *buffer;
733
734 mutex_lock(&client->lock);
735 buffer = handle->buffer;
736
737 mutex_lock(&buffer->lock);
738
739 iommu_map = ion_iommu_lookup(buffer, domain_num, partition_num);
740
741 if (!iommu_map) {
742 WARN(1, "%s: (%d,%d) was never mapped for %p\n", __func__,
743 domain_num, partition_num, buffer);
744 goto out;
745 }
746
Laura Abbott8c017362011-09-22 20:59:12 -0700747 kref_put(&iommu_map->ref, ion_iommu_release);
748
Laura Abbottb14ed962012-01-30 14:18:08 -0800749 buffer->iommu_map_cnt--;
Laura Abbott8c017362011-09-22 20:59:12 -0700750out:
751 mutex_unlock(&buffer->lock);
752
753 mutex_unlock(&client->lock);
754
755}
756EXPORT_SYMBOL(ion_unmap_iommu);
757
Laura Abbottb14ed962012-01-30 14:18:08 -0800758void *ion_map_kernel(struct ion_client *client, struct ion_handle *handle,
759 unsigned long flags)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700760{
761 struct ion_buffer *buffer;
Laura Abbottb14ed962012-01-30 14:18:08 -0800762 void *vaddr;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700763
764 mutex_lock(&client->lock);
765 if (!ion_handle_validate(client, handle)) {
Laura Abbottb14ed962012-01-30 14:18:08 -0800766 pr_err("%s: invalid handle passed to map_kernel.\n",
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700767 __func__);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700768 mutex_unlock(&client->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700769 return ERR_PTR(-EINVAL);
770 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700771
Laura Abbottb14ed962012-01-30 14:18:08 -0800772 buffer = handle->buffer;
773
774 if (!handle->buffer->heap->ops->map_kernel) {
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700775 pr_err("%s: map_kernel is not implemented by this heap.\n",
776 __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(-ENODEV);
779 }
Laura Abbott894fd582011-08-19 13:33:56 -0700780
Laura Abbott8c017362011-09-22 20:59:12 -0700781 if (ion_validate_buffer_flags(buffer, flags)) {
Laura Abbottb14ed962012-01-30 14:18:08 -0800782 mutex_unlock(&client->lock);
783 return ERR_PTR(-EEXIST);
Laura Abbott894fd582011-08-19 13:33:56 -0700784 }
785
Laura Abbottb14ed962012-01-30 14:18:08 -0800786 mutex_lock(&buffer->lock);
787 vaddr = ion_handle_kmap_get(handle);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700788 mutex_unlock(&buffer->lock);
789 mutex_unlock(&client->lock);
Laura Abbottb14ed962012-01-30 14:18:08 -0800790 return vaddr;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700791}
Olav Hauganbd453a92012-07-05 14:21:34 -0700792EXPORT_SYMBOL(ion_map_kernel);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700793
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700794void ion_unmap_kernel(struct ion_client *client, struct ion_handle *handle)
795{
796 struct ion_buffer *buffer;
797
798 mutex_lock(&client->lock);
799 buffer = handle->buffer;
800 mutex_lock(&buffer->lock);
Laura Abbottb14ed962012-01-30 14:18:08 -0800801 ion_handle_kmap_put(handle);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700802 mutex_unlock(&buffer->lock);
803 mutex_unlock(&client->lock);
804}
Olav Hauganbd453a92012-07-05 14:21:34 -0700805EXPORT_SYMBOL(ion_unmap_kernel);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700806
Olav Haugan41f85792012-02-08 15:28:05 -0800807int ion_do_cache_op(struct ion_client *client, struct ion_handle *handle,
Laura Abbottabcb6f72011-10-04 16:26:49 -0700808 void *uaddr, unsigned long offset, unsigned long len,
809 unsigned int cmd)
810{
811 struct ion_buffer *buffer;
Laura Abbottabcb6f72011-10-04 16:26:49 -0700812 int ret = -EINVAL;
813
814 mutex_lock(&client->lock);
815 if (!ion_handle_validate(client, handle)) {
816 pr_err("%s: invalid handle passed to do_cache_op.\n",
817 __func__);
818 mutex_unlock(&client->lock);
819 return -EINVAL;
820 }
821 buffer = handle->buffer;
822 mutex_lock(&buffer->lock);
823
Laura Abbottcbaa6682011-10-19 12:14:14 -0700824 if (!ION_IS_CACHED(buffer->flags)) {
Laura Abbottabcb6f72011-10-04 16:26:49 -0700825 ret = 0;
826 goto out;
827 }
828
829 if (!handle->buffer->heap->ops->cache_op) {
830 pr_err("%s: cache_op is not implemented by this heap.\n",
831 __func__);
832 ret = -ENODEV;
833 goto out;
834 }
835
Laura Abbottabcb6f72011-10-04 16:26:49 -0700836
837 ret = buffer->heap->ops->cache_op(buffer->heap, buffer, uaddr,
838 offset, len, cmd);
839
840out:
841 mutex_unlock(&buffer->lock);
842 mutex_unlock(&client->lock);
843 return ret;
844
845}
Olav Hauganbd453a92012-07-05 14:21:34 -0700846EXPORT_SYMBOL(ion_do_cache_op);
Laura Abbottabcb6f72011-10-04 16:26:49 -0700847
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700848static int ion_debug_client_show(struct seq_file *s, void *unused)
849{
850 struct ion_client *client = s->private;
851 struct rb_node *n;
Olav Haugan854c9e12012-05-16 16:34:28 -0700852 struct rb_node *n2;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700853
Olav Haugan854c9e12012-05-16 16:34:28 -0700854 seq_printf(s, "%16.16s: %16.16s : %16.16s : %12.12s : %12.12s : %s\n",
855 "heap_name", "size_in_bytes", "handle refcount",
856 "buffer", "physical", "[domain,partition] - virt");
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700857
858 mutex_lock(&client->lock);
859 for (n = rb_first(&client->handles); n; n = rb_next(n)) {
860 struct ion_handle *handle = rb_entry(n, struct ion_handle,
861 node);
862 enum ion_heap_type type = handle->buffer->heap->type;
863
Olav Haugan854c9e12012-05-16 16:34:28 -0700864 seq_printf(s, "%16.16s: %16x : %16d : %12p",
Laura Abbott68c80642011-10-21 17:32:27 -0700865 handle->buffer->heap->name,
866 handle->buffer->size,
867 atomic_read(&handle->ref.refcount),
868 handle->buffer);
Olav Haugan854c9e12012-05-16 16:34:28 -0700869
870 if (type == ION_HEAP_TYPE_SYSTEM_CONTIG ||
871 type == ION_HEAP_TYPE_CARVEOUT ||
872 type == ION_HEAP_TYPE_CP)
873 seq_printf(s, " : %12lx", handle->buffer->priv_phys);
874 else
875 seq_printf(s, " : %12s", "N/A");
876
877 for (n2 = rb_first(&handle->buffer->iommu_maps); n2;
878 n2 = rb_next(n2)) {
879 struct ion_iommu_map *imap =
880 rb_entry(n2, struct ion_iommu_map, node);
881 seq_printf(s, " : [%d,%d] - %8lx",
882 imap->domain_info[DI_DOMAIN_NUM],
883 imap->domain_info[DI_PARTITION_NUM],
884 imap->iova_addr);
885 }
886 seq_printf(s, "\n");
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700887 }
888 mutex_unlock(&client->lock);
889
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700890 return 0;
891}
892
893static int ion_debug_client_open(struct inode *inode, struct file *file)
894{
895 return single_open(file, ion_debug_client_show, inode->i_private);
896}
897
898static const struct file_operations debug_client_fops = {
899 .open = ion_debug_client_open,
900 .read = seq_read,
901 .llseek = seq_lseek,
902 .release = single_release,
903};
904
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700905struct ion_client *ion_client_create(struct ion_device *dev,
906 unsigned int heap_mask,
907 const char *name)
908{
909 struct ion_client *client;
910 struct task_struct *task;
911 struct rb_node **p;
912 struct rb_node *parent = NULL;
913 struct ion_client *entry;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700914 pid_t pid;
Olav Haugane8a31972012-05-16 13:11:41 -0700915 unsigned int name_len;
916
917 if (!name) {
918 pr_err("%s: Name cannot be null\n", __func__);
919 return ERR_PTR(-EINVAL);
920 }
921 name_len = strnlen(name, 64);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700922
923 get_task_struct(current->group_leader);
924 task_lock(current->group_leader);
925 pid = task_pid_nr(current->group_leader);
926 /* don't bother to store task struct for kernel threads,
927 they can't be killed anyway */
928 if (current->group_leader->flags & PF_KTHREAD) {
929 put_task_struct(current->group_leader);
930 task = NULL;
931 } else {
932 task = current->group_leader;
933 }
934 task_unlock(current->group_leader);
935
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700936 client = kzalloc(sizeof(struct ion_client), GFP_KERNEL);
937 if (!client) {
Laura Abbottb14ed962012-01-30 14:18:08 -0800938 if (task)
939 put_task_struct(current->group_leader);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700940 return ERR_PTR(-ENOMEM);
941 }
942
943 client->dev = dev;
944 client->handles = RB_ROOT;
945 mutex_init(&client->lock);
Olav Haugan63e5f3b2012-01-11 16:42:37 -0800946
Olav Haugan6625c7d2012-01-24 13:50:43 -0800947 client->name = kzalloc(name_len+1, GFP_KERNEL);
Olav Haugan63e5f3b2012-01-11 16:42:37 -0800948 if (!client->name) {
949 put_task_struct(current->group_leader);
950 kfree(client);
951 return ERR_PTR(-ENOMEM);
952 } else {
Olav Haugan6625c7d2012-01-24 13:50:43 -0800953 strlcpy(client->name, name, name_len+1);
Olav Haugan63e5f3b2012-01-11 16:42:37 -0800954 }
955
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700956 client->heap_mask = heap_mask;
957 client->task = task;
958 client->pid = pid;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700959
960 mutex_lock(&dev->lock);
Laura Abbottb14ed962012-01-30 14:18:08 -0800961 p = &dev->clients.rb_node;
962 while (*p) {
963 parent = *p;
964 entry = rb_entry(parent, struct ion_client, node);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700965
Laura Abbottb14ed962012-01-30 14:18:08 -0800966 if (client < entry)
967 p = &(*p)->rb_left;
968 else if (client > entry)
969 p = &(*p)->rb_right;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700970 }
Laura Abbottb14ed962012-01-30 14:18:08 -0800971 rb_link_node(&client->node, parent, p);
972 rb_insert_color(&client->node, &dev->clients);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700973
Laura Abbotteed86032011-12-05 15:32:36 -0800974
975 client->debug_root = debugfs_create_file(name, 0664,
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700976 dev->debug_root, client,
977 &debug_client_fops);
978 mutex_unlock(&dev->lock);
979
980 return client;
981}
982
Laura Abbottb14ed962012-01-30 14:18:08 -0800983void ion_client_destroy(struct ion_client *client)
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700984{
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700985 struct ion_device *dev = client->dev;
986 struct rb_node *n;
987
988 pr_debug("%s: %d\n", __func__, __LINE__);
989 while ((n = rb_first(&client->handles))) {
990 struct ion_handle *handle = rb_entry(n, struct ion_handle,
991 node);
992 ion_handle_destroy(&handle->ref);
993 }
994 mutex_lock(&dev->lock);
Laura Abbottb14ed962012-01-30 14:18:08 -0800995 if (client->task)
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700996 put_task_struct(client->task);
Laura Abbottb14ed962012-01-30 14:18:08 -0800997 rb_erase(&client->node, &dev->clients);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700998 debugfs_remove_recursive(client->debug_root);
999 mutex_unlock(&dev->lock);
1000
Olav Haugan63e5f3b2012-01-11 16:42:37 -08001001 kfree(client->name);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001002 kfree(client);
1003}
Olav Hauganbd453a92012-07-05 14:21:34 -07001004EXPORT_SYMBOL(ion_client_destroy);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001005
Laura Abbott273dd8e2011-10-12 14:26:33 -07001006int ion_handle_get_flags(struct ion_client *client, struct ion_handle *handle,
1007 unsigned long *flags)
Rebecca Schultz Zavin46d71332012-05-07 16:06:32 -07001008{
1009 struct ion_buffer *buffer;
Rebecca Schultz Zavin46d71332012-05-07 16:06:32 -07001010
1011 mutex_lock(&client->lock);
1012 if (!ion_handle_validate(client, handle)) {
Laura Abbott273dd8e2011-10-12 14:26:33 -07001013 pr_err("%s: invalid handle passed to %s.\n",
1014 __func__, __func__);
Rebecca Schultz Zavin46d71332012-05-07 16:06:32 -07001015 mutex_unlock(&client->lock);
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001016 return -EINVAL;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001017 }
Laura Abbott273dd8e2011-10-12 14:26:33 -07001018 buffer = handle->buffer;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001019 mutex_lock(&buffer->lock);
Laura Abbott273dd8e2011-10-12 14:26:33 -07001020 *flags = buffer->flags;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001021 mutex_unlock(&buffer->lock);
Laura Abbott273dd8e2011-10-12 14:26:33 -07001022 mutex_unlock(&client->lock);
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001023
Laura Abbott273dd8e2011-10-12 14:26:33 -07001024 return 0;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001025}
Laura Abbott273dd8e2011-10-12 14:26:33 -07001026EXPORT_SYMBOL(ion_handle_get_flags);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001027
Laura Abbott8c017362011-09-22 20:59:12 -07001028int ion_handle_get_size(struct ion_client *client, struct ion_handle *handle,
1029 unsigned long *size)
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001030{
Laura Abbott8c017362011-09-22 20:59:12 -07001031 struct ion_buffer *buffer;
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001032
Laura Abbott8c017362011-09-22 20:59:12 -07001033 mutex_lock(&client->lock);
1034 if (!ion_handle_validate(client, handle)) {
1035 pr_err("%s: invalid handle passed to %s.\n",
1036 __func__, __func__);
1037 mutex_unlock(&client->lock);
1038 return -EINVAL;
Rebecca Schultz Zavinbe4a1ee2012-04-26 20:44:10 -07001039 }
Laura Abbott8c017362011-09-22 20:59:12 -07001040 buffer = handle->buffer;
Rebecca Schultz Zavinbe4a1ee2012-04-26 20:44:10 -07001041 mutex_lock(&buffer->lock);
Laura Abbott8c017362011-09-22 20:59:12 -07001042 *size = buffer->size;
Rebecca Schultz Zavinbe4a1ee2012-04-26 20:44:10 -07001043 mutex_unlock(&buffer->lock);
Laura Abbott8c017362011-09-22 20:59:12 -07001044 mutex_unlock(&client->lock);
1045
1046 return 0;
1047}
1048EXPORT_SYMBOL(ion_handle_get_size);
1049
Laura Abbottb14ed962012-01-30 14:18:08 -08001050struct sg_table *ion_sg_table(struct ion_client *client,
1051 struct ion_handle *handle)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001052{
Laura Abbottb14ed962012-01-30 14:18:08 -08001053 struct ion_buffer *buffer;
1054 struct sg_table *table;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001055
Laura Abbottb14ed962012-01-30 14:18:08 -08001056 mutex_lock(&client->lock);
1057 if (!ion_handle_validate(client, handle)) {
1058 pr_err("%s: invalid handle passed to map_dma.\n",
1059 __func__);
1060 mutex_unlock(&client->lock);
1061 return ERR_PTR(-EINVAL);
1062 }
1063 buffer = handle->buffer;
1064 table = buffer->sg_table;
1065 mutex_unlock(&client->lock);
1066 return table;
1067}
Olav Hauganbd453a92012-07-05 14:21:34 -07001068EXPORT_SYMBOL(ion_sg_table);
Laura Abbottb14ed962012-01-30 14:18:08 -08001069
1070static struct sg_table *ion_map_dma_buf(struct dma_buf_attachment *attachment,
1071 enum dma_data_direction direction)
1072{
1073 struct dma_buf *dmabuf = attachment->dmabuf;
1074 struct ion_buffer *buffer = dmabuf->priv;
1075
1076 return buffer->sg_table;
1077}
1078
1079static void ion_unmap_dma_buf(struct dma_buf_attachment *attachment,
1080 struct sg_table *table,
1081 enum dma_data_direction direction)
1082{
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001083}
1084
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001085static void ion_vma_open(struct vm_area_struct *vma)
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001086{
Laura Abbottb14ed962012-01-30 14:18:08 -08001087 struct ion_buffer *buffer = vma->vm_private_data;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001088
1089 pr_debug("%s: %d\n", __func__, __LINE__);
Laura Abbottb14ed962012-01-30 14:18:08 -08001090
Rebecca Schultz Zavinbe4a1ee2012-04-26 20:44:10 -07001091 mutex_lock(&buffer->lock);
Laura Abbott77168502011-12-05 11:06:24 -08001092 buffer->umap_cnt++;
Rebecca Schultz Zavinbe4a1ee2012-04-26 20:44:10 -07001093 mutex_unlock(&buffer->lock);
1094}
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001095
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001096static void ion_vma_close(struct vm_area_struct *vma)
1097{
Laura Abbottb14ed962012-01-30 14:18:08 -08001098 struct ion_buffer *buffer = vma->vm_private_data;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001099
1100 pr_debug("%s: %d\n", __func__, __LINE__);
Laura Abbottb14ed962012-01-30 14:18:08 -08001101
Laura Abbott77168502011-12-05 11:06:24 -08001102 mutex_lock(&buffer->lock);
1103 buffer->umap_cnt--;
1104 mutex_unlock(&buffer->lock);
Laura Abbotta6835092011-11-14 15:27:02 -08001105
1106 if (buffer->heap->ops->unmap_user)
1107 buffer->heap->ops->unmap_user(buffer->heap, buffer);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001108}
1109
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001110static struct vm_operations_struct ion_vm_ops = {
1111 .open = ion_vma_open,
1112 .close = ion_vma_close,
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001113};
1114
Laura Abbottb14ed962012-01-30 14:18:08 -08001115static int ion_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma)
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001116{
Laura Abbottb14ed962012-01-30 14:18:08 -08001117 struct ion_buffer *buffer = dmabuf->priv;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001118 int ret;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001119
Laura Abbottb14ed962012-01-30 14:18:08 -08001120 if (!buffer->heap->ops->map_user) {
1121 pr_err("%s: this heap does not define a method for mapping "
1122 "to userspace\n", __func__);
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001123 return -EINVAL;
1124 }
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001125
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001126 mutex_lock(&buffer->lock);
1127 /* now map it to userspace */
Laura Abbottb14ed962012-01-30 14:18:08 -08001128 ret = buffer->heap->ops->map_user(buffer->heap, buffer, vma);
Laura Abbotte8bc7aa2011-12-09 14:49:33 -08001129
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001130 if (ret) {
Laura Abbottb14ed962012-01-30 14:18:08 -08001131 mutex_unlock(&buffer->lock);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001132 pr_err("%s: failure mapping buffer to userspace\n",
1133 __func__);
Laura Abbottb14ed962012-01-30 14:18:08 -08001134 } else {
1135 buffer->umap_cnt++;
1136 mutex_unlock(&buffer->lock);
1137
1138 vma->vm_ops = &ion_vm_ops;
1139 /*
1140 * move the buffer into the vm_private_data so we can access it
1141 * from vma_open/close
1142 */
1143 vma->vm_private_data = buffer;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001144 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001145 return ret;
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001146}
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001147
Laura Abbottb14ed962012-01-30 14:18:08 -08001148static void ion_dma_buf_release(struct dma_buf *dmabuf)
1149{
1150 struct ion_buffer *buffer = dmabuf->priv;
1151 ion_buffer_put(buffer);
1152}
1153
1154static void *ion_dma_buf_kmap(struct dma_buf *dmabuf, unsigned long offset)
1155{
1156 struct ion_buffer *buffer = dmabuf->priv;
1157 return buffer->vaddr + offset;
1158}
1159
1160static void ion_dma_buf_kunmap(struct dma_buf *dmabuf, unsigned long offset,
1161 void *ptr)
1162{
1163 return;
1164}
1165
1166static int ion_dma_buf_begin_cpu_access(struct dma_buf *dmabuf, size_t start,
1167 size_t len,
1168 enum dma_data_direction direction)
1169{
1170 struct ion_buffer *buffer = dmabuf->priv;
1171 void *vaddr;
1172
1173 if (!buffer->heap->ops->map_kernel) {
1174 pr_err("%s: map kernel is not implemented by this heap.\n",
1175 __func__);
1176 return -ENODEV;
1177 }
1178
1179 mutex_lock(&buffer->lock);
1180 vaddr = ion_buffer_kmap_get(buffer);
1181 mutex_unlock(&buffer->lock);
1182 if (IS_ERR(vaddr))
1183 return PTR_ERR(vaddr);
1184 if (!vaddr)
1185 return -ENOMEM;
1186 return 0;
1187}
1188
1189static void ion_dma_buf_end_cpu_access(struct dma_buf *dmabuf, size_t start,
1190 size_t len,
1191 enum dma_data_direction direction)
1192{
1193 struct ion_buffer *buffer = dmabuf->priv;
1194
1195 mutex_lock(&buffer->lock);
1196 ion_buffer_kmap_put(buffer);
1197 mutex_unlock(&buffer->lock);
1198}
1199
1200struct dma_buf_ops dma_buf_ops = {
1201 .map_dma_buf = ion_map_dma_buf,
1202 .unmap_dma_buf = ion_unmap_dma_buf,
1203 .mmap = ion_mmap,
1204 .release = ion_dma_buf_release,
1205 .begin_cpu_access = ion_dma_buf_begin_cpu_access,
1206 .end_cpu_access = ion_dma_buf_end_cpu_access,
1207 .kmap_atomic = ion_dma_buf_kmap,
1208 .kunmap_atomic = ion_dma_buf_kunmap,
1209 .kmap = ion_dma_buf_kmap,
1210 .kunmap = ion_dma_buf_kunmap,
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001211};
1212
Laura Abbottb14ed962012-01-30 14:18:08 -08001213static int ion_share_set_flags(struct ion_client *client,
1214 struct ion_handle *handle,
1215 unsigned long flags)
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001216{
Laura Abbottb14ed962012-01-30 14:18:08 -08001217 struct ion_buffer *buffer;
1218 bool valid_handle;
Mitchel Humpherys97e21232012-09-11 15:59:11 -07001219 unsigned long ion_flags = 0;
Laura Abbottb14ed962012-01-30 14:18:08 -08001220 if (flags & O_DSYNC)
Mitchel Humpherys97e21232012-09-11 15:59:11 -07001221 ion_flags = ION_SET_UNCACHED(ion_flags);
1222 else
1223 ion_flags = ION_SET_CACHED(ion_flags);
1224
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001225
Laura Abbottb14ed962012-01-30 14:18:08 -08001226 mutex_lock(&client->lock);
1227 valid_handle = ion_handle_validate(client, handle);
1228 mutex_unlock(&client->lock);
1229 if (!valid_handle) {
1230 WARN(1, "%s: invalid handle passed to set_flags.\n", __func__);
1231 return -EINVAL;
1232 }
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001233
Laura Abbottb14ed962012-01-30 14:18:08 -08001234 buffer = handle->buffer;
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001235
Laura Abbottb14ed962012-01-30 14:18:08 -08001236 return 0;
1237}
Laura Abbott4b5d0482011-09-27 18:35:14 -07001238
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001239
Laura Abbottb14ed962012-01-30 14:18:08 -08001240int ion_share_dma_buf(struct ion_client *client, struct ion_handle *handle)
1241{
1242 struct ion_buffer *buffer;
1243 struct dma_buf *dmabuf;
1244 bool valid_handle;
1245 int fd;
1246
1247 mutex_lock(&client->lock);
1248 valid_handle = ion_handle_validate(client, handle);
1249 mutex_unlock(&client->lock);
1250 if (!valid_handle) {
Olav Haugan0df59942012-07-05 14:27:30 -07001251 WARN(1, "%s: invalid handle passed to share.\n", __func__);
Laura Abbottb14ed962012-01-30 14:18:08 -08001252 return -EINVAL;
1253 }
1254
1255 buffer = handle->buffer;
1256 ion_buffer_get(buffer);
1257 dmabuf = dma_buf_export(buffer, &dma_buf_ops, buffer->size, O_RDWR);
1258 if (IS_ERR(dmabuf)) {
1259 ion_buffer_put(buffer);
1260 return PTR_ERR(dmabuf);
1261 }
1262 fd = dma_buf_fd(dmabuf, O_CLOEXEC);
Ajay Dudani173f6132012-08-01 18:06:18 -07001263 if (fd < 0)
Laura Abbottb14ed962012-01-30 14:18:08 -08001264 dma_buf_put(dmabuf);
Ajay Dudani173f6132012-08-01 18:06:18 -07001265
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001266 return fd;
Laura Abbottb14ed962012-01-30 14:18:08 -08001267}
Olav Hauganbd453a92012-07-05 14:21:34 -07001268EXPORT_SYMBOL(ion_share_dma_buf);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001269
Laura Abbottb14ed962012-01-30 14:18:08 -08001270struct ion_handle *ion_import_dma_buf(struct ion_client *client, int fd)
1271{
1272 struct dma_buf *dmabuf;
1273 struct ion_buffer *buffer;
1274 struct ion_handle *handle;
1275
1276 dmabuf = dma_buf_get(fd);
1277 if (IS_ERR_OR_NULL(dmabuf))
1278 return ERR_PTR(PTR_ERR(dmabuf));
1279 /* if this memory came from ion */
1280
1281 if (dmabuf->ops != &dma_buf_ops) {
1282 pr_err("%s: can not import dmabuf from another exporter\n",
1283 __func__);
1284 dma_buf_put(dmabuf);
1285 return ERR_PTR(-EINVAL);
1286 }
1287 buffer = dmabuf->priv;
1288
1289 mutex_lock(&client->lock);
1290 /* if a handle exists for this buffer just take a reference to it */
1291 handle = ion_handle_lookup(client, buffer);
1292 if (!IS_ERR_OR_NULL(handle)) {
1293 ion_handle_get(handle);
1294 goto end;
1295 }
1296 handle = ion_handle_create(client, buffer);
1297 if (IS_ERR_OR_NULL(handle))
1298 goto end;
1299 ion_handle_add(client, handle);
1300end:
1301 mutex_unlock(&client->lock);
1302 dma_buf_put(dmabuf);
1303 return handle;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001304}
Olav Hauganbd453a92012-07-05 14:21:34 -07001305EXPORT_SYMBOL(ion_import_dma_buf);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001306
1307static long ion_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1308{
1309 struct ion_client *client = filp->private_data;
1310
1311 switch (cmd) {
1312 case ION_IOC_ALLOC:
1313 {
1314 struct ion_allocation_data data;
1315
1316 if (copy_from_user(&data, (void __user *)arg, sizeof(data)))
1317 return -EFAULT;
1318 data.handle = ion_alloc(client, data.len, data.align,
Hanumant Singh2ac41c92012-08-29 18:39:44 -07001319 data.heap_mask, data.flags);
KyongHo Cho9ae7e012011-09-07 11:27:07 +09001320
Laura Abbottb14ed962012-01-30 14:18:08 -08001321 if (IS_ERR(data.handle))
1322 return PTR_ERR(data.handle);
KyongHo Cho9ae7e012011-09-07 11:27:07 +09001323
Laura Abbottb14ed962012-01-30 14:18:08 -08001324 if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
1325 ion_free(client, data.handle);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001326 return -EFAULT;
Laura Abbottb14ed962012-01-30 14:18:08 -08001327 }
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001328 break;
1329 }
1330 case ION_IOC_FREE:
1331 {
1332 struct ion_handle_data data;
1333 bool valid;
1334
1335 if (copy_from_user(&data, (void __user *)arg,
1336 sizeof(struct ion_handle_data)))
1337 return -EFAULT;
1338 mutex_lock(&client->lock);
1339 valid = ion_handle_validate(client, data.handle);
1340 mutex_unlock(&client->lock);
1341 if (!valid)
1342 return -EINVAL;
1343 ion_free(client, data.handle);
1344 break;
1345 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001346 case ION_IOC_MAP:
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001347 case ION_IOC_SHARE:
1348 {
1349 struct ion_fd_data data;
Laura Abbottb14ed962012-01-30 14:18:08 -08001350 int ret;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001351 if (copy_from_user(&data, (void __user *)arg, sizeof(data)))
1352 return -EFAULT;
Laura Abbottb14ed962012-01-30 14:18:08 -08001353
1354 ret = ion_share_set_flags(client, data.handle, filp->f_flags);
1355 if (ret)
1356 return ret;
1357
1358 data.fd = ion_share_dma_buf(client, data.handle);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001359 if (copy_to_user((void __user *)arg, &data, sizeof(data)))
1360 return -EFAULT;
Olav Hauganc2d2cf52012-05-15 14:40:11 -07001361 if (data.fd < 0)
1362 return data.fd;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001363 break;
1364 }
1365 case ION_IOC_IMPORT:
1366 {
1367 struct ion_fd_data data;
Olav Hauganc2d2cf52012-05-15 14:40:11 -07001368 int ret = 0;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001369 if (copy_from_user(&data, (void __user *)arg,
1370 sizeof(struct ion_fd_data)))
1371 return -EFAULT;
Laura Abbottb14ed962012-01-30 14:18:08 -08001372 data.handle = ion_import_dma_buf(client, data.fd);
Olav Haugan21ceb8a2012-05-15 14:40:11 -07001373 if (IS_ERR(data.handle)) {
1374 ret = PTR_ERR(data.handle);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001375 data.handle = NULL;
Olav Haugan21ceb8a2012-05-15 14:40:11 -07001376 }
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001377 if (copy_to_user((void __user *)arg, &data,
1378 sizeof(struct ion_fd_data)))
1379 return -EFAULT;
Olav Hauganc2d2cf52012-05-15 14:40:11 -07001380 if (ret < 0)
1381 return ret;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001382 break;
1383 }
1384 case ION_IOC_CUSTOM:
1385 {
1386 struct ion_device *dev = client->dev;
1387 struct ion_custom_data data;
1388
1389 if (!dev->custom_ioctl)
1390 return -ENOTTY;
1391 if (copy_from_user(&data, (void __user *)arg,
1392 sizeof(struct ion_custom_data)))
1393 return -EFAULT;
1394 return dev->custom_ioctl(client, data.cmd, data.arg);
1395 }
Laura Abbottabcb6f72011-10-04 16:26:49 -07001396 case ION_IOC_CLEAN_CACHES:
Mitchel Humpherysfd02cfb2012-09-04 17:00:29 -07001397 return client->dev->custom_ioctl(client,
1398 ION_IOC_CLEAN_CACHES, arg);
Laura Abbottabcb6f72011-10-04 16:26:49 -07001399 case ION_IOC_INV_CACHES:
Mitchel Humpherysfd02cfb2012-09-04 17:00:29 -07001400 return client->dev->custom_ioctl(client,
1401 ION_IOC_INV_CACHES, arg);
Laura Abbottabcb6f72011-10-04 16:26:49 -07001402 case ION_IOC_CLEAN_INV_CACHES:
Mitchel Humpherysfd02cfb2012-09-04 17:00:29 -07001403 return client->dev->custom_ioctl(client,
1404 ION_IOC_CLEAN_INV_CACHES, arg);
Laura Abbott273dd8e2011-10-12 14:26:33 -07001405 case ION_IOC_GET_FLAGS:
Mitchel Humpherysfd02cfb2012-09-04 17:00:29 -07001406 return client->dev->custom_ioctl(client,
1407 ION_IOC_GET_FLAGS, arg);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001408 default:
1409 return -ENOTTY;
1410 }
1411 return 0;
1412}
1413
1414static int ion_release(struct inode *inode, struct file *file)
1415{
1416 struct ion_client *client = file->private_data;
1417
1418 pr_debug("%s: %d\n", __func__, __LINE__);
Laura Abbottb14ed962012-01-30 14:18:08 -08001419 ion_client_destroy(client);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001420 return 0;
1421}
1422
1423static int ion_open(struct inode *inode, struct file *file)
1424{
1425 struct miscdevice *miscdev = file->private_data;
1426 struct ion_device *dev = container_of(miscdev, struct ion_device, dev);
1427 struct ion_client *client;
Laura Abbotteed86032011-12-05 15:32:36 -08001428 char debug_name[64];
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001429
1430 pr_debug("%s: %d\n", __func__, __LINE__);
Laura Abbotteed86032011-12-05 15:32:36 -08001431 snprintf(debug_name, 64, "%u", task_pid_nr(current->group_leader));
1432 client = ion_client_create(dev, -1, debug_name);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001433 if (IS_ERR_OR_NULL(client))
1434 return PTR_ERR(client);
1435 file->private_data = client;
1436
1437 return 0;
1438}
1439
1440static const struct file_operations ion_fops = {
1441 .owner = THIS_MODULE,
1442 .open = ion_open,
1443 .release = ion_release,
1444 .unlocked_ioctl = ion_ioctl,
1445};
1446
1447static size_t ion_debug_heap_total(struct ion_client *client,
Laura Abbott3647ac32011-10-31 14:09:53 -07001448 enum ion_heap_ids id)
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001449{
1450 size_t size = 0;
1451 struct rb_node *n;
1452
1453 mutex_lock(&client->lock);
1454 for (n = rb_first(&client->handles); n; n = rb_next(n)) {
1455 struct ion_handle *handle = rb_entry(n,
1456 struct ion_handle,
1457 node);
Laura Abbott3647ac32011-10-31 14:09:53 -07001458 if (handle->buffer->heap->id == id)
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001459 size += handle->buffer->size;
1460 }
1461 mutex_unlock(&client->lock);
1462 return size;
1463}
1464
Olav Haugan0671b9a2012-05-25 11:58:56 -07001465/**
1466 * Searches through a clients handles to find if the buffer is owned
1467 * by this client. Used for debug output.
1468 * @param client pointer to candidate owner of buffer
1469 * @param buf pointer to buffer that we are trying to find the owner of
1470 * @return 1 if found, 0 otherwise
1471 */
1472static int ion_debug_find_buffer_owner(const struct ion_client *client,
1473 const struct ion_buffer *buf)
1474{
1475 struct rb_node *n;
1476
1477 for (n = rb_first(&client->handles); n; n = rb_next(n)) {
1478 const struct ion_handle *handle = rb_entry(n,
1479 const struct ion_handle,
1480 node);
1481 if (handle->buffer == buf)
1482 return 1;
1483 }
1484 return 0;
1485}
1486
1487/**
1488 * Adds mem_map_data pointer to the tree of mem_map
1489 * Used for debug output.
1490 * @param mem_map The mem_map tree
1491 * @param data The new data to add to the tree
1492 */
1493static void ion_debug_mem_map_add(struct rb_root *mem_map,
1494 struct mem_map_data *data)
1495{
1496 struct rb_node **p = &mem_map->rb_node;
1497 struct rb_node *parent = NULL;
1498 struct mem_map_data *entry;
1499
1500 while (*p) {
1501 parent = *p;
1502 entry = rb_entry(parent, struct mem_map_data, node);
1503
1504 if (data->addr < entry->addr) {
1505 p = &(*p)->rb_left;
1506 } else if (data->addr > entry->addr) {
1507 p = &(*p)->rb_right;
1508 } else {
1509 pr_err("%s: mem_map_data already found.", __func__);
1510 BUG();
1511 }
1512 }
1513 rb_link_node(&data->node, parent, p);
1514 rb_insert_color(&data->node, mem_map);
1515}
1516
1517/**
1518 * Search for an owner of a buffer by iterating over all ION clients.
1519 * @param dev ion device containing pointers to all the clients.
1520 * @param buffer pointer to buffer we are trying to find the owner of.
1521 * @return name of owner.
1522 */
1523const char *ion_debug_locate_owner(const struct ion_device *dev,
1524 const struct ion_buffer *buffer)
1525{
1526 struct rb_node *j;
1527 const char *client_name = NULL;
1528
Laura Abbottb14ed962012-01-30 14:18:08 -08001529 for (j = rb_first(&dev->clients); j && !client_name;
Olav Haugan0671b9a2012-05-25 11:58:56 -07001530 j = rb_next(j)) {
1531 struct ion_client *client = rb_entry(j, struct ion_client,
1532 node);
1533 if (ion_debug_find_buffer_owner(client, buffer))
1534 client_name = client->name;
1535 }
1536 return client_name;
1537}
1538
1539/**
1540 * Create a mem_map of the heap.
1541 * @param s seq_file to log error message to.
1542 * @param heap The heap to create mem_map for.
1543 * @param mem_map The mem map to be created.
1544 */
1545void ion_debug_mem_map_create(struct seq_file *s, struct ion_heap *heap,
1546 struct rb_root *mem_map)
1547{
1548 struct ion_device *dev = heap->dev;
1549 struct rb_node *n;
1550
1551 for (n = rb_first(&dev->buffers); n; n = rb_next(n)) {
1552 struct ion_buffer *buffer =
1553 rb_entry(n, struct ion_buffer, node);
1554 if (buffer->heap->id == heap->id) {
1555 struct mem_map_data *data =
1556 kzalloc(sizeof(*data), GFP_KERNEL);
1557 if (!data) {
1558 seq_printf(s, "ERROR: out of memory. "
1559 "Part of memory map will not be logged\n");
1560 break;
1561 }
1562 data->addr = buffer->priv_phys;
1563 data->addr_end = buffer->priv_phys + buffer->size-1;
1564 data->size = buffer->size;
1565 data->client_name = ion_debug_locate_owner(dev, buffer);
1566 ion_debug_mem_map_add(mem_map, data);
1567 }
1568 }
1569}
1570
1571/**
1572 * Free the memory allocated by ion_debug_mem_map_create
1573 * @param mem_map The mem map to free.
1574 */
1575static void ion_debug_mem_map_destroy(struct rb_root *mem_map)
1576{
1577 if (mem_map) {
1578 struct rb_node *n;
1579 while ((n = rb_first(mem_map)) != 0) {
1580 struct mem_map_data *data =
1581 rb_entry(n, struct mem_map_data, node);
1582 rb_erase(&data->node, mem_map);
1583 kfree(data);
1584 }
1585 }
1586}
1587
1588/**
1589 * Print heap debug information.
1590 * @param s seq_file to log message to.
1591 * @param heap pointer to heap that we will print debug information for.
1592 */
1593static void ion_heap_print_debug(struct seq_file *s, struct ion_heap *heap)
1594{
1595 if (heap->ops->print_debug) {
1596 struct rb_root mem_map = RB_ROOT;
1597 ion_debug_mem_map_create(s, heap, &mem_map);
1598 heap->ops->print_debug(heap, s, &mem_map);
1599 ion_debug_mem_map_destroy(&mem_map);
1600 }
1601}
1602
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001603static int ion_debug_heap_show(struct seq_file *s, void *unused)
1604{
1605 struct ion_heap *heap = s->private;
1606 struct ion_device *dev = heap->dev;
1607 struct rb_node *n;
1608
Olav Haugane4900b52012-05-25 11:58:03 -07001609 mutex_lock(&dev->lock);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001610 seq_printf(s, "%16.s %16.s %16.s\n", "client", "pid", "size");
Rebecca Schultz Zavin043a6142012-02-01 11:09:46 -08001611
Laura Abbottb14ed962012-01-30 14:18:08 -08001612 for (n = rb_first(&dev->clients); n; n = rb_next(n)) {
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001613 struct ion_client *client = rb_entry(n, struct ion_client,
1614 node);
Laura Abbott3647ac32011-10-31 14:09:53 -07001615 size_t size = ion_debug_heap_total(client, heap->id);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001616 if (!size)
1617 continue;
Laura Abbottb14ed962012-01-30 14:18:08 -08001618 if (client->task) {
1619 char task_comm[TASK_COMM_LEN];
1620
1621 get_task_comm(task_comm, client->task);
1622 seq_printf(s, "%16.s %16u %16u\n", task_comm,
1623 client->pid, size);
1624 } else {
1625 seq_printf(s, "%16.s %16u %16u\n", client->name,
1626 client->pid, size);
1627 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001628 }
Olav Haugan0671b9a2012-05-25 11:58:56 -07001629 ion_heap_print_debug(s, heap);
Olav Haugane4900b52012-05-25 11:58:03 -07001630 mutex_unlock(&dev->lock);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001631 return 0;
1632}
1633
1634static int ion_debug_heap_open(struct inode *inode, struct file *file)
1635{
1636 return single_open(file, ion_debug_heap_show, inode->i_private);
1637}
1638
1639static const struct file_operations debug_heap_fops = {
1640 .open = ion_debug_heap_open,
1641 .read = seq_read,
1642 .llseek = seq_lseek,
1643 .release = single_release,
1644};
1645
1646void ion_device_add_heap(struct ion_device *dev, struct ion_heap *heap)
1647{
1648 struct rb_node **p = &dev->heaps.rb_node;
1649 struct rb_node *parent = NULL;
1650 struct ion_heap *entry;
1651
Laura Abbottb14ed962012-01-30 14:18:08 -08001652 if (!heap->ops->allocate || !heap->ops->free || !heap->ops->map_dma ||
1653 !heap->ops->unmap_dma)
1654 pr_err("%s: can not add heap with invalid ops struct.\n",
1655 __func__);
1656
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001657 heap->dev = dev;
1658 mutex_lock(&dev->lock);
1659 while (*p) {
1660 parent = *p;
1661 entry = rb_entry(parent, struct ion_heap, node);
1662
1663 if (heap->id < entry->id) {
1664 p = &(*p)->rb_left;
1665 } else if (heap->id > entry->id ) {
1666 p = &(*p)->rb_right;
1667 } else {
1668 pr_err("%s: can not insert multiple heaps with "
1669 "id %d\n", __func__, heap->id);
1670 goto end;
1671 }
1672 }
1673
1674 rb_link_node(&heap->node, parent, p);
1675 rb_insert_color(&heap->node, &dev->heaps);
1676 debugfs_create_file(heap->name, 0664, dev->debug_root, heap,
1677 &debug_heap_fops);
1678end:
1679 mutex_unlock(&dev->lock);
1680}
1681
Laura Abbott7e446482012-06-13 15:59:39 -07001682int ion_secure_heap(struct ion_device *dev, int heap_id, int version,
1683 void *data)
Olav Haugan0a852512012-01-09 10:20:55 -08001684{
1685 struct rb_node *n;
1686 int ret_val = 0;
1687
1688 /*
1689 * traverse the list of heaps available in this system
1690 * and find the heap that is specified.
1691 */
1692 mutex_lock(&dev->lock);
1693 for (n = rb_first(&dev->heaps); n != NULL; n = rb_next(n)) {
1694 struct ion_heap *heap = rb_entry(n, struct ion_heap, node);
1695 if (heap->type != ION_HEAP_TYPE_CP)
1696 continue;
1697 if (ION_HEAP(heap->id) != heap_id)
1698 continue;
1699 if (heap->ops->secure_heap)
Laura Abbott7e446482012-06-13 15:59:39 -07001700 ret_val = heap->ops->secure_heap(heap, version, data);
Olav Haugan0a852512012-01-09 10:20:55 -08001701 else
1702 ret_val = -EINVAL;
1703 break;
1704 }
1705 mutex_unlock(&dev->lock);
1706 return ret_val;
1707}
Olav Hauganbd453a92012-07-05 14:21:34 -07001708EXPORT_SYMBOL(ion_secure_heap);
Olav Haugan0a852512012-01-09 10:20:55 -08001709
Laura Abbott7e446482012-06-13 15:59:39 -07001710int ion_unsecure_heap(struct ion_device *dev, int heap_id, int version,
1711 void *data)
Olav Haugan0a852512012-01-09 10:20:55 -08001712{
1713 struct rb_node *n;
1714 int ret_val = 0;
1715
1716 /*
1717 * traverse the list of heaps available in this system
1718 * and find the heap that is specified.
1719 */
1720 mutex_lock(&dev->lock);
1721 for (n = rb_first(&dev->heaps); n != NULL; n = rb_next(n)) {
1722 struct ion_heap *heap = rb_entry(n, struct ion_heap, node);
1723 if (heap->type != ION_HEAP_TYPE_CP)
1724 continue;
1725 if (ION_HEAP(heap->id) != heap_id)
1726 continue;
1727 if (heap->ops->secure_heap)
Laura Abbott7e446482012-06-13 15:59:39 -07001728 ret_val = heap->ops->unsecure_heap(heap, version, data);
Olav Haugan0a852512012-01-09 10:20:55 -08001729 else
1730 ret_val = -EINVAL;
1731 break;
1732 }
1733 mutex_unlock(&dev->lock);
1734 return ret_val;
1735}
Olav Hauganbd453a92012-07-05 14:21:34 -07001736EXPORT_SYMBOL(ion_unsecure_heap);
Olav Haugan0a852512012-01-09 10:20:55 -08001737
Laura Abbott404f8242011-10-31 14:22:53 -07001738static int ion_debug_leak_show(struct seq_file *s, void *unused)
1739{
1740 struct ion_device *dev = s->private;
1741 struct rb_node *n;
1742 struct rb_node *n2;
1743
1744 /* mark all buffers as 1 */
1745 seq_printf(s, "%16.s %16.s %16.s %16.s\n", "buffer", "heap", "size",
1746 "ref cnt");
1747 mutex_lock(&dev->lock);
1748 for (n = rb_first(&dev->buffers); n; n = rb_next(n)) {
1749 struct ion_buffer *buf = rb_entry(n, struct ion_buffer,
1750 node);
1751
1752 buf->marked = 1;
1753 }
1754
1755 /* now see which buffers we can access */
Laura Abbottb14ed962012-01-30 14:18:08 -08001756 for (n = rb_first(&dev->clients); n; n = rb_next(n)) {
Laura Abbott404f8242011-10-31 14:22:53 -07001757 struct ion_client *client = rb_entry(n, struct ion_client,
1758 node);
1759
1760 mutex_lock(&client->lock);
1761 for (n2 = rb_first(&client->handles); n2; n2 = rb_next(n2)) {
1762 struct ion_handle *handle = rb_entry(n2,
1763 struct ion_handle, node);
1764
1765 handle->buffer->marked = 0;
1766
1767 }
1768 mutex_unlock(&client->lock);
1769
1770 }
1771
Laura Abbott404f8242011-10-31 14:22:53 -07001772 /* And anyone still marked as a 1 means a leaked handle somewhere */
1773 for (n = rb_first(&dev->buffers); n; n = rb_next(n)) {
1774 struct ion_buffer *buf = rb_entry(n, struct ion_buffer,
1775 node);
1776
1777 if (buf->marked == 1)
1778 seq_printf(s, "%16.x %16.s %16.x %16.d\n",
1779 (int)buf, buf->heap->name, buf->size,
1780 atomic_read(&buf->ref.refcount));
1781 }
1782 mutex_unlock(&dev->lock);
1783 return 0;
1784}
1785
1786static int ion_debug_leak_open(struct inode *inode, struct file *file)
1787{
1788 return single_open(file, ion_debug_leak_show, inode->i_private);
1789}
1790
1791static const struct file_operations debug_leak_fops = {
1792 .open = ion_debug_leak_open,
1793 .read = seq_read,
1794 .llseek = seq_lseek,
1795 .release = single_release,
1796};
1797
1798
1799
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001800struct ion_device *ion_device_create(long (*custom_ioctl)
1801 (struct ion_client *client,
1802 unsigned int cmd,
1803 unsigned long arg))
1804{
1805 struct ion_device *idev;
1806 int ret;
1807
1808 idev = kzalloc(sizeof(struct ion_device), GFP_KERNEL);
1809 if (!idev)
1810 return ERR_PTR(-ENOMEM);
1811
1812 idev->dev.minor = MISC_DYNAMIC_MINOR;
1813 idev->dev.name = "ion";
1814 idev->dev.fops = &ion_fops;
1815 idev->dev.parent = NULL;
1816 ret = misc_register(&idev->dev);
1817 if (ret) {
1818 pr_err("ion: failed to register misc device.\n");
1819 return ERR_PTR(ret);
1820 }
1821
1822 idev->debug_root = debugfs_create_dir("ion", NULL);
1823 if (IS_ERR_OR_NULL(idev->debug_root))
1824 pr_err("ion: failed to create debug files.\n");
1825
1826 idev->custom_ioctl = custom_ioctl;
1827 idev->buffers = RB_ROOT;
1828 mutex_init(&idev->lock);
1829 idev->heaps = RB_ROOT;
Laura Abbottb14ed962012-01-30 14:18:08 -08001830 idev->clients = RB_ROOT;
Laura Abbott404f8242011-10-31 14:22:53 -07001831 debugfs_create_file("check_leaked_fds", 0664, idev->debug_root, idev,
1832 &debug_leak_fops);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001833 return idev;
1834}
1835
1836void ion_device_destroy(struct ion_device *dev)
1837{
1838 misc_deregister(&dev->dev);
1839 /* XXX need to free the heaps and clients ? */
1840 kfree(dev);
1841}
Laura Abbottb14ed962012-01-30 14:18:08 -08001842
1843void __init ion_reserve(struct ion_platform_data *data)
1844{
1845 int i, ret;
1846
1847 for (i = 0; i < data->nr; i++) {
1848 if (data->heaps[i].size == 0)
1849 continue;
1850 ret = memblock_reserve(data->heaps[i].base,
1851 data->heaps[i].size);
1852 if (ret)
1853 pr_err("memblock reserve of %x@%lx failed\n",
1854 data->heaps[i].size,
1855 data->heaps[i].base);
1856 }
1857}