Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 389e2406 authored by Isaac J. Manjarres's avatar Isaac J. Manjarres Committed by Sandeep Patil
Browse files

ANDROID: staging: android: ion: Expose ion_alloc() to kernel space



Expose ion_alloc() to kernel space clients. Users of this
function will receive a dma-buf structure for sharing
the ION buffer that was allocated.

Bug: 133508579
Test: ion-unit-tests
Change-Id: I410044127ba92a759a79c65e422b0b75b74e2159
Signed-off-by: default avatarIsaac J. Manjarres <isaacm@codeaurora.org>
Signed-off-by: default avatarSandeep Patil <sspatil@google.com>
parent f40a12b3
Loading
Loading
Loading
Loading
+30 −10
Original line number Diff line number Diff line
@@ -3,6 +3,8 @@
 * ION Memory Allocator
 *
 * Copyright (C) 2011 Google, Inc.
 * Copyright (c) 2019, The Linux Foundation. All rights reserved.
 *
 */

#include <linux/debugfs.h>
@@ -387,13 +389,13 @@ static const struct dma_buf_ops dma_buf_ops = {
	.unmap = ion_dma_buf_kunmap,
};

static int ion_alloc(size_t len, unsigned int heap_id_mask, unsigned int flags)
static struct dma_buf *ion_alloc_dmabuf(size_t len, unsigned int heap_id_mask,
					unsigned int flags)
{
	struct ion_device *dev = internal_dev;
	struct ion_buffer *buffer = NULL;
	struct ion_heap *heap;
	DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
	int fd;
	struct dma_buf *dmabuf;

	pr_debug("%s: len %zu heap_id_mask %u flags %x\n", __func__,
@@ -407,7 +409,7 @@ static int ion_alloc(size_t len, unsigned int heap_id_mask, unsigned int flags)
	len = PAGE_ALIGN(len);

	if (!len)
		return -EINVAL;
		return ERR_PTR(-EINVAL);

	down_read(&dev->lock);
	plist_for_each_entry(heap, &dev->heaps, node) {
@@ -421,10 +423,10 @@ static int ion_alloc(size_t len, unsigned int heap_id_mask, unsigned int flags)
	up_read(&dev->lock);

	if (!buffer)
		return -ENODEV;
		return ERR_PTR(-ENODEV);

	if (IS_ERR(buffer))
		return PTR_ERR(buffer);
		return ERR_CAST(buffer);

	exp_info.ops = &dma_buf_ops;
	exp_info.size = buffer->size;
@@ -432,11 +434,29 @@ static int ion_alloc(size_t len, unsigned int heap_id_mask, unsigned int flags)
	exp_info.priv = buffer;

	dmabuf = dma_buf_export(&exp_info);
	if (IS_ERR(dmabuf)) {
	if (IS_ERR(dmabuf))
		_ion_buffer_destroy(buffer);
		return PTR_ERR(dmabuf);

	return dmabuf;
}

struct dma_buf *ion_alloc(size_t len, unsigned int heap_id_mask,
			  unsigned int flags)
{
	return ion_alloc_dmabuf(len, heap_id_mask, flags);
}
EXPORT_SYMBOL_GPL(ion_alloc);

static int ion_alloc_fd(size_t len, unsigned int heap_id_mask,
			unsigned int flags)
{
	int fd;
	struct dma_buf *dmabuf;

	dmabuf = ion_alloc_dmabuf(len, heap_id_mask, flags);
	if (IS_ERR(dmabuf))
		return PTR_ERR(dmabuf);

	fd = dma_buf_fd(dmabuf, O_CLOEXEC);
	if (fd < 0)
		dma_buf_put(dmabuf);
@@ -540,7 +560,7 @@ static long ion_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
	{
		int fd;

		fd = ion_alloc(data.allocation.len,
		fd = ion_alloc_fd(data.allocation.len,
				  data.allocation.heap_id_mask,
				  data.allocation.flags);
		if (fd < 0)

include/linux/ion.h

0 → 100644
+26 −0
Original line number Diff line number Diff line
/* SPDX-License-Identifier: GPL-2.0-only */
/*
 * Copyright (c) 2019, The Linux Foundation. All rights reserved.
 */

#ifndef _ION_KERNEL_H
#define _ION_KERNEL_H

#include <linux/dma-buf.h>
#include <linux/err.h>

#ifdef CONFIG_ION
/*
 * Allocates an ION buffer.
 */
struct dma_buf *ion_alloc(size_t len, unsigned int heap_id_mask,
			  unsigned int flags);

#else
static inline struct dma_buf *ion_alloc(size_t len, unsigned int heap_id_mask,
					unsigned int flags)
{
	return ERR_PTR(-ENOMEM);
}
#endif /* CONFIG_ION */
#endif /* _ION_KERNEL_H */