1 // SPDX-License-Identifier: GPL-2.0
3 * drivers/staging/android/ion/ion.c
5 * Copyright (C) 2011 Google, Inc.
8 #include <linux/anon_inodes.h>
9 #include <linux/debugfs.h>
10 #include <linux/device.h>
11 #include <linux/dma-buf.h>
12 #include <linux/err.h>
13 #include <linux/export.h>
14 #include <linux/file.h>
15 #include <linux/freezer.h>
17 #include <linux/idr.h>
18 #include <linux/kthread.h>
19 #include <linux/list.h>
20 #include <linux/memblock.h>
21 #include <linux/miscdevice.h>
23 #include <linux/mm_types.h>
24 #include <linux/rbtree.h>
25 #include <linux/sched/task.h>
26 #include <linux/seq_file.h>
27 #include <linux/slab.h>
28 #include <linux/uaccess.h>
29 #include <linux/vmalloc.h>
33 static struct ion_device *internal_dev;
36 /* this function should only be called while dev->lock is held */
37 static void ion_buffer_add(struct ion_device *dev,
38 struct ion_buffer *buffer)
40 struct rb_node **p = &dev->buffers.rb_node;
41 struct rb_node *parent = NULL;
42 struct ion_buffer *entry;
46 entry = rb_entry(parent, struct ion_buffer, node);
50 } else if (buffer > entry) {
53 pr_err("%s: buffer already found.", __func__);
58 rb_link_node(&buffer->node, parent, p);
59 rb_insert_color(&buffer->node, &dev->buffers);
62 /* this function should only be called while dev->lock is held */
63 static struct ion_buffer *ion_buffer_create(struct ion_heap *heap,
64 struct ion_device *dev,
68 struct ion_buffer *buffer;
71 buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
73 return ERR_PTR(-ENOMEM);
76 buffer->flags = flags;
78 ret = heap->ops->allocate(heap, buffer, len, flags);
81 if (!(heap->flags & ION_HEAP_FLAG_DEFER_FREE))
84 ion_heap_freelist_drain(heap, 0);
85 ret = heap->ops->allocate(heap, buffer, len, flags);
90 if (!buffer->sg_table) {
91 WARN_ONCE(1, "This heap needs to set the sgtable");
101 INIT_LIST_HEAD(&buffer->attachments);
102 mutex_init(&buffer->lock);
103 mutex_lock(&dev->buffer_lock);
104 ion_buffer_add(dev, buffer);
105 mutex_unlock(&dev->buffer_lock);
109 heap->ops->free(buffer);
115 void ion_buffer_destroy(struct ion_buffer *buffer)
117 if (WARN_ON(buffer->kmap_cnt > 0))
118 buffer->heap->ops->unmap_kernel(buffer->heap, buffer);
119 buffer->heap->ops->free(buffer);
123 static void _ion_buffer_destroy(struct ion_buffer *buffer)
125 struct ion_heap *heap = buffer->heap;
126 struct ion_device *dev = buffer->dev;
128 mutex_lock(&dev->buffer_lock);
129 rb_erase(&buffer->node, &dev->buffers);
130 mutex_unlock(&dev->buffer_lock);
132 if (heap->flags & ION_HEAP_FLAG_DEFER_FREE)
133 ion_heap_freelist_add(heap, buffer);
135 ion_buffer_destroy(buffer);
138 static void *ion_buffer_kmap_get(struct ion_buffer *buffer)
142 if (buffer->kmap_cnt) {
144 return buffer->vaddr;
146 vaddr = buffer->heap->ops->map_kernel(buffer->heap, buffer);
147 if (WARN_ONCE(!vaddr,
148 "heap->ops->map_kernel should return ERR_PTR on error"))
149 return ERR_PTR(-EINVAL);
152 buffer->vaddr = vaddr;
157 static void ion_buffer_kmap_put(struct ion_buffer *buffer)
160 if (!buffer->kmap_cnt) {
161 buffer->heap->ops->unmap_kernel(buffer->heap, buffer);
162 buffer->vaddr = NULL;
166 static struct sg_table *dup_sg_table(struct sg_table *table)
168 struct sg_table *new_table;
170 struct scatterlist *sg, *new_sg;
172 new_table = kzalloc(sizeof(*new_table), GFP_KERNEL);
174 return ERR_PTR(-ENOMEM);
176 ret = sg_alloc_table(new_table, table->nents, GFP_KERNEL);
179 return ERR_PTR(-ENOMEM);
182 new_sg = new_table->sgl;
183 for_each_sg(table->sgl, sg, table->nents, i) {
184 memcpy(new_sg, sg, sizeof(*sg));
185 new_sg->dma_address = 0;
186 new_sg = sg_next(new_sg);
192 static void free_duped_table(struct sg_table *table)
194 sg_free_table(table);
198 struct ion_dma_buf_attachment {
200 struct sg_table *table;
201 struct list_head list;
204 static int ion_dma_buf_attach(struct dma_buf *dmabuf, struct device *dev,
205 struct dma_buf_attachment *attachment)
207 struct ion_dma_buf_attachment *a;
208 struct sg_table *table;
209 struct ion_buffer *buffer = dmabuf->priv;
211 a = kzalloc(sizeof(*a), GFP_KERNEL);
215 table = dup_sg_table(buffer->sg_table);
223 INIT_LIST_HEAD(&a->list);
225 attachment->priv = a;
227 mutex_lock(&buffer->lock);
228 list_add(&a->list, &buffer->attachments);
229 mutex_unlock(&buffer->lock);
234 static void ion_dma_buf_detatch(struct dma_buf *dmabuf,
235 struct dma_buf_attachment *attachment)
237 struct ion_dma_buf_attachment *a = attachment->priv;
238 struct ion_buffer *buffer = dmabuf->priv;
240 free_duped_table(a->table);
241 mutex_lock(&buffer->lock);
243 mutex_unlock(&buffer->lock);
248 static struct sg_table *ion_map_dma_buf(struct dma_buf_attachment *attachment,
249 enum dma_data_direction direction)
251 struct ion_dma_buf_attachment *a = attachment->priv;
252 struct sg_table *table;
256 if (!dma_map_sg(attachment->dev, table->sgl, table->nents,
258 return ERR_PTR(-ENOMEM);
263 static void ion_unmap_dma_buf(struct dma_buf_attachment *attachment,
264 struct sg_table *table,
265 enum dma_data_direction direction)
267 dma_unmap_sg(attachment->dev, table->sgl, table->nents, direction);
270 static int ion_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma)
272 struct ion_buffer *buffer = dmabuf->priv;
275 if (!buffer->heap->ops->map_user) {
276 pr_err("%s: this heap does not define a method for mapping to userspace\n",
281 if (!(buffer->flags & ION_FLAG_CACHED))
282 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
284 mutex_lock(&buffer->lock);
285 /* now map it to userspace */
286 ret = buffer->heap->ops->map_user(buffer->heap, buffer, vma);
287 mutex_unlock(&buffer->lock);
290 pr_err("%s: failure mapping buffer to userspace\n",
296 static void ion_dma_buf_release(struct dma_buf *dmabuf)
298 struct ion_buffer *buffer = dmabuf->priv;
300 _ion_buffer_destroy(buffer);
303 static void *ion_dma_buf_kmap(struct dma_buf *dmabuf, unsigned long offset)
305 struct ion_buffer *buffer = dmabuf->priv;
307 return buffer->vaddr + offset * PAGE_SIZE;
310 static void ion_dma_buf_kunmap(struct dma_buf *dmabuf, unsigned long offset,
315 static int ion_dma_buf_begin_cpu_access(struct dma_buf *dmabuf,
316 enum dma_data_direction direction)
318 struct ion_buffer *buffer = dmabuf->priv;
320 struct ion_dma_buf_attachment *a;
323 * TODO: Move this elsewhere because we don't always need a vaddr
325 if (buffer->heap->ops->map_kernel) {
326 mutex_lock(&buffer->lock);
327 vaddr = ion_buffer_kmap_get(buffer);
328 mutex_unlock(&buffer->lock);
331 mutex_lock(&buffer->lock);
332 list_for_each_entry(a, &buffer->attachments, list) {
333 dma_sync_sg_for_cpu(a->dev, a->table->sgl, a->table->nents,
336 mutex_unlock(&buffer->lock);
341 static int ion_dma_buf_end_cpu_access(struct dma_buf *dmabuf,
342 enum dma_data_direction direction)
344 struct ion_buffer *buffer = dmabuf->priv;
345 struct ion_dma_buf_attachment *a;
347 if (buffer->heap->ops->map_kernel) {
348 mutex_lock(&buffer->lock);
349 ion_buffer_kmap_put(buffer);
350 mutex_unlock(&buffer->lock);
353 mutex_lock(&buffer->lock);
354 list_for_each_entry(a, &buffer->attachments, list) {
355 dma_sync_sg_for_device(a->dev, a->table->sgl, a->table->nents,
358 mutex_unlock(&buffer->lock);
363 static const struct dma_buf_ops dma_buf_ops = {
364 .map_dma_buf = ion_map_dma_buf,
365 .unmap_dma_buf = ion_unmap_dma_buf,
367 .release = ion_dma_buf_release,
368 .attach = ion_dma_buf_attach,
369 .detach = ion_dma_buf_detatch,
370 .begin_cpu_access = ion_dma_buf_begin_cpu_access,
371 .end_cpu_access = ion_dma_buf_end_cpu_access,
372 .map = ion_dma_buf_kmap,
373 .unmap = ion_dma_buf_kunmap,
376 int ion_alloc(size_t len, unsigned int heap_id_mask, unsigned int flags)
378 struct ion_device *dev = internal_dev;
379 struct ion_buffer *buffer = NULL;
380 struct ion_heap *heap;
381 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
383 struct dma_buf *dmabuf;
385 pr_debug("%s: len %zu heap_id_mask %u flags %x\n", __func__,
386 len, heap_id_mask, flags);
388 * traverse the list of heaps available in this system in priority
389 * order. If the heap type is supported by the client, and matches the
390 * request of the caller allocate from it. Repeat until allocate has
391 * succeeded or all heaps have been tried
393 len = PAGE_ALIGN(len);
398 down_read(&dev->lock);
399 plist_for_each_entry(heap, &dev->heaps, node) {
400 /* if the caller didn't specify this heap id */
401 if (!((1 << heap->id) & heap_id_mask))
403 buffer = ion_buffer_create(heap, dev, len, flags);
413 return PTR_ERR(buffer);
415 exp_info.ops = &dma_buf_ops;
416 exp_info.size = buffer->size;
417 exp_info.flags = O_RDWR;
418 exp_info.priv = buffer;
420 dmabuf = dma_buf_export(&exp_info);
421 if (IS_ERR(dmabuf)) {
422 _ion_buffer_destroy(buffer);
423 return PTR_ERR(dmabuf);
426 fd = dma_buf_fd(dmabuf, O_CLOEXEC);
433 int ion_query_heaps(struct ion_heap_query *query)
435 struct ion_device *dev = internal_dev;
436 struct ion_heap_data __user *buffer = u64_to_user_ptr(query->heaps);
437 int ret = -EINVAL, cnt = 0, max_cnt;
438 struct ion_heap *heap;
439 struct ion_heap_data hdata;
441 memset(&hdata, 0, sizeof(hdata));
443 down_read(&dev->lock);
445 query->cnt = dev->heap_cnt;
453 max_cnt = query->cnt;
455 plist_for_each_entry(heap, &dev->heaps, node) {
456 strncpy(hdata.name, heap->name, MAX_HEAP_NAME);
457 hdata.name[sizeof(hdata.name) - 1] = '\0';
458 hdata.type = heap->type;
459 hdata.heap_id = heap->id;
461 if (copy_to_user(&buffer[cnt], &hdata, sizeof(hdata))) {
478 static const struct file_operations ion_fops = {
479 .owner = THIS_MODULE,
480 .unlocked_ioctl = ion_ioctl,
482 .compat_ioctl = ion_ioctl,
486 static int debug_shrink_set(void *data, u64 val)
488 struct ion_heap *heap = data;
489 struct shrink_control sc;
492 sc.gfp_mask = GFP_HIGHUSER;
496 objs = heap->shrinker.count_objects(&heap->shrinker, &sc);
497 sc.nr_to_scan = objs;
500 heap->shrinker.scan_objects(&heap->shrinker, &sc);
504 static int debug_shrink_get(void *data, u64 *val)
506 struct ion_heap *heap = data;
507 struct shrink_control sc;
510 sc.gfp_mask = GFP_HIGHUSER;
513 objs = heap->shrinker.count_objects(&heap->shrinker, &sc);
518 DEFINE_SIMPLE_ATTRIBUTE(debug_shrink_fops, debug_shrink_get,
519 debug_shrink_set, "%llu\n");
521 void ion_device_add_heap(struct ion_heap *heap)
523 struct ion_device *dev = internal_dev;
526 if (!heap->ops->allocate || !heap->ops->free)
527 pr_err("%s: can not add heap with invalid ops struct.\n",
530 spin_lock_init(&heap->free_lock);
531 heap->free_list_size = 0;
533 if (heap->flags & ION_HEAP_FLAG_DEFER_FREE)
534 ion_heap_init_deferred_free(heap);
536 if ((heap->flags & ION_HEAP_FLAG_DEFER_FREE) || heap->ops->shrink) {
537 ret = ion_heap_init_shrinker(heap);
539 pr_err("%s: Failed to register shrinker\n", __func__);
543 down_write(&dev->lock);
544 heap->id = heap_id++;
546 * use negative heap->id to reverse the priority -- when traversing
547 * the list later attempt higher id numbers first
549 plist_node_init(&heap->node, -heap->id);
550 plist_add(&heap->node, &dev->heaps);
552 if (heap->shrinker.count_objects && heap->shrinker.scan_objects) {
555 snprintf(debug_name, 64, "%s_shrink", heap->name);
556 debugfs_create_file(debug_name, 0644, dev->debug_root,
557 heap, &debug_shrink_fops);
561 up_write(&dev->lock);
563 EXPORT_SYMBOL(ion_device_add_heap);
565 static int ion_device_create(void)
567 struct ion_device *idev;
570 idev = kzalloc(sizeof(*idev), GFP_KERNEL);
574 idev->dev.minor = MISC_DYNAMIC_MINOR;
575 idev->dev.name = "ion";
576 idev->dev.fops = &ion_fops;
577 idev->dev.parent = NULL;
578 ret = misc_register(&idev->dev);
580 pr_err("ion: failed to register misc device.\n");
585 idev->debug_root = debugfs_create_dir("ion", NULL);
586 idev->buffers = RB_ROOT;
587 mutex_init(&idev->buffer_lock);
588 init_rwsem(&idev->lock);
589 plist_head_init(&idev->heaps);
593 subsys_initcall(ion_device_create);