3 * drivers/gpu/ion/ion.c
5 * Copyright (C) 2011 Google, Inc.
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.
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.
18 #include <linux/device.h>
19 #include <linux/file.h>
20 #include <linux/freezer.h>
22 #include <linux/anon_inodes.h>
23 #include <linux/kthread.h>
24 #include <linux/list.h>
25 #include <linux/memblock.h>
26 #include <linux/miscdevice.h>
27 #include <linux/export.h>
29 #include <linux/mm_types.h>
30 #include <linux/rbtree.h>
31 #include <linux/slab.h>
32 #include <linux/seq_file.h>
33 #include <linux/uaccess.h>
34 #include <linux/vmalloc.h>
35 #include <linux/debugfs.h>
36 #include <linux/dma-buf.h>
37 #include <linux/idr.h>
38 #include <linux/rockchip_ion.h>
39 #include <linux/dma-contiguous.h>
43 #include "compat_ion.h"
45 #define CREATE_TRACE_POINTS
46 #include "../trace/ion.h"
49 * struct ion_device - the metadata of the ion device node
50 * @dev: the actual misc device
51 * @buffers: an rb tree of all the existing buffers
52 * @buffer_lock: lock protecting the tree of buffers
53 * @lock: rwsem protecting the tree of heaps and clients
54 * @heaps: list of all the heaps in the system
55 * @user_clients: list of all the clients created from userspace
58 struct miscdevice dev;
59 struct rb_root buffers;
60 struct mutex buffer_lock;
61 struct rw_semaphore lock;
62 struct plist_head heaps;
63 long (*custom_ioctl) (struct ion_client *client, unsigned int cmd,
65 struct rb_root clients;
66 struct dentry *debug_root;
67 struct dentry *heaps_debug_root;
68 struct dentry *clients_debug_root;
72 * struct ion_client - a process/hw block local address space
73 * @node: node in the tree of all clients
74 * @dev: backpointer to ion device
75 * @handles: an rb tree of all the handles in this client
76 * @idr: an idr space for allocating handle ids
77 * @lock: lock protecting the tree of handles
78 * @name: used for debugging
79 * @display_name: used for debugging (unique version of @name)
80 * @display_serial: used for debugging (to make display_name unique)
81 * @task: used for debugging
83 * A client represents a list of buffers this client may access.
84 * The mutex stored here is used to protect both handles tree
85 * as well as the handles themselves, and should be held while modifying either.
89 struct ion_device *dev;
90 struct rb_root handles;
96 struct task_struct *task;
98 struct dentry *debug_root;
102 * ion_handle - a client local reference to a buffer
103 * @ref: reference count
104 * @client: back pointer to the client the buffer resides in
105 * @buffer: pointer to the buffer
106 * @node: node in the client's handle rbtree
107 * @kmap_cnt: count of times this client has mapped to kernel
108 * @id: client-unique id allocated by client->idr
110 * Modifications to node, map_cnt or mapping should be protected by the
111 * lock in the client. Other fields are never changed after initialization.
115 struct ion_client *client;
116 struct ion_buffer *buffer;
118 unsigned int kmap_cnt;
122 #ifdef CONFIG_ROCKCHIP_IOMMU
123 static void ion_iommu_force_unmap(struct ion_buffer *buffer);
125 #ifdef CONFIG_ION_ROCKCHIP_SNAPSHOT
126 extern char *rockchip_ion_snapshot_get(unsigned *size);
127 extern int rockchip_ion_snapshot_debugfs(struct dentry* root);
128 static int ion_snapshot_save(struct ion_device *idev, size_t len);
131 bool ion_buffer_fault_user_mappings(struct ion_buffer *buffer)
133 return (buffer->flags & ION_FLAG_CACHED) &&
134 !(buffer->flags & ION_FLAG_CACHED_NEEDS_SYNC);
137 bool ion_buffer_cached(struct ion_buffer *buffer)
139 return !!(buffer->flags & ION_FLAG_CACHED);
142 static inline struct page *ion_buffer_page(struct page *page)
144 return (struct page *)((unsigned long)page & ~(1UL));
147 static inline bool ion_buffer_page_is_dirty(struct page *page)
149 return !!((unsigned long)page & 1UL);
152 static inline void ion_buffer_page_dirty(struct page **page)
154 *page = (struct page *)((unsigned long)(*page) | 1UL);
157 static inline void ion_buffer_page_clean(struct page **page)
159 *page = (struct page *)((unsigned long)(*page) & ~(1UL));
162 /* this function should only be called while dev->lock is held */
163 static void ion_buffer_add(struct ion_device *dev,
164 struct ion_buffer *buffer)
166 struct rb_node **p = &dev->buffers.rb_node;
167 struct rb_node *parent = NULL;
168 struct ion_buffer *entry;
172 entry = rb_entry(parent, struct ion_buffer, node);
174 if (buffer < entry) {
176 } else if (buffer > entry) {
179 pr_err("%s: buffer already found.", __func__);
184 rb_link_node(&buffer->node, parent, p);
185 rb_insert_color(&buffer->node, &dev->buffers);
188 /* this function should only be called while dev->lock is held */
189 static struct ion_buffer *ion_buffer_create(struct ion_heap *heap,
190 struct ion_device *dev,
195 struct ion_buffer *buffer;
196 struct sg_table *table;
197 struct scatterlist *sg;
200 buffer = kzalloc(sizeof(struct ion_buffer), GFP_KERNEL);
202 return ERR_PTR(-ENOMEM);
205 buffer->flags = flags;
206 kref_init(&buffer->ref);
208 ret = heap->ops->allocate(heap, buffer, len, align, flags);
211 if (!(heap->flags & ION_HEAP_FLAG_DEFER_FREE))
214 ion_heap_freelist_drain(heap, 0);
215 ret = heap->ops->allocate(heap, buffer, len, align,
224 table = heap->ops->map_dma(heap, buffer);
225 if (WARN_ONCE(table == NULL,
226 "heap->ops->map_dma should return ERR_PTR on error"))
227 table = ERR_PTR(-EINVAL);
229 heap->ops->free(buffer);
231 return ERR_PTR(PTR_ERR(table));
233 buffer->sg_table = table;
234 if (ion_buffer_fault_user_mappings(buffer)) {
235 int num_pages = PAGE_ALIGN(buffer->size) / PAGE_SIZE;
236 struct scatterlist *sg;
239 buffer->pages = vmalloc(sizeof(struct page *) * num_pages);
240 if (!buffer->pages) {
245 for_each_sg(table->sgl, sg, table->nents, i) {
246 struct page *page = sg_page(sg);
248 for (j = 0; j < sg->length / PAGE_SIZE; j++)
249 buffer->pages[k++] = page++;
258 INIT_LIST_HEAD(&buffer->vmas);
259 mutex_init(&buffer->lock);
260 /* this will set up dma addresses for the sglist -- it is not
261 technically correct as per the dma api -- a specific
262 device isn't really taking ownership here. However, in practice on
263 our systems the only dma_address space is physical addresses.
264 Additionally, we can't afford the overhead of invalidating every
265 allocation via dma_map_sg. The implicit contract here is that
266 memory comming from the heaps is ready for dma, ie if it has a
267 cached mapping that mapping has been invalidated */
268 for_each_sg(buffer->sg_table->sgl, sg, buffer->sg_table->nents, i)
269 sg_dma_address(sg) = sg_phys(sg);
270 mutex_lock(&dev->buffer_lock);
271 ion_buffer_add(dev, buffer);
272 mutex_unlock(&dev->buffer_lock);
276 heap->ops->unmap_dma(heap, buffer);
277 heap->ops->free(buffer);
280 vfree(buffer->pages);
286 void ion_buffer_destroy(struct ion_buffer *buffer)
288 trace_ion_buffer_destroy("", (unsigned int)buffer, buffer->size);
290 if (WARN_ON(buffer->kmap_cnt > 0))
291 buffer->heap->ops->unmap_kernel(buffer->heap, buffer);
292 buffer->heap->ops->unmap_dma(buffer->heap, buffer);
293 #ifdef CONFIG_ROCKCHIP_IOMMU
294 ion_iommu_force_unmap(buffer);
296 buffer->heap->ops->free(buffer);
298 vfree(buffer->pages);
302 static void _ion_buffer_destroy(struct kref *kref)
304 struct ion_buffer *buffer = container_of(kref, struct ion_buffer, ref);
305 struct ion_heap *heap = buffer->heap;
306 struct ion_device *dev = buffer->dev;
308 mutex_lock(&dev->buffer_lock);
309 rb_erase(&buffer->node, &dev->buffers);
310 mutex_unlock(&dev->buffer_lock);
312 if (heap->flags & ION_HEAP_FLAG_DEFER_FREE)
313 ion_heap_freelist_add(heap, buffer);
315 ion_buffer_destroy(buffer);
318 static void ion_buffer_get(struct ion_buffer *buffer)
320 kref_get(&buffer->ref);
323 static int ion_buffer_put(struct ion_buffer *buffer)
325 return kref_put(&buffer->ref, _ion_buffer_destroy);
328 static void ion_buffer_add_to_handle(struct ion_buffer *buffer)
330 mutex_lock(&buffer->lock);
331 buffer->handle_count++;
332 mutex_unlock(&buffer->lock);
335 static void ion_buffer_remove_from_handle(struct ion_buffer *buffer)
338 * when a buffer is removed from a handle, if it is not in
339 * any other handles, copy the taskcomm and the pid of the
340 * process it's being removed from into the buffer. At this
341 * point there will be no way to track what processes this buffer is
342 * being used by, it only exists as a dma_buf file descriptor.
343 * The taskcomm and pid can provide a debug hint as to where this fd
346 mutex_lock(&buffer->lock);
347 buffer->handle_count--;
348 BUG_ON(buffer->handle_count < 0);
349 if (!buffer->handle_count) {
350 struct task_struct *task;
352 task = current->group_leader;
353 get_task_comm(buffer->task_comm, task);
354 buffer->pid = task_pid_nr(task);
356 mutex_unlock(&buffer->lock);
359 static struct ion_handle *ion_handle_create(struct ion_client *client,
360 struct ion_buffer *buffer)
362 struct ion_handle *handle;
364 handle = kzalloc(sizeof(struct ion_handle), GFP_KERNEL);
366 return ERR_PTR(-ENOMEM);
367 kref_init(&handle->ref);
368 RB_CLEAR_NODE(&handle->node);
369 handle->client = client;
370 ion_buffer_get(buffer);
371 ion_buffer_add_to_handle(buffer);
372 handle->buffer = buffer;
377 static void ion_handle_kmap_put(struct ion_handle *);
379 static void ion_handle_destroy(struct kref *kref)
381 struct ion_handle *handle = container_of(kref, struct ion_handle, ref);
382 struct ion_client *client = handle->client;
383 struct ion_buffer *buffer = handle->buffer;
385 mutex_lock(&buffer->lock);
386 while (handle->kmap_cnt)
387 ion_handle_kmap_put(handle);
388 mutex_unlock(&buffer->lock);
390 idr_remove(&client->idr, handle->id);
391 if (!RB_EMPTY_NODE(&handle->node))
392 rb_erase(&handle->node, &client->handles);
394 ion_buffer_remove_from_handle(buffer);
395 ion_buffer_put(buffer);
400 struct ion_buffer *ion_handle_buffer(struct ion_handle *handle)
402 return handle->buffer;
405 static void ion_handle_get(struct ion_handle *handle)
407 kref_get(&handle->ref);
410 int ion_handle_put(struct ion_handle *handle)
412 struct ion_client *client = handle->client;
415 mutex_lock(&client->lock);
416 ret = kref_put(&handle->ref, ion_handle_destroy);
417 mutex_unlock(&client->lock);
422 static struct ion_handle *ion_handle_lookup(struct ion_client *client,
423 struct ion_buffer *buffer)
425 struct rb_node *n = client->handles.rb_node;
428 struct ion_handle *entry = rb_entry(n, struct ion_handle, node);
429 if (buffer < entry->buffer)
431 else if (buffer > entry->buffer)
436 return ERR_PTR(-EINVAL);
439 struct ion_handle *ion_handle_get_by_id(struct ion_client *client,
442 struct ion_handle *handle;
444 mutex_lock(&client->lock);
445 handle = idr_find(&client->idr, id);
447 ion_handle_get(handle);
448 mutex_unlock(&client->lock);
450 return handle ? handle : ERR_PTR(-EINVAL);
453 static bool ion_handle_validate(struct ion_client *client,
454 struct ion_handle *handle)
456 WARN_ON(!mutex_is_locked(&client->lock));
457 return (idr_find(&client->idr, handle->id) == handle);
460 static int ion_handle_add(struct ion_client *client, struct ion_handle *handle)
463 struct rb_node **p = &client->handles.rb_node;
464 struct rb_node *parent = NULL;
465 struct ion_handle *entry;
467 id = idr_alloc(&client->idr, handle, 1, 0, GFP_KERNEL);
475 entry = rb_entry(parent, struct ion_handle, node);
477 if (handle->buffer < entry->buffer)
479 else if (handle->buffer > entry->buffer)
482 WARN(1, "%s: buffer already found.", __func__);
485 rb_link_node(&handle->node, parent, p);
486 rb_insert_color(&handle->node, &client->handles);
491 struct ion_handle *ion_alloc(struct ion_client *client, size_t len,
492 size_t align, unsigned int heap_id_mask,
495 struct ion_handle *handle;
496 struct ion_device *dev = client->dev;
497 struct ion_buffer *buffer = NULL;
498 struct ion_heap *heap;
501 pr_debug("%s: len %zu align %zu heap_id_mask %u flags %x\n", __func__,
502 len, align, heap_id_mask, flags);
504 * traverse the list of heaps available in this system in priority
505 * order. If the heap type is supported by the client, and matches the
506 * request of the caller allocate from it. Repeat until allocate has
507 * succeeded or all heaps have been tried
509 len = PAGE_ALIGN(len);
512 return ERR_PTR(-EINVAL);
514 down_read(&dev->lock);
515 plist_for_each_entry(heap, &dev->heaps, node) {
516 /* if the caller didn't specify this heap id */
517 if (!((1 << heap->id) & heap_id_mask))
519 buffer = ion_buffer_create(heap, dev, len, align, flags);
526 return ERR_PTR(-ENODEV);
528 if (IS_ERR(buffer)) {
529 #ifdef CONFIG_ION_ROCKCHIP_SNAPSHOT
530 ion_snapshot_save(client->dev, len);
532 return ERR_PTR(PTR_ERR(buffer));
535 handle = ion_handle_create(client, buffer);
538 * ion_buffer_create will create a buffer with a ref_cnt of 1,
539 * and ion_handle_create will take a second reference, drop one here
541 ion_buffer_put(buffer);
546 mutex_lock(&client->lock);
547 ret = ion_handle_add(client, handle);
548 mutex_unlock(&client->lock);
550 ion_handle_put(handle);
551 handle = ERR_PTR(ret);
554 trace_ion_buffer_alloc(client->display_name, (unsigned int)buffer,
559 EXPORT_SYMBOL(ion_alloc);
561 void ion_free(struct ion_client *client, struct ion_handle *handle)
565 BUG_ON(client != handle->client);
567 mutex_lock(&client->lock);
568 valid_handle = ion_handle_validate(client, handle);
571 WARN(1, "%s: invalid handle passed to free.\n", __func__);
572 mutex_unlock(&client->lock);
575 mutex_unlock(&client->lock);
576 trace_ion_buffer_free(client->display_name, (unsigned int)handle->buffer,
577 handle->buffer->size);
578 ion_handle_put(handle);
580 EXPORT_SYMBOL(ion_free);
582 int ion_phys(struct ion_client *client, struct ion_handle *handle,
583 ion_phys_addr_t *addr, size_t *len)
585 struct ion_buffer *buffer;
588 mutex_lock(&client->lock);
589 if (!ion_handle_validate(client, handle)) {
590 mutex_unlock(&client->lock);
594 buffer = handle->buffer;
596 if (!buffer->heap->ops->phys) {
597 pr_err("%s: ion_phys is not implemented by this heap.\n",
599 mutex_unlock(&client->lock);
602 mutex_unlock(&client->lock);
603 ret = buffer->heap->ops->phys(buffer->heap, buffer, addr, len);
606 EXPORT_SYMBOL(ion_phys);
608 static void *ion_buffer_kmap_get(struct ion_buffer *buffer)
612 if (buffer->kmap_cnt) {
614 return buffer->vaddr;
616 vaddr = buffer->heap->ops->map_kernel(buffer->heap, buffer);
617 if (WARN_ONCE(vaddr == NULL,
618 "heap->ops->map_kernel should return ERR_PTR on error"))
619 return ERR_PTR(-EINVAL);
622 buffer->vaddr = vaddr;
627 static void *ion_handle_kmap_get(struct ion_handle *handle)
629 struct ion_buffer *buffer = handle->buffer;
632 if (handle->kmap_cnt) {
634 return buffer->vaddr;
636 vaddr = ion_buffer_kmap_get(buffer);
643 static void ion_buffer_kmap_put(struct ion_buffer *buffer)
646 if (!buffer->kmap_cnt) {
647 buffer->heap->ops->unmap_kernel(buffer->heap, buffer);
648 buffer->vaddr = NULL;
652 static void ion_handle_kmap_put(struct ion_handle *handle)
654 struct ion_buffer *buffer = handle->buffer;
657 if (!handle->kmap_cnt)
658 ion_buffer_kmap_put(buffer);
661 void *ion_map_kernel(struct ion_client *client, struct ion_handle *handle)
663 struct ion_buffer *buffer;
666 mutex_lock(&client->lock);
667 if (!ion_handle_validate(client, handle)) {
668 pr_err("%s: invalid handle passed to map_kernel.\n",
670 mutex_unlock(&client->lock);
671 return ERR_PTR(-EINVAL);
674 buffer = handle->buffer;
676 if (!handle->buffer->heap->ops->map_kernel) {
677 pr_err("%s: map_kernel is not implemented by this heap.\n",
679 mutex_unlock(&client->lock);
680 return ERR_PTR(-ENODEV);
683 mutex_lock(&buffer->lock);
684 vaddr = ion_handle_kmap_get(handle);
685 mutex_unlock(&buffer->lock);
686 mutex_unlock(&client->lock);
687 trace_ion_kernel_map(client->display_name, (unsigned int)buffer,
688 buffer->size, (unsigned int)vaddr);
691 EXPORT_SYMBOL(ion_map_kernel);
693 void ion_unmap_kernel(struct ion_client *client, struct ion_handle *handle)
695 struct ion_buffer *buffer;
697 mutex_lock(&client->lock);
698 buffer = handle->buffer;
699 mutex_lock(&buffer->lock);
700 trace_ion_kernel_unmap(client->display_name, (unsigned int)buffer,
702 ion_handle_kmap_put(handle);
703 mutex_unlock(&buffer->lock);
704 mutex_unlock(&client->lock);
706 EXPORT_SYMBOL(ion_unmap_kernel);
708 #ifdef CONFIG_ROCKCHIP_IOMMU
709 static void ion_iommu_add(struct ion_buffer *buffer,
710 struct ion_iommu_map *iommu)
712 struct rb_node **p = &buffer->iommu_maps.rb_node;
713 struct rb_node *parent = NULL;
714 struct ion_iommu_map *entry;
718 entry = rb_entry(parent, struct ion_iommu_map, node);
720 if (iommu->key < entry->key) {
722 } else if (iommu->key > entry->key) {
725 pr_err("%s: buffer %p already has mapping for domainid %x\n",
733 rb_link_node(&iommu->node, parent, p);
734 rb_insert_color(&iommu->node, &buffer->iommu_maps);
737 static struct ion_iommu_map *ion_iommu_lookup(struct ion_buffer *buffer,
740 struct rb_node **p = &buffer->iommu_maps.rb_node;
741 struct rb_node *parent = NULL;
742 struct ion_iommu_map *entry;
746 entry = rb_entry(parent, struct ion_iommu_map, node);
748 if (key < entry->key)
750 else if (key > entry->key)
759 static struct ion_iommu_map *__ion_iommu_map(struct ion_buffer *buffer,
760 struct device *iommu_dev, unsigned long *iova)
762 struct ion_iommu_map *data;
765 data = kmalloc(sizeof(*data), GFP_ATOMIC);
768 return ERR_PTR(-ENOMEM);
770 data->buffer = buffer;
771 data->key = (uint32_t)iommu_dev;
773 ret = buffer->heap->ops->map_iommu(buffer, iommu_dev, data,
774 buffer->size, buffer->flags);
778 kref_init(&data->ref);
779 *iova = data->iova_addr;
781 ion_iommu_add(buffer, data);
790 int ion_map_iommu(struct device *iommu_dev, struct ion_client *client,
791 struct ion_handle *handle, unsigned long *iova, unsigned long *size)
793 struct ion_buffer *buffer;
794 struct ion_iommu_map *iommu_map;
797 mutex_lock(&client->lock);
798 if (!ion_handle_validate(client, handle)) {
799 pr_err("%s: invalid handle passed to map_kernel.\n",
801 mutex_unlock(&client->lock);
805 buffer = handle->buffer;
806 pr_debug("%s: map buffer(%p)\n", __func__, buffer);
808 mutex_lock(&buffer->lock);
810 if (ION_IS_CACHED(buffer->flags)) {
811 pr_err("%s: Cannot map iommu as cached.\n", __func__);
816 if (!handle->buffer->heap->ops->map_iommu) {
817 pr_err("%s: map_iommu is not implemented by this heap.\n",
823 if (buffer->size & ~PAGE_MASK) {
824 pr_debug("%s: buffer size %x is not aligned to %lx", __func__,
825 buffer->size, PAGE_SIZE);
830 iommu_map = ion_iommu_lookup(buffer, (uint32_t)iommu_dev);
832 pr_debug("%s: create new map for buffer(%p)\n", __func__, buffer);
833 iommu_map = __ion_iommu_map(buffer, iommu_dev, iova);
834 if (IS_ERR(iommu_map))
835 ret = PTR_ERR(iommu_map);
837 pr_debug("%s: buffer(%p) already mapped\n", __func__, buffer);
838 if (iommu_map->mapped_size != buffer->size) {
839 pr_err("%s: handle %p is already mapped with length"
840 " %x, trying to map with length %x\n",
841 __func__, handle, iommu_map->mapped_size, buffer->size);
844 kref_get(&iommu_map->ref);
845 *iova = iommu_map->iova_addr;
849 buffer->iommu_map_cnt++;
850 *size = buffer->size;
851 trace_ion_iommu_map(client->display_name, (unsigned int)buffer, buffer->size,
852 dev_name(iommu_dev), *iova, *size, buffer->iommu_map_cnt);
854 mutex_unlock(&buffer->lock);
855 mutex_unlock(&client->lock);
858 EXPORT_SYMBOL(ion_map_iommu);
860 static void ion_iommu_release(struct kref *kref)
862 struct ion_iommu_map *map = container_of(kref, struct ion_iommu_map,
864 struct ion_buffer *buffer = map->buffer;
866 trace_ion_iommu_release("", (unsigned int)buffer, buffer->size,
867 "", map->iova_addr, map->mapped_size, buffer->iommu_map_cnt);
869 rb_erase(&map->node, &buffer->iommu_maps);
870 buffer->heap->ops->unmap_iommu((struct device*)map->key, map);
875 * Unmap any outstanding mappings which would otherwise have been leaked.
877 static void ion_iommu_force_unmap(struct ion_buffer *buffer)
879 struct ion_iommu_map *iommu_map;
880 struct rb_node *node;
881 const struct rb_root *rb = &(buffer->iommu_maps);
883 pr_debug("%s: force unmap buffer(%p)\n", __func__, buffer);
885 mutex_lock(&buffer->lock);
887 while ((node = rb_first(rb)) != 0) {
888 iommu_map = rb_entry(node, struct ion_iommu_map, node);
889 /* set ref count to 1 to force release */
890 kref_init(&iommu_map->ref);
891 kref_put(&iommu_map->ref, ion_iommu_release);
894 mutex_unlock(&buffer->lock);
897 void ion_unmap_iommu(struct device *iommu_dev, struct ion_client *client,
898 struct ion_handle *handle)
900 struct ion_iommu_map *iommu_map;
901 struct ion_buffer *buffer;
903 mutex_lock(&client->lock);
904 buffer = handle->buffer;
905 pr_debug("%s: unmap buffer(%p)\n", __func__, buffer);
907 mutex_lock(&buffer->lock);
909 iommu_map = ion_iommu_lookup(buffer, (uint32_t)iommu_dev);
912 WARN(1, "%s: (%p) was never mapped for %p\n", __func__,
917 kref_put(&iommu_map->ref, ion_iommu_release);
919 buffer->iommu_map_cnt--;
921 trace_ion_iommu_unmap(client->display_name, (unsigned int)buffer, buffer->size,
922 dev_name(iommu_dev), iommu_map->iova_addr,
923 iommu_map->mapped_size, buffer->iommu_map_cnt);
925 mutex_unlock(&buffer->lock);
926 mutex_unlock(&client->lock);
928 EXPORT_SYMBOL(ion_unmap_iommu);
930 static int ion_debug_client_show_buffer_map(struct seq_file *s, struct ion_buffer *buffer)
932 struct ion_iommu_map *iommu_map;
933 const struct rb_root *rb;
934 struct rb_node *node;
936 pr_debug("%s: buffer(%p)\n", __func__, buffer);
938 mutex_lock(&buffer->lock);
939 rb = &(buffer->iommu_maps);
942 while (node != NULL) {
943 iommu_map = rb_entry(node, struct ion_iommu_map, node);
944 seq_printf(s, "%16.16s: 0x%08lx 0x%08x 0x%08x %8zuKB %4d\n",
945 "<iommu>", iommu_map->iova_addr, 0, 0, iommu_map->mapped_size>>10,
946 atomic_read(&iommu_map->ref.refcount));
948 node = rb_next(node);
951 mutex_unlock(&buffer->lock);
956 int ion_map_iommu(struct device *iommu_dev, struct ion_client *client,
957 struct ion_handle *handle, unsigned long *iova, unsigned long *size)
961 void ion_unmap_iommu(struct device *iommu_dev, struct ion_client *client,
962 struct ion_handle *handle)
967 static int ion_debug_client_show_buffer(struct seq_file *s, void *unused)
969 struct ion_client *client = s->private;
972 seq_printf(s, "----------------------------------------------------\n");
973 seq_printf(s, "%16.s: %12.s %12.s %12.s %10.s %4.s %4.s %4.s\n",
974 "heap_name", "VA", "PA", "IBUF", "size", "HC", "IBR", "IHR");
975 mutex_lock(&client->lock);
976 for (n = rb_first(&client->handles); n; n = rb_next(n)) {
977 struct ion_handle *handle = rb_entry(n, struct ion_handle, node);
978 struct ion_buffer *buffer = handle->buffer;
979 ion_phys_addr_t pa = 0;
980 size_t len = buffer->size;
982 mutex_lock(&buffer->lock);
984 if (buffer->heap->ops->phys)
985 buffer->heap->ops->phys(buffer->heap, buffer, &pa, &len);
987 seq_printf(s, "%16.16s: 0x%08lx 0x%08lx 0x%08lx %8zuKB %4d %4d %4d\n",
988 buffer->heap->name, (unsigned long)buffer->vaddr, pa,
989 (unsigned long)buffer, len>>10, buffer->handle_count,
990 atomic_read(&buffer->ref.refcount),
991 atomic_read(&handle->ref.refcount));
993 mutex_unlock(&buffer->lock);
995 #ifdef CONFIG_ROCKCHIP_IOMMU
996 ion_debug_client_show_buffer_map(s, buffer);
999 mutex_unlock(&client->lock);
1004 static int ion_debug_client_show(struct seq_file *s, void *unused)
1006 struct ion_client *client = s->private;
1008 size_t sizes[ION_NUM_HEAP_IDS] = {0};
1009 const char *names[ION_NUM_HEAP_IDS] = {NULL};
1012 mutex_lock(&client->lock);
1013 for (n = rb_first(&client->handles); n; n = rb_next(n)) {
1014 struct ion_handle *handle = rb_entry(n, struct ion_handle,
1016 unsigned int id = handle->buffer->heap->id;
1019 names[id] = handle->buffer->heap->name;
1020 sizes[id] += handle->buffer->size;
1022 mutex_unlock(&client->lock);
1024 seq_printf(s, "%16.16s: %16.16s\n", "heap_name", "size_in_bytes");
1025 for (i = 0; i < ION_NUM_HEAP_IDS; i++) {
1028 seq_printf(s, "%16.16s: %16zu\n", names[i], sizes[i]);
1030 ion_debug_client_show_buffer(s, unused);
1034 static int ion_debug_client_open(struct inode *inode, struct file *file)
1036 return single_open(file, ion_debug_client_show, inode->i_private);
1039 static const struct file_operations debug_client_fops = {
1040 .open = ion_debug_client_open,
1042 .llseek = seq_lseek,
1043 .release = single_release,
1046 static int ion_get_client_serial(const struct rb_root *root,
1047 const unsigned char *name)
1050 struct rb_node *node;
1051 for (node = rb_first(root); node; node = rb_next(node)) {
1052 struct ion_client *client = rb_entry(node, struct ion_client,
1054 if (strcmp(client->name, name))
1056 serial = max(serial, client->display_serial);
1061 struct ion_client *ion_client_create(struct ion_device *dev,
1064 struct ion_client *client;
1065 struct task_struct *task;
1067 struct rb_node *parent = NULL;
1068 struct ion_client *entry;
1072 pr_err("%s: Name cannot be null\n", __func__);
1073 return ERR_PTR(-EINVAL);
1076 get_task_struct(current->group_leader);
1077 task_lock(current->group_leader);
1078 pid = task_pid_nr(current->group_leader);
1079 /* don't bother to store task struct for kernel threads,
1080 they can't be killed anyway */
1081 if (current->group_leader->flags & PF_KTHREAD) {
1082 put_task_struct(current->group_leader);
1085 task = current->group_leader;
1087 task_unlock(current->group_leader);
1089 client = kzalloc(sizeof(struct ion_client), GFP_KERNEL);
1091 goto err_put_task_struct;
1094 client->handles = RB_ROOT;
1095 idr_init(&client->idr);
1096 mutex_init(&client->lock);
1097 client->task = task;
1099 client->name = kstrdup(name, GFP_KERNEL);
1101 goto err_free_client;
1103 down_write(&dev->lock);
1104 client->display_serial = ion_get_client_serial(&dev->clients, name);
1105 client->display_name = kasprintf(
1106 GFP_KERNEL, "%s-%d", name, client->display_serial);
1107 if (!client->display_name) {
1108 up_write(&dev->lock);
1109 goto err_free_client_name;
1111 p = &dev->clients.rb_node;
1114 entry = rb_entry(parent, struct ion_client, node);
1118 else if (client > entry)
1119 p = &(*p)->rb_right;
1121 rb_link_node(&client->node, parent, p);
1122 rb_insert_color(&client->node, &dev->clients);
1124 client->debug_root = debugfs_create_file(client->display_name, 0664,
1125 dev->clients_debug_root,
1126 client, &debug_client_fops);
1127 if (!client->debug_root) {
1128 char buf[256], *path;
1129 path = dentry_path(dev->clients_debug_root, buf, 256);
1130 pr_err("Failed to create client debugfs at %s/%s\n",
1131 path, client->display_name);
1134 trace_ion_client_create(client->display_name);
1136 up_write(&dev->lock);
1140 err_free_client_name:
1141 kfree(client->name);
1144 err_put_task_struct:
1146 put_task_struct(current->group_leader);
1147 return ERR_PTR(-ENOMEM);
1149 EXPORT_SYMBOL(ion_client_create);
1151 void ion_client_destroy(struct ion_client *client)
1153 struct ion_device *dev = client->dev;
1156 pr_debug("%s: %d\n", __func__, __LINE__);
1157 while ((n = rb_first(&client->handles))) {
1158 struct ion_handle *handle = rb_entry(n, struct ion_handle,
1160 ion_handle_destroy(&handle->ref);
1163 idr_destroy(&client->idr);
1165 down_write(&dev->lock);
1167 put_task_struct(client->task);
1168 rb_erase(&client->node, &dev->clients);
1169 debugfs_remove_recursive(client->debug_root);
1170 up_write(&dev->lock);
1172 trace_ion_client_destroy(client->display_name);
1174 kfree(client->display_name);
1175 kfree(client->name);
1178 EXPORT_SYMBOL(ion_client_destroy);
1180 struct sg_table *ion_sg_table(struct ion_client *client,
1181 struct ion_handle *handle)
1183 struct ion_buffer *buffer;
1184 struct sg_table *table;
1186 mutex_lock(&client->lock);
1187 if (!ion_handle_validate(client, handle)) {
1188 pr_err("%s: invalid handle passed to map_dma.\n",
1190 mutex_unlock(&client->lock);
1191 return ERR_PTR(-EINVAL);
1193 buffer = handle->buffer;
1194 table = buffer->sg_table;
1195 mutex_unlock(&client->lock);
1198 EXPORT_SYMBOL(ion_sg_table);
1200 static void ion_buffer_sync_for_device(struct ion_buffer *buffer,
1202 enum dma_data_direction direction);
1204 static struct sg_table *ion_map_dma_buf(struct dma_buf_attachment *attachment,
1205 enum dma_data_direction direction)
1207 struct dma_buf *dmabuf = attachment->dmabuf;
1208 struct ion_buffer *buffer = dmabuf->priv;
1210 ion_buffer_sync_for_device(buffer, attachment->dev, direction);
1211 return buffer->sg_table;
1214 static void ion_unmap_dma_buf(struct dma_buf_attachment *attachment,
1215 struct sg_table *table,
1216 enum dma_data_direction direction)
1220 void ion_pages_sync_for_device(struct device *dev, struct page *page,
1221 size_t size, enum dma_data_direction dir)
1223 struct scatterlist sg;
1225 sg_init_table(&sg, 1);
1226 sg_set_page(&sg, page, size, 0);
1228 * This is not correct - sg_dma_address needs a dma_addr_t that is valid
1229 * for the the targeted device, but this works on the currently targeted
1232 sg_dma_address(&sg) = page_to_phys(page);
1233 dma_sync_sg_for_device(dev, &sg, 1, dir);
1236 struct ion_vma_list {
1237 struct list_head list;
1238 struct vm_area_struct *vma;
1241 static void ion_buffer_sync_for_device(struct ion_buffer *buffer,
1243 enum dma_data_direction dir)
1245 struct ion_vma_list *vma_list;
1246 int pages = PAGE_ALIGN(buffer->size) / PAGE_SIZE;
1249 pr_debug("%s: syncing for device %s\n", __func__,
1250 dev ? dev_name(dev) : "null");
1252 if (!ion_buffer_fault_user_mappings(buffer))
1255 mutex_lock(&buffer->lock);
1256 for (i = 0; i < pages; i++) {
1257 struct page *page = buffer->pages[i];
1259 if (ion_buffer_page_is_dirty(page))
1260 ion_pages_sync_for_device(dev, ion_buffer_page(page),
1263 ion_buffer_page_clean(buffer->pages + i);
1265 list_for_each_entry(vma_list, &buffer->vmas, list) {
1266 struct vm_area_struct *vma = vma_list->vma;
1268 zap_page_range(vma, vma->vm_start, vma->vm_end - vma->vm_start,
1271 mutex_unlock(&buffer->lock);
1274 static int ion_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1276 struct ion_buffer *buffer = vma->vm_private_data;
1280 mutex_lock(&buffer->lock);
1281 ion_buffer_page_dirty(buffer->pages + vmf->pgoff);
1282 BUG_ON(!buffer->pages || !buffer->pages[vmf->pgoff]);
1284 pfn = page_to_pfn(ion_buffer_page(buffer->pages[vmf->pgoff]));
1285 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
1286 mutex_unlock(&buffer->lock);
1288 return VM_FAULT_ERROR;
1290 return VM_FAULT_NOPAGE;
1293 static void ion_vm_open(struct vm_area_struct *vma)
1295 struct ion_buffer *buffer = vma->vm_private_data;
1296 struct ion_vma_list *vma_list;
1298 vma_list = kmalloc(sizeof(struct ion_vma_list), GFP_KERNEL);
1301 vma_list->vma = vma;
1302 mutex_lock(&buffer->lock);
1303 list_add(&vma_list->list, &buffer->vmas);
1304 mutex_unlock(&buffer->lock);
1305 pr_debug("%s: adding %p\n", __func__, vma);
1308 static void ion_vm_close(struct vm_area_struct *vma)
1310 struct ion_buffer *buffer = vma->vm_private_data;
1311 struct ion_vma_list *vma_list, *tmp;
1313 pr_debug("%s\n", __func__);
1314 mutex_lock(&buffer->lock);
1315 list_for_each_entry_safe(vma_list, tmp, &buffer->vmas, list) {
1316 if (vma_list->vma != vma)
1318 list_del(&vma_list->list);
1320 pr_debug("%s: deleting %p\n", __func__, vma);
1323 mutex_unlock(&buffer->lock);
1326 static struct vm_operations_struct ion_vma_ops = {
1327 .open = ion_vm_open,
1328 .close = ion_vm_close,
1329 .fault = ion_vm_fault,
1332 static int ion_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma)
1334 struct ion_buffer *buffer = dmabuf->priv;
1337 if (!buffer->heap->ops->map_user) {
1338 pr_err("%s: this heap does not define a method for mapping "
1339 "to userspace\n", __func__);
1343 if (ion_buffer_fault_user_mappings(buffer)) {
1344 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND |
1346 vma->vm_private_data = buffer;
1347 vma->vm_ops = &ion_vma_ops;
1352 if (!(buffer->flags & ION_FLAG_CACHED))
1353 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
1355 mutex_lock(&buffer->lock);
1356 /* now map it to userspace */
1357 ret = buffer->heap->ops->map_user(buffer->heap, buffer, vma);
1358 mutex_unlock(&buffer->lock);
1361 pr_err("%s: failure mapping buffer to userspace\n",
1364 trace_ion_buffer_mmap("", (unsigned int)buffer, buffer->size,
1365 vma->vm_start, vma->vm_end);
1370 int ion_munmap(struct dma_buf *dmabuf, struct vm_area_struct *vma)
1372 struct ion_buffer *buffer = dmabuf->priv;
1374 trace_ion_buffer_munmap("", (unsigned int)buffer, buffer->size,
1375 vma->vm_start, vma->vm_end);
1380 static void ion_dma_buf_release(struct dma_buf *dmabuf)
1382 struct ion_buffer *buffer = dmabuf->priv;
1383 ion_buffer_put(buffer);
1386 static void *ion_dma_buf_kmap(struct dma_buf *dmabuf, unsigned long offset)
1388 struct ion_buffer *buffer = dmabuf->priv;
1389 return buffer->vaddr + offset * PAGE_SIZE;
1392 static void ion_dma_buf_kunmap(struct dma_buf *dmabuf, unsigned long offset,
1398 static int ion_dma_buf_begin_cpu_access(struct dma_buf *dmabuf, size_t start,
1400 enum dma_data_direction direction)
1402 struct ion_buffer *buffer = dmabuf->priv;
1405 if (!buffer->heap->ops->map_kernel) {
1406 pr_err("%s: map kernel is not implemented by this heap.\n",
1411 mutex_lock(&buffer->lock);
1412 vaddr = ion_buffer_kmap_get(buffer);
1413 mutex_unlock(&buffer->lock);
1415 return PTR_ERR(vaddr);
1419 static void ion_dma_buf_end_cpu_access(struct dma_buf *dmabuf, size_t start,
1421 enum dma_data_direction direction)
1423 struct ion_buffer *buffer = dmabuf->priv;
1425 mutex_lock(&buffer->lock);
1426 ion_buffer_kmap_put(buffer);
1427 mutex_unlock(&buffer->lock);
1430 static struct dma_buf_ops dma_buf_ops = {
1431 .map_dma_buf = ion_map_dma_buf,
1432 .unmap_dma_buf = ion_unmap_dma_buf,
1434 .release = ion_dma_buf_release,
1435 .begin_cpu_access = ion_dma_buf_begin_cpu_access,
1436 .end_cpu_access = ion_dma_buf_end_cpu_access,
1437 .kmap_atomic = ion_dma_buf_kmap,
1438 .kunmap_atomic = ion_dma_buf_kunmap,
1439 .kmap = ion_dma_buf_kmap,
1440 .kunmap = ion_dma_buf_kunmap,
1443 struct dma_buf *ion_share_dma_buf(struct ion_client *client,
1444 struct ion_handle *handle)
1446 struct ion_buffer *buffer;
1447 struct dma_buf *dmabuf;
1450 mutex_lock(&client->lock);
1451 valid_handle = ion_handle_validate(client, handle);
1452 if (!valid_handle) {
1453 WARN(1, "%s: invalid handle passed to share.\n", __func__);
1454 mutex_unlock(&client->lock);
1455 return ERR_PTR(-EINVAL);
1457 buffer = handle->buffer;
1458 ion_buffer_get(buffer);
1459 mutex_unlock(&client->lock);
1461 dmabuf = dma_buf_export(buffer, &dma_buf_ops, buffer->size, O_RDWR);
1462 if (IS_ERR(dmabuf)) {
1463 ion_buffer_put(buffer);
1469 EXPORT_SYMBOL(ion_share_dma_buf);
1471 int ion_share_dma_buf_fd(struct ion_client *client, struct ion_handle *handle)
1473 struct dma_buf *dmabuf;
1476 dmabuf = ion_share_dma_buf(client, handle);
1478 return PTR_ERR(dmabuf);
1480 fd = dma_buf_fd(dmabuf, O_CLOEXEC);
1482 dma_buf_put(dmabuf);
1484 trace_ion_buffer_share(client->display_name, (unsigned int)handle->buffer,
1485 handle->buffer->size, fd);
1488 EXPORT_SYMBOL(ion_share_dma_buf_fd);
1490 struct ion_handle *ion_import_dma_buf(struct ion_client *client, int fd)
1492 struct dma_buf *dmabuf;
1493 struct ion_buffer *buffer;
1494 struct ion_handle *handle;
1497 dmabuf = dma_buf_get(fd);
1499 return ERR_PTR(PTR_ERR(dmabuf));
1500 /* if this memory came from ion */
1502 if (dmabuf->ops != &dma_buf_ops) {
1503 pr_err("%s: can not import dmabuf from another exporter\n",
1505 dma_buf_put(dmabuf);
1506 return ERR_PTR(-EINVAL);
1508 buffer = dmabuf->priv;
1510 mutex_lock(&client->lock);
1511 /* if a handle exists for this buffer just take a reference to it */
1512 handle = ion_handle_lookup(client, buffer);
1513 if (!IS_ERR(handle)) {
1514 ion_handle_get(handle);
1515 mutex_unlock(&client->lock);
1518 mutex_unlock(&client->lock);
1520 handle = ion_handle_create(client, buffer);
1524 mutex_lock(&client->lock);
1525 ret = ion_handle_add(client, handle);
1526 mutex_unlock(&client->lock);
1528 ion_handle_put(handle);
1529 handle = ERR_PTR(ret);
1532 trace_ion_buffer_import(client->display_name, (unsigned int)buffer,
1535 dma_buf_put(dmabuf);
1538 EXPORT_SYMBOL(ion_import_dma_buf);
1540 static int ion_sync_for_device(struct ion_client *client, int fd)
1542 struct dma_buf *dmabuf;
1543 struct ion_buffer *buffer;
1545 dmabuf = dma_buf_get(fd);
1547 return PTR_ERR(dmabuf);
1549 /* if this memory came from ion */
1550 if (dmabuf->ops != &dma_buf_ops) {
1551 pr_err("%s: can not sync dmabuf from another exporter\n",
1553 dma_buf_put(dmabuf);
1556 buffer = dmabuf->priv;
1558 dma_sync_sg_for_device(NULL, buffer->sg_table->sgl,
1559 buffer->sg_table->nents, DMA_BIDIRECTIONAL);
1560 dma_buf_put(dmabuf);
1564 /* fix up the cases where the ioctl direction bits are incorrect */
1565 static unsigned int ion_ioctl_dir(unsigned int cmd)
1570 case ION_IOC_CUSTOM:
1573 return _IOC_DIR(cmd);
1577 static long ion_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1579 struct ion_client *client = filp->private_data;
1580 struct ion_device *dev = client->dev;
1581 struct ion_handle *cleanup_handle = NULL;
1586 struct ion_fd_data fd;
1587 struct ion_allocation_data allocation;
1588 struct ion_handle_data handle;
1589 struct ion_custom_data custom;
1592 dir = ion_ioctl_dir(cmd);
1594 if (_IOC_SIZE(cmd) > sizeof(data))
1597 if (dir & _IOC_WRITE)
1598 if (copy_from_user(&data, (void __user *)arg, _IOC_SIZE(cmd)))
1604 struct ion_handle *handle;
1606 handle = ion_alloc(client, data.allocation.len,
1607 data.allocation.align,
1608 data.allocation.heap_id_mask,
1609 data.allocation.flags);
1611 return PTR_ERR(handle);
1613 data.allocation.handle = handle->id;
1615 cleanup_handle = handle;
1620 struct ion_handle *handle;
1622 handle = ion_handle_get_by_id(client, data.handle.handle);
1624 return PTR_ERR(handle);
1625 ion_free(client, handle);
1626 ion_handle_put(handle);
1632 struct ion_handle *handle;
1634 handle = ion_handle_get_by_id(client, data.handle.handle);
1636 return PTR_ERR(handle);
1637 data.fd.fd = ion_share_dma_buf_fd(client, handle);
1638 ion_handle_put(handle);
1643 case ION_IOC_IMPORT:
1645 struct ion_handle *handle;
1646 handle = ion_import_dma_buf(client, data.fd.fd);
1648 ret = PTR_ERR(handle);
1650 data.handle.handle = handle->id;
1655 ret = ion_sync_for_device(client, data.fd.fd);
1658 case ION_IOC_CUSTOM:
1660 if (!dev->custom_ioctl)
1662 ret = dev->custom_ioctl(client, data.custom.cmd,
1670 if (dir & _IOC_READ) {
1671 if (copy_to_user((void __user *)arg, &data, _IOC_SIZE(cmd))) {
1673 ion_free(client, cleanup_handle);
1680 static int ion_release(struct inode *inode, struct file *file)
1682 struct ion_client *client = file->private_data;
1684 pr_debug("%s: %d\n", __func__, __LINE__);
1685 ion_client_destroy(client);
1689 static int ion_open(struct inode *inode, struct file *file)
1691 struct miscdevice *miscdev = file->private_data;
1692 struct ion_device *dev = container_of(miscdev, struct ion_device, dev);
1693 struct ion_client *client;
1694 char debug_name[64];
1696 pr_debug("%s: %d\n", __func__, __LINE__);
1697 snprintf(debug_name, 64, "%u", task_pid_nr(current->group_leader));
1698 client = ion_client_create(dev, debug_name);
1700 return PTR_ERR(client);
1701 file->private_data = client;
1706 static const struct file_operations ion_fops = {
1707 .owner = THIS_MODULE,
1709 .release = ion_release,
1710 .unlocked_ioctl = ion_ioctl,
1711 .compat_ioctl = compat_ion_ioctl,
1714 static size_t ion_debug_heap_total(struct ion_client *client,
1720 mutex_lock(&client->lock);
1721 for (n = rb_first(&client->handles); n; n = rb_next(n)) {
1722 struct ion_handle *handle = rb_entry(n,
1725 if (handle->buffer->heap->id == id)
1726 size += handle->buffer->size;
1728 mutex_unlock(&client->lock);
1732 static int ion_debug_heap_show(struct seq_file *s, void *unused)
1734 struct ion_heap *heap = s->private;
1735 struct ion_device *dev = heap->dev;
1737 size_t total_size = 0;
1738 size_t total_orphaned_size = 0;
1740 seq_printf(s, "%16.s %16.s %16.s\n", "client", "pid", "size");
1741 seq_printf(s, "----------------------------------------------------\n");
1743 for (n = rb_first(&dev->clients); n; n = rb_next(n)) {
1744 struct ion_client *client = rb_entry(n, struct ion_client,
1746 size_t size = ion_debug_heap_total(client, heap->id);
1750 char task_comm[TASK_COMM_LEN];
1752 get_task_comm(task_comm, client->task);
1753 seq_printf(s, "%16.s %16u %16zu\n", task_comm,
1756 seq_printf(s, "%16.s %16u %16zu\n", client->name,
1760 seq_printf(s, "----------------------------------------------------\n");
1761 seq_printf(s, "orphaned allocations (info is from last known client):"
1763 mutex_lock(&dev->buffer_lock);
1764 for (n = rb_first(&dev->buffers); n; n = rb_next(n)) {
1765 struct ion_buffer *buffer = rb_entry(n, struct ion_buffer,
1767 if (buffer->heap->id != heap->id)
1769 total_size += buffer->size;
1770 if (!buffer->handle_count) {
1771 seq_printf(s, "%16.s %16u %16zu %d %d\n",
1772 buffer->task_comm, buffer->pid,
1773 buffer->size, buffer->kmap_cnt,
1774 atomic_read(&buffer->ref.refcount));
1775 total_orphaned_size += buffer->size;
1778 mutex_unlock(&dev->buffer_lock);
1779 seq_printf(s, "----------------------------------------------------\n");
1780 seq_printf(s, "%16.s %16zu\n", "total orphaned",
1781 total_orphaned_size);
1782 seq_printf(s, "%16.s %16zu\n", "total ", total_size);
1783 if (heap->flags & ION_HEAP_FLAG_DEFER_FREE)
1784 seq_printf(s, "%16.s %16zu\n", "deferred free",
1785 heap->free_list_size);
1786 seq_printf(s, "----------------------------------------------------\n");
1788 if (heap->debug_show)
1789 heap->debug_show(heap, s, unused);
1794 static int ion_debug_heap_open(struct inode *inode, struct file *file)
1796 return single_open(file, ion_debug_heap_show, inode->i_private);
1799 static const struct file_operations debug_heap_fops = {
1800 .open = ion_debug_heap_open,
1802 .llseek = seq_lseek,
1803 .release = single_release,
1806 #ifdef DEBUG_HEAP_SHRINKER
1807 static int debug_shrink_set(void *data, u64 val)
1809 struct ion_heap *heap = data;
1810 struct shrink_control sc;
1819 objs = heap->shrinker.shrink(&heap->shrinker, &sc);
1820 sc.nr_to_scan = objs;
1822 heap->shrinker.shrink(&heap->shrinker, &sc);
1826 static int debug_shrink_get(void *data, u64 *val)
1828 struct ion_heap *heap = data;
1829 struct shrink_control sc;
1835 objs = heap->shrinker.shrink(&heap->shrinker, &sc);
1840 DEFINE_SIMPLE_ATTRIBUTE(debug_shrink_fops, debug_shrink_get,
1841 debug_shrink_set, "%llu\n");
1845 // struct "cma" quoted from drivers/base/dma-contiguous.c
1847 unsigned long base_pfn;
1848 unsigned long count;
1849 unsigned long *bitmap;
1852 // struct "ion_cma_heap" quoted from drivers/staging/android/ion/ion_cma_heap.c
1853 struct ion_cma_heap {
1854 struct ion_heap heap;
1858 static int ion_cma_heap_debug_show(struct seq_file *s, void *unused)
1860 struct ion_heap *heap = s->private;
1861 struct ion_cma_heap *cma_heap = container_of(heap,
1862 struct ion_cma_heap,
1864 struct device *dev = cma_heap->dev;
1865 struct cma *cma = dev_get_cma_area(dev);
1867 int rows = cma->count/(SZ_1M >> PAGE_SHIFT);
1868 phys_addr_t base = __pfn_to_phys(cma->base_pfn);
1870 seq_printf(s, "%s Heap bitmap:\n", heap->name);
1872 for(i = rows - 1; i>= 0; i--){
1873 seq_printf(s, "%.4uM@0x%08x: %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
1874 i+1, base+(i)*SZ_1M,
1875 cma->bitmap[i*8 + 7],
1876 cma->bitmap[i*8 + 6],
1877 cma->bitmap[i*8 + 5],
1878 cma->bitmap[i*8 + 4],
1879 cma->bitmap[i*8 + 3],
1880 cma->bitmap[i*8 + 2],
1881 cma->bitmap[i*8 + 1],
1884 seq_printf(s, "Heap size: %luM, Heap base: 0x%08x\n",
1885 (cma->count)>>8, base);
1890 static int ion_debug_heap_bitmap_open(struct inode *inode, struct file *file)
1892 return single_open(file, ion_cma_heap_debug_show, inode->i_private);
1895 static const struct file_operations debug_heap_bitmap_fops = {
1896 .open = ion_debug_heap_bitmap_open,
1898 .llseek = seq_lseek,
1899 .release = single_release,
1903 void ion_device_add_heap(struct ion_device *dev, struct ion_heap *heap)
1905 struct dentry *debug_file;
1907 if (!heap->ops->allocate || !heap->ops->free || !heap->ops->map_dma ||
1908 !heap->ops->unmap_dma)
1909 pr_err("%s: can not add heap with invalid ops struct.\n",
1912 if (heap->flags & ION_HEAP_FLAG_DEFER_FREE)
1913 ion_heap_init_deferred_free(heap);
1915 if ((heap->flags & ION_HEAP_FLAG_DEFER_FREE) || heap->ops->shrink)
1916 ion_heap_init_shrinker(heap);
1919 down_write(&dev->lock);
1920 /* use negative heap->id to reverse the priority -- when traversing
1921 the list later attempt higher id numbers first */
1922 plist_node_init(&heap->node, -heap->id);
1923 plist_add(&heap->node, &dev->heaps);
1924 debug_file = debugfs_create_file(heap->name, 0664,
1925 dev->heaps_debug_root, heap,
1929 char buf[256], *path;
1930 path = dentry_path(dev->heaps_debug_root, buf, 256);
1931 pr_err("Failed to create heap debugfs at %s/%s\n",
1935 #ifdef DEBUG_HEAP_SHRINKER
1936 if (heap->shrinker.shrink) {
1937 char debug_name[64];
1939 snprintf(debug_name, 64, "%s_shrink", heap->name);
1940 debug_file = debugfs_create_file(
1941 debug_name, 0644, dev->heaps_debug_root, heap,
1942 &debug_shrink_fops);
1944 char buf[256], *path;
1945 path = dentry_path(dev->heaps_debug_root, buf, 256);
1946 pr_err("Failed to create heap shrinker debugfs at %s/%s\n",
1952 if (ION_HEAP_TYPE_DMA==heap->type) {
1953 char* heap_bitmap_name = kasprintf(
1954 GFP_KERNEL, "%s-bitmap", heap->name);
1955 debug_file = debugfs_create_file(heap_bitmap_name, 0664,
1956 dev->heaps_debug_root, heap,
1957 &debug_heap_bitmap_fops);
1959 char buf[256], *path;
1960 path = dentry_path(dev->heaps_debug_root, buf, 256);
1961 pr_err("Failed to create heap debugfs at %s/%s\n",
1962 path, heap_bitmap_name);
1964 kfree(heap_bitmap_name);
1967 up_write(&dev->lock);
1970 struct ion_device *ion_device_create(long (*custom_ioctl)
1971 (struct ion_client *client,
1975 struct ion_device *idev;
1978 idev = kzalloc(sizeof(struct ion_device), GFP_KERNEL);
1980 return ERR_PTR(-ENOMEM);
1982 idev->dev.minor = MISC_DYNAMIC_MINOR;
1983 idev->dev.name = "ion";
1984 idev->dev.fops = &ion_fops;
1985 idev->dev.parent = NULL;
1986 ret = misc_register(&idev->dev);
1988 pr_err("ion: failed to register misc device.\n");
1989 return ERR_PTR(ret);
1992 idev->debug_root = debugfs_create_dir("ion", NULL);
1993 if (!idev->debug_root) {
1994 pr_err("ion: failed to create debugfs root directory.\n");
1997 idev->heaps_debug_root = debugfs_create_dir("heaps", idev->debug_root);
1998 if (!idev->heaps_debug_root) {
1999 pr_err("ion: failed to create debugfs heaps directory.\n");
2002 idev->clients_debug_root = debugfs_create_dir("clients",
2004 if (!idev->clients_debug_root)
2005 pr_err("ion: failed to create debugfs clients directory.\n");
2007 #ifdef CONFIG_ION_ROCKCHIP_SNAPSHOT
2008 rockchip_ion_snapshot_debugfs(idev->debug_root);
2013 idev->custom_ioctl = custom_ioctl;
2014 idev->buffers = RB_ROOT;
2015 mutex_init(&idev->buffer_lock);
2016 init_rwsem(&idev->lock);
2017 plist_head_init(&idev->heaps);
2018 idev->clients = RB_ROOT;
2022 void ion_device_destroy(struct ion_device *dev)
2024 misc_deregister(&dev->dev);
2025 debugfs_remove_recursive(dev->debug_root);
2026 /* XXX need to free the heaps and clients ? */
2030 void __init ion_reserve(struct ion_platform_data *data)
2034 for (i = 0; i < data->nr; i++) {
2035 if (data->heaps[i].size == 0)
2038 if (data->heaps[i].id==ION_CMA_HEAP_ID) {
2039 struct device *dev = (struct device*)data->heaps[i].priv;
2040 int ret = dma_declare_contiguous(dev,
2041 data->heaps[i].size,
2042 data->heaps[i].base,
2043 MEMBLOCK_ALLOC_ANYWHERE);
2045 pr_err("%s: dma_declare_contiguous failed %d\n",
2049 data->heaps[i].base = PFN_PHYS(dev_get_cma_area(dev)->base_pfn);
2050 } else if (data->heaps[i].base == 0) {
2052 paddr = memblock_alloc_base(data->heaps[i].size,
2053 data->heaps[i].align,
2054 MEMBLOCK_ALLOC_ANYWHERE);
2056 pr_err("%s: error allocating memblock for "
2061 data->heaps[i].base = paddr;
2063 int ret = memblock_reserve(data->heaps[i].base,
2064 data->heaps[i].size);
2066 pr_err("memblock reserve of %zx@%lx failed\n",
2067 data->heaps[i].size,
2068 data->heaps[i].base);
2072 pr_info("%s: %s reserved base %lx size %zu\n", __func__,
2073 data->heaps[i].name,
2074 data->heaps[i].base,
2075 data->heaps[i].size);
2079 #ifdef CONFIG_ION_ROCKCHIP_SNAPSHOT
2081 // Find the maximum can be allocated memory
2082 static unsigned long ion_find_max_zero_area(unsigned long *map, unsigned long size)
2084 unsigned long index, i, zero_sz, max_zero_sz, start;
2089 index = find_next_zero_bit(map, size, start);
2090 if (index>=size) break;
2092 i = find_next_bit(map, size, index);
2094 pr_debug("zero[%lx, %lx]\n", index, zero_sz);
2095 max_zero_sz = max(max_zero_sz, zero_sz);
2097 } while(start<=size);
2099 pr_debug("max_zero_sz=%lx\n", max_zero_sz);
2103 static int ion_snapshot_save(struct ion_device *idev, size_t len)
2105 static struct seq_file seqf;
2106 struct ion_heap *heap;
2109 seqf.buf = rockchip_ion_snapshot_get(&seqf.size);
2113 memset(seqf.buf, 0, seqf.size);
2115 pr_debug("%s: save snapshot 0x%x@0x%lx\n", __func__, seqf.size,
2118 seq_printf(&seqf, "call by comm: %s pid: %d, alloc: %uKB\n",
2119 current->comm, current->pid, len>>10);
2121 down_read(&idev->lock);
2123 plist_for_each_entry(heap, &idev->heaps, node) {
2124 seqf.private = (void*)heap;
2125 seq_printf(&seqf, "++++++++++++++++ HEAP: %s ++++++++++++++++\n",
2127 ion_debug_heap_show(&seqf, NULL);
2128 if (ION_HEAP_TYPE_DMA==heap->type) {
2129 struct ion_cma_heap *cma_heap = container_of(heap,
2130 struct ion_cma_heap,
2132 struct cma *cma = dev_get_cma_area(cma_heap->dev);
2133 seq_printf(&seqf, "\n");
2134 seq_printf(&seqf, "Maximum allocation of pages: %ld\n",
2135 ion_find_max_zero_area(cma->bitmap, cma->count));
2136 seq_printf(&seqf, "\n");
2140 up_read(&idev->lock);