libion: Initial version of the libion-android1 package

Change-Id: I13997a1dd5b0b0b0fc5dc9685f7c69ce5dae001a
diff --git a/debian/changelog b/debian/changelog
new file mode 100644
index 0000000..aa9c540
--- /dev/null
+++ b/debian/changelog
@@ -0,0 +1,5 @@
+android-core (1-1) mendel-chef; urgency=medium
+
+  * Initial package creation. (CLOSES: 131340748)
+
+ -- Coral <coral-support@google.com>  Thu, 04 Apr 2019 17:08:25 -0700
diff --git a/debian/compat b/debian/compat
new file mode 100644
index 0000000..f599e28
--- /dev/null
+++ b/debian/compat
@@ -0,0 +1 @@
+10
diff --git a/debian/control b/debian/control
new file mode 100644
index 0000000..2e91c79
--- /dev/null
+++ b/debian/control
@@ -0,0 +1,21 @@
+Source: android-core
+Maintainer: Coral <coral-support@google.com>
+Build-Depends: bc,
+               cpio,
+               debhelper (>=10),
+               gcc-aarch64-linux-gnu,
+               libssl-dev,
+               python,
+               quilt
+Section: misc
+Priority: optional
+Standards-Version: 3.9.8
+
+Package: libion-android1
+Section: misc
+Priority: optional
+Architecture: arm64 armhf
+Depends: ${misc:Depends}
+Description: Android's libion user space library for using the ion memory allocator
+ This is the user-space library for interacting with the Android ion memory
+ allocator.
diff --git a/debian/copyright b/debian/copyright
new file mode 100644
index 0000000..4a6a2a0
--- /dev/null
+++ b/debian/copyright
@@ -0,0 +1,7 @@
+Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
+Upstream-Name: android-core
+Source: https://coral.googlesource.com/android-core-debian
+
+Files: *
+Copyright: Copyright 2018 Google, LLC <coral-support@google.com>
+License: Apache-2.0
diff --git a/debian/libion-android1.install b/debian/libion-android1.install
new file mode 100644
index 0000000..ac7302d
--- /dev/null
+++ b/debian/libion-android1.install
@@ -0,0 +1 @@
+libion-android/usr /
diff --git a/debian/patches/0001-libion-Add-makefile.patch b/debian/patches/0001-libion-Add-makefile.patch
new file mode 100644
index 0000000..140c3c5
--- /dev/null
+++ b/debian/patches/0001-libion-Add-makefile.patch
@@ -0,0 +1,82 @@
+From 61500b369c926840d5a519a02673d8cee63db62a Mon Sep 17 00:00:00 2001
+From: Yong Wu <yong.wu@mediatek.com>
+Date: Tue, 9 Jan 2018 13:51:40 +0800
+Subject: [PATCH 1/2] libion: Add makefile
+
+1. Add makefile
+2. fix size_t build fail
+3. fix LOGD build fail
+
+Signed-off-by: Yong Wu <yong.wu@mediatek.com>
+---
+ libion/Makefile                   | 26 ++++++++++++++++++++++++++
+ libion/ion.c                      |  7 ++++++-
+ libion/kernel-headers/linux/ion.h |  1 +
+ 3 files changed, 33 insertions(+), 1 deletion(-)
+ create mode 100644 libion/Makefile
+
+diff --git a/libion/Makefile b/libion/Makefile
+new file mode 100644
+index 0000000..9112288
+--- /dev/null
++++ b/libion/Makefile
+@@ -0,0 +1,26 @@
++TARGET=libion-android.so.1
++SRC=ion.c
++OBJS=${SRC:%.c=%.o}
++
++.PHONY: all clean install
++
++all : ${TARGET}
++
++${TARGET} :  $(OBJS)
++	${CC} -g -shared -Wl,-soname,libion-android.so.1 -o ${TARGET} $<
++
++%.o: %.c
++	${CC} -g -fPIC -I./include -I./kernel-headers -c $< -o $@
++
++clean:
++	${RM} -r *.o
++
++install:
++	install -d ${DESTDIR}${LIBDIR}
++	install -d ${DESTDIR}${INCDIR}/
++	install -d ${DESTDIR}${INCDIR}/ion/
++	install -m 644 ${SRCDIR}/libion/libion-android.so.1 ${DESTDIR}${LIBDIR}
++	install -m 444 ${SRCDIR}/libion/include/ion/ion.h ${DESTDIR}${INCDIR}/ion/ion.h
++	install -d ${DESTDIR}${INCDIR}/linux
++	install -m 444 ${SRCDIR}/libion/kernel-headers/linux/ion.h ${DESTDIR}${INCDIR}/linux/ion.h
++	install -m 444 ${SRCDIR}/libion/kernel-headers/linux/ion_mtk.h ${DESTDIR}${INCDIR}/linux/ion_mtk.h
+diff --git a/libion/ion.c b/libion/ion.c
+index 9aaa6f2..bf4dec2 100644
+--- a/libion/ion.c
++++ b/libion/ion.c
+@@ -30,7 +30,12 @@
+ #include <unistd.h>
+ 
+ #include <ion/ion.h>
+-#include <log/log.h>
++/* #include <log/log.h> */
++
++#define ALOGE(format, arg...) \
++	    do { \
++                dprintf(0, "%s" format"\n", "LIBION", ##arg);  \
++        } while (0)
+ 
+ int ion_open()
+ {
+diff --git a/libion/kernel-headers/linux/ion.h b/libion/kernel-headers/linux/ion.h
+index 3c28080..7c0aa34 100644
+--- a/libion/kernel-headers/linux/ion.h
++++ b/libion/kernel-headers/linux/ion.h
+@@ -20,6 +20,7 @@
+ #define _UAPI_LINUX_ION_H
+ #include <linux/ioctl.h>
+ #include <linux/types.h>
++#include <sys/types.h>
+ /* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
+ typedef int ion_user_handle_t;
+ enum ion_heap_type {
+-- 
+1.9.1
+
diff --git a/debian/patches/0002-libion-Add-MediaTek-ION-special-heap-and-command.patch b/debian/patches/0002-libion-Add-MediaTek-ION-special-heap-and-command.patch
new file mode 100644
index 0000000..ce72feb
--- /dev/null
+++ b/debian/patches/0002-libion-Add-MediaTek-ION-special-heap-and-command.patch
@@ -0,0 +1,260 @@
+From cb5fc70e9fa02e8dc4851c095eeee4a2c9e27be8 Mon Sep 17 00:00:00 2001
+From: Yong Wu <yong.wu@mediatek.com>
+Date: Tue, 9 Jan 2018 12:18:21 +0800
+Subject: [PATCH 2/2] libion: Add MediaTek ION special heap and command
+
+Add Mediatek ion heap and command.
+ION_HEAP_TYPE_MULTIMEDIA: mm heap which hook the iommu drivers.
+
+Also, we add a custom ioctl to get iova for the userspace buffer.
+Example code about how to use this custom ioctl:
+    ====================
+        struct ion_custom_data custom_data;
+        struct ion_sys_data sys_data = {0};
+        int ret;
+
+        sys_data.sys_cmd = ION_SYS_GET_PHYS;
+        sys_data.get_phys_param.handle = ion_handle;
+        /* ion handle in userspace */
+
+        custom_data.cmd = ION_CMD_SYSTEM;
+        custom_data.arg = (unsigned long) &sys_data;
+
+        ret = ioctl(fd, ION_IOC_CUSTOM, &custom_data);
+        if (ret < 0) {
+            printf("ion_custom_ioctl %x failed with code %d: %s\n",
+                ION_IOC_CUSTOM, ret, strerror(errno));
+            return -errno;
+        }
+        /* Then you could get the iova in sys_data.get_phys_param.phy_addr */
+        /* In order to avoid change the original libion interface, we don't
+           enclose this to a new interface.*/
+    ====================
+And, Don't forget to "close" the shared fd.
+
+Signed-off-by: Yong Wu <yong.wu@mediatek.com>
+---
+ libion/kernel-headers/linux/ion_mtk.h | 211 ++++++++++++++++++++++++++++++++++
+ 1 file changed, 211 insertions(+)
+ create mode 100644 libion/kernel-headers/linux/ion_mtk.h
+
+diff --git a/libion/kernel-headers/linux/ion_mtk.h b/libion/kernel-headers/linux/ion_mtk.h
+new file mode 100644
+index 0000000..e22c5ca
+--- /dev/null
++++ b/libion/kernel-headers/linux/ion_mtk.h
+@@ -0,0 +1,211 @@
++/*
++* Copyright (C) 2017 MediaTek.
++*
++* Licensed under the Apache License, Version 2.0 (the "License");
++* you may not use this file except in compliance with the License.
++* You may obtain a copy of the License at
++*
++*      http://www.apache.org/licenses/LICENSE-2.0
++*
++* Unless required by applicable law or agreed to in writing, software
++* distributed under the License is distributed on an "AS IS" BASIS,
++* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
++* See the License for the specific language governing permissions and
++* limitations under the License.
++*/
++
++#ifndef _MTK_MM_ION_H
++#define _MTK_MM_ION_H
++
++enum mtk_ion_heap_type {
++	ION_HEAP_TYPE_DMA_MTK = 9,
++	ION_HEAP_TYPE_MULTIMEDIA = 10,
++	ION_HEAP_TYPE_FB = 11,
++	ION_HEAP_TYPE_MULTIMEDIA_FOR_CAMERA = 12,
++	ION_HEAP_TYPE_MULTIMEDIA_SEC = 13,
++	ION_HEAP_TYPE_MULTIMEDIA_MAP_MVA = 14,
++};
++
++#define ION_HEAP_DMA_MTK_MASK		(1 << ION_HEAP_TYPE_DMA_MTK)
++#define ION_HEAP_MULTIMEDIA_MASK	(1 << ION_HEAP_TYPE_MULTIMEDIA)
++#define ION_HEAP_FB_MASK		(1 << ION_HEAP_TYPE_FB)
++#define ION_HEAP_CAMERA_MASK		(1 << ION_HEAP_TYPE_MULTIMEDIA_FOR_CAMERA)
++#define ION_HEAP_MULTIMEDIA_SEC_MASK BIT (1 << ION_HEAP_TYPE_MULTIMEDIA_SEC)
++#define ION_HEAP_MULTIMEDIA_MAP_MVA_MASK (1 << ION_HEAP_TYPE_MULTIMEDIA_MAP_MVA)
++
++
++enum ION_CMDS {
++	ION_CMD_SYSTEM,
++	ION_CMD_MULTIMEDIA,
++	ION_CMD_MULTIMEDIA_SEC
++};
++
++enum ION_MM_CMDS {
++	ION_MM_CONFIG_BUFFER,
++	ION_MM_SET_DEBUG_INFO,
++	ION_MM_GET_DEBUG_INFO,
++	ION_MM_SET_SF_BUF_INFO,
++	ION_MM_GET_SF_BUF_INFO,
++	ION_MM_CONFIG_BUFFER_EXT
++};
++
++enum ION_SYS_CMDS {
++	ION_SYS_CACHE_SYNC,
++	ION_SYS_GET_PHYS,
++	ION_SYS_GET_CLIENT,
++	ION_SYS_SET_HANDLE_BACKTRACE,
++	ION_SYS_SET_CLIENT_NAME,
++	ION_SYS_DMA_OP,
++};
++
++enum ION_CACHE_SYNC_TYPE {
++	ION_CACHE_CLEAN_BY_RANGE,
++	ION_CACHE_INVALID_BY_RANGE,
++	ION_CACHE_FLUSH_BY_RANGE,
++	ION_CACHE_CLEAN_BY_RANGE_USE_VA,
++	ION_CACHE_INVALID_BY_RANGE_USE_VA,
++	ION_CACHE_FLUSH_BY_RANGE_USE_VA,
++	ION_CACHE_CLEAN_ALL,
++	ION_CACHE_INVALID_ALL,
++	ION_CACHE_FLUSH_ALL
++};
++
++enum ION_ERRORE {
++	ION_ERROR_CONFIG_LOCKED = 0x10000
++};
++
++/* mm or mm_sec heap flag which is do not conflist with ION_HEAP_FLAG_DEFER_FREE */
++#define ION_FLAG_MM_HEAP_INIT_ZERO (1 << 16)
++#define ION_FLAG_MM_HEAP_SEC_PA (1 << 18)
++
++#define ION_FLAG_GET_FIXED_PHYS 0x103
++
++struct ion_sys_cache_sync_param {
++	union {
++		ion_user_handle_t handle;
++		void *kernel_handle;
++	};
++	void *va;
++	unsigned int size;
++	enum ION_CACHE_SYNC_TYPE sync_type;
++};
++
++enum ION_DMA_TYPE {
++	ION_DMA_MAP_AREA,
++	ION_DMA_UNMAP_AREA,
++	ION_DMA_MAP_AREA_VA,
++	ION_DMA_UNMAP_AREA_VA,
++	ION_DMA_FLUSH_BY_RANGE,
++	ION_DMA_FLUSH_BY_RANGE_USE_VA,
++	ION_DMA_CACHE_FLUSH_ALL
++};
++
++enum ION_DMA_DIR {
++	ION_DMA_FROM_DEVICE,
++	ION_DMA_TO_DEVICE,
++	ION_DMA_BIDIRECTIONAL,
++};
++
++struct ion_dma_param {
++	union {
++		ion_user_handle_t handle;
++		void *kernel_handle;
++	};
++	void *va;
++	unsigned int size;
++	enum ION_DMA_TYPE dma_type;
++	enum ION_DMA_DIR dma_dir;
++};
++
++struct ion_sys_get_phys_param {
++	union {
++		ion_user_handle_t handle;
++		void *kernel_handle;
++	};
++	unsigned int phy_addr;
++	unsigned long len;
++};
++
++#define ION_MM_DBG_NAME_LEN 16
++#define ION_MM_SF_BUF_INFO_LEN 16
++
++struct ion_sys_client_name {
++	char name[ION_MM_DBG_NAME_LEN];
++};
++
++struct ion_sys_get_client_param {
++	unsigned int client;
++};
++
++#define BACKTRACE_SIZE 10
++
++struct ion_sys_record_param {
++	pid_t group_id;
++	pid_t pid;
++	unsigned int action;
++	unsigned int address_type;
++	unsigned int address;
++	unsigned int length;
++	unsigned int backtrace[BACKTRACE_SIZE];
++	unsigned int backtrace_num;
++	struct ion_handle *handle;
++	struct ion_client *client;
++	struct ion_buffer *buffer;
++	struct file *file;
++	int fd;
++};
++
++struct ion_sys_data {
++	enum ION_SYS_CMDS sys_cmd;
++	union {
++		struct ion_sys_cache_sync_param cache_sync_param;
++		struct ion_sys_get_phys_param get_phys_param;
++		struct ion_sys_get_client_param get_client_param;
++		struct ion_sys_client_name client_name_param;
++		struct ion_sys_record_param record_param;
++		struct ion_dma_param dma_param;
++	};
++};
++
++struct ion_mm_config_buffer_param {
++	union {
++		ion_user_handle_t handle;
++		void *kernel_handle;
++	};
++	int module_id;
++	unsigned int security;
++	unsigned int coherent;
++	unsigned int reserve_iova_start;
++	unsigned int reserve_iova_end;
++};
++
++struct ion_mm_buf_debug_info {
++	union {
++		ion_user_handle_t handle;
++		void *kernel_handle;
++	};
++	char dbg_name[ION_MM_DBG_NAME_LEN];
++	unsigned int value1;
++	unsigned int value2;
++	unsigned int value3;
++	unsigned int value4;
++};
++
++struct ion_mm_sf_buf_info {
++	union {
++		ion_user_handle_t handle;
++		void *kernel_handle;
++	};
++	unsigned int info[ION_MM_SF_BUF_INFO_LEN];
++};
++
++struct ion_mm_data {
++	enum ION_MM_CMDS mm_cmd;
++	union {
++		struct ion_mm_config_buffer_param config_buffer_param;
++		struct ion_mm_buf_debug_info buf_debug_info_param;
++		struct ion_mm_sf_buf_info sf_buf_info_param;
++	};
++};
++
++#endif
+-- 
+1.9.1
+
diff --git a/debian/patches/series b/debian/patches/series
new file mode 100644
index 0000000..a08827a
--- /dev/null
+++ b/debian/patches/series
@@ -0,0 +1,2 @@
+0002-libion-Add-MediaTek-ION-special-heap-and-command.patch
+0001-libion-Add-makefile.patch
diff --git a/debian/rules b/debian/rules
new file mode 100755
index 0000000..ac1bd47
--- /dev/null
+++ b/debian/rules
@@ -0,0 +1,23 @@
+#!/usr/bin/make -f
+# -*- makefile -*-
+
+# Uncomment this to turn on verbose mode.
+# export DH_VERBOSE=1
+
+LIBION_DESTDIR := $(CURDIR)/debian/tmp/libion-android
+
+CROSS_COMPILE=aarch64-linux-gnu-
+CC=$(CROSS_COMPILE)gcc
+
+%:
+	dh $@
+
+override_dh_auto_build:
+	+make -C libion CC=$(CC)
+
+override_dh_auto_install:
+	+make -C libion install \
+		SRCDIR=$(CURDIR) \
+		DESTDIR=$(LIBION_DESTDIR) \
+		INCDIR=/usr/include \
+		LIBDIR=/usr/lib
diff --git a/debian/source/format b/debian/source/format
new file mode 100644
index 0000000..163aaf8
--- /dev/null
+++ b/debian/source/format
@@ -0,0 +1 @@
+3.0 (quilt)