Merge "Use -Werror in device/linaro/hikey"
diff --git a/AndroidProducts.mk b/AndroidProducts.mk
index 4ae9e53..f10d1cf 100644
--- a/AndroidProducts.mk
+++ b/AndroidProducts.mk
@@ -11,4 +11,5 @@
 
 PRODUCT_MAKEFILES := \
 $(LOCAL_DIR)/hikey.mk \
+$(LOCAL_DIR)/hikey32.mk \
 $(LOCAL_DIR)/hikey960.mk
diff --git a/factory-images/generate-factory-images-hikey960.sh b/factory-images/generate-factory-images-hikey960.sh
index 9d2b02b..ce27027 100755
--- a/factory-images/generate-factory-images-hikey960.sh
+++ b/factory-images/generate-factory-images-hikey960.sh
@@ -33,12 +33,12 @@
 
 # copy over bootloader binaries
 cp $ANDROID_BUILD_TOP/$DEVICE_DIR/installer/hikey960/bl31.bin tmp/$PRODUCT-$VERSION/
-cp $ANDROID_BUILD_TOP/$DEVICE_DIR/installer/hikey960/fastboot.img tmp/$PRODUCT-$VERSION/
+cp $ANDROID_BUILD_TOP/$DEVICE_DIR/installer/hikey960/hisi-fastboot.img tmp/$PRODUCT-$VERSION/
 cp $ANDROID_BUILD_TOP/$DEVICE_DIR/installer/hikey960/lpm3.img tmp/$PRODUCT-$VERSION/
 cp $ANDROID_BUILD_TOP/$DEVICE_DIR/installer/hikey960/nvme.img tmp/$PRODUCT-$VERSION/
-cp $ANDROID_BUILD_TOP/$DEVICE_DIR/installer/hikey960/ptable.img tmp/$PRODUCT-$VERSION/
+cp $ANDROID_BUILD_TOP/$DEVICE_DIR/installer/hikey960/hisi-ptable.img tmp/$PRODUCT-$VERSION/
 cp $ANDROID_BUILD_TOP/$DEVICE_DIR/installer/hikey960/README tmp/$PRODUCT-$VERSION/
-cp $ANDROID_BUILD_TOP/$DEVICE_DIR/installer/hikey960/sec_xloader.img tmp/$PRODUCT-$VERSION/
+cp $ANDROID_BUILD_TOP/$DEVICE_DIR/installer/hikey960/hisi-sec_xloader.img tmp/$PRODUCT-$VERSION/
 
 # copy over dts.img
 cp $ANDROID_BUILD_TOP/out/target/product/hikey960/dt.img tmp/$PRODUCT-$VERSION/
@@ -72,9 +72,10 @@
 
 
 
-fastboot flash ptable		ptable.img
 fastboot flash xloader		sec_xloader.img
+fastboot flash ptable		ptable.img
 fastboot flash fastboot		fastboot.img
+fastboot reboot-bootloader
 fastboot flash nvme		nvme.img
 fastboot flash fw_lpm3		lpm3.img
 fastboot flash trustfirmware	bl31.bin
diff --git a/gralloc/Android.mk b/gralloc/Android.mk
index 2191b7e..fc62685 100644
--- a/gralloc/Android.mk
+++ b/gralloc/Android.mk
@@ -13,7 +13,6 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-ifeq ($(TARGET_PRODUCT),hikey)
 LOCAL_PATH := $(call my-dir)
 
 # HAL module implemenation, not prelinked and stored in
@@ -34,4 +33,3 @@
 LOCAL_MODULE_RELATIVE_PATH := hw
 
 include $(BUILD_SHARED_LIBRARY)
-endif
diff --git a/gralloc/alloc_device.cpp b/gralloc/alloc_device.cpp
index cef5851..d1377ff 100644
--- a/gralloc/alloc_device.cpp
+++ b/gralloc/alloc_device.cpp
@@ -16,6 +16,7 @@
  * limitations under the License.
  */
 
+#include <cstdlib>
 #include <string.h>
 #include <errno.h>
 #include <pthread.h>
@@ -40,11 +41,11 @@
 #if GRALLOC_ARM_DMA_BUF_MODULE
 #include <linux/ion.h>
 #include <ion/ion.h>
+#include "ion_4.12.h"
 #endif
 
 #define GRALLOC_ALIGN( value, base ) (((value) + ((base) - 1)) & ~((base) - 1))
 
-
 #if GRALLOC_SIMULATE_FAILURES
 #include <cutils/properties.h>
 
@@ -106,34 +107,47 @@
 		int shared_fd;
 		int ret;
 
-		ret = ion_alloc(m->ion_client, size, 0, ION_HEAP_SYSTEM_MASK, 0, &(ion_hnd));
-
-		if (ret != 0)
+		if (m->gralloc_legacy_ion)
 		{
-			AERR("Failed to ion_alloc from ion_client:%d", m->ion_client);
-			return -1;
-		}
+			ret = ion_alloc(m->ion_client, size, 0, ION_HEAP_SYSTEM_MASK, 0, &(ion_hnd));
 
-		ret = ion_share(m->ion_client, ion_hnd, &shared_fd);
-
-		if (ret != 0)
-		{
-			AERR("ion_share( %d ) failed", m->ion_client);
-
-			if (0 != ion_free(m->ion_client, ion_hnd))
+			if (ret != 0)
 			{
-				AERR("ion_free( %d ) failed", m->ion_client);
+				AERR("Failed to ion_alloc from ion_client:%d", m->ion_client);
+				return -1;
 			}
 
-			return -1;
-		}
+			ret = ion_share(m->ion_client, ion_hnd, &shared_fd);
 
-		// we do not need ion_hnd once we have shared_fd
-		if (0 != ion_free(m->ion_client, ion_hnd))
-		{
-			AWAR("ion_free( %d ) failed", m->ion_client);
+			if (ret != 0)
+			{
+				AERR("ion_share( %d ) failed", m->ion_client);
+
+				if (0 != ion_free(m->ion_client, ion_hnd))
+				{
+					AERR("ion_free( %d ) failed", m->ion_client);
+				}
+
+				return -1;
+			}
+
+			// we do not need ion_hnd once we have shared_fd
+			if (0 != ion_free(m->ion_client, ion_hnd))
+			{
+				AWAR("ion_free( %d ) failed", m->ion_client);
+			}
+			ion_hnd = ION_INVALID_HANDLE;
 		}
-		ion_hnd = ION_INVALID_HANDLE;
+		else
+		{
+			ret = ion_alloc_fd(m->ion_client, size, 0, 1 << m->system_heap_id, 0, &(shared_fd));
+
+			if (ret != 0)
+			{
+				AERR("Failed to ion_alloc_fd from ion_client:%d", m->ion_client);
+				return -1;
+			}
+		}
 
 		cpu_ptr = (unsigned char *)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, shared_fd, 0);
 
@@ -597,6 +611,54 @@
 	return 0;
 }
 
+#if GRALLOC_ARM_DMA_BUF_MODULE
+static int find_system_heap_id(int ion_client)
+{
+	int i, ret, cnt, system_heap_id = -1;
+	struct ion_heap_data *data;
+
+	ret = ion_query_heap_cnt(ion_client, &cnt);
+
+	if (ret)
+	{
+		AERR("ion count query failed with %s", strerror(errno));
+		return -1;
+	}
+
+	data = (struct ion_heap_data *)malloc(cnt * sizeof(*data));
+	if (!data)
+	{
+		AERR("Error allocating data %s\n", strerror(errno));
+		return -1;
+	}
+
+	ret = ion_query_get_heaps(ion_client, cnt, data);
+	if (ret)
+	{
+		AERR("Error querying heaps from ion %s", strerror(errno));
+	}
+	else
+	{
+		for (i = 0; i < cnt; i++) {
+			struct ion_heap_data *dat = (struct ion_heap_data *)data;
+			if (strcmp(dat[i].name, "ion_system_heap") == 0) {
+				system_heap_id = dat[i].heap_id;
+				break;
+			}
+		}
+
+		if (i > cnt)
+		{
+			AERR("No System Heap Found amongst %d heaps\n", cnt);
+			system_heap_id = -1;
+		}
+	}
+
+	free(data);
+	return system_heap_id;
+}
+#endif
+
 int alloc_device_open(hw_module_t const *module, const char *name, hw_device_t **device)
 {
 	MALI_IGNORE(name);
@@ -643,6 +705,20 @@
 		return -1;
 	}
 
+	m->gralloc_legacy_ion = ion_is_legacy(m->ion_client);
+
+	if (!m->gralloc_legacy_ion)
+	{
+		m->system_heap_id = find_system_heap_id(m->ion_client);
+		if (m->system_heap_id < 0)
+		{
+			delete dev;
+			ion_close(m->ion_client);
+			m->ion_client = -1;
+			return -1;
+		}
+	}
+
 #endif
 
 	*device = &dev->common;
diff --git a/gralloc/gralloc_priv.h b/gralloc/gralloc_priv.h
index 3615960..582c487 100644
--- a/gralloc/gralloc_priv.h
+++ b/gralloc/gralloc_priv.h
@@ -110,6 +110,8 @@
 	pthread_mutex_t lock;
 	buffer_handle_t currentBuffer;
 	int ion_client;
+	int system_heap_id;
+	bool gralloc_legacy_ion;
 
 	struct fb_var_screeninfo info;
 	struct fb_fix_screeninfo finfo;
diff --git a/gralloc/ion_4.12.h b/gralloc/ion_4.12.h
new file mode 100644
index 0000000..6ae79d4
--- /dev/null
+++ b/gralloc/ion_4.12.h
@@ -0,0 +1,125 @@
+/*
+ * Adapted from drivers/staging/android/uapi/ion.h
+ *
+ * Copyright (C) 2011 Google, Inc.
+ *
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+
+#ifndef _UAPI_LINUX_ION_NEW_H
+#define _UAPI_LINUX_ION_NEW_H
+
+#include <linux/ioctl.h>
+#include <linux/types.h>
+
+#define ION_NUM_HEAP_IDS (sizeof(unsigned int) * 8)
+
+/**
+ * DOC: Ion Userspace API
+ *
+ * create a client by opening /dev/ion
+ * most operations handled via following ioctls
+ *
+ */
+
+/**
+ * struct ion_new_allocation_data - metadata passed from userspace for allocations
+ * @len:		size of the allocation
+ * @heap_id_mask:	mask of heap ids to allocate from
+ * @flags:		flags passed to heap
+ * @handle:		pointer that will be populated with a cookie to use to
+ *			refer to this allocation
+ *
+ * Provided by userspace as an argument to the ioctl - added _new to denote
+ * this belongs to the new ION interface.
+ */
+struct ion_new_allocation_data {
+    __u64 len;
+    __u32 heap_id_mask;
+    __u32 flags;
+    __u32 fd;
+    __u32 unused;
+};
+
+#define MAX_HEAP_NAME 32
+
+/**
+ * struct ion_heap_data - data about a heap
+ * @name - first 32 characters of the heap name
+ * @type - heap type
+ * @heap_id - heap id for the heap
+ */
+struct ion_heap_data {
+    char name[MAX_HEAP_NAME];
+    __u32 type;
+    __u32 heap_id;
+    __u32 reserved0;
+    __u32 reserved1;
+    __u32 reserved2;
+};
+
+/**
+ * struct ion_heap_query - collection of data about all heaps
+ * @cnt - total number of heaps to be copied
+ * @heaps - buffer to copy heap data
+ */
+struct ion_heap_query {
+    __u32 cnt;       /* Total number of heaps to be copied */
+    __u32 reserved0; /* align to 64bits */
+    __u64 heaps;     /* buffer to be populated */
+    __u32 reserved1;
+    __u32 reserved2;
+};
+
+#define ION_IOC_MAGIC 'I'
+
+/**
+ * DOC: ION_IOC_NEW_ALLOC - allocate memory
+ *
+ * Takes an ion_allocation_data struct and returns it with the handle field
+ * populated with the opaque handle for the allocation.
+ * TODO: This IOCTL will clash by design; however, only one of
+ *  ION_IOC_ALLOC or ION_IOC_NEW_ALLOC paths will be exercised,
+ *  so this should not conflict.
+ */
+#define ION_IOC_NEW_ALLOC _IOWR(ION_IOC_MAGIC, 0, struct ion_new_allocation_data)
+
+/**
+ * DOC: ION_IOC_FREE - free memory
+ *
+ * Takes an ion_handle_data struct and frees the handle.
+ *
+ * #define ION_IOC_FREE		_IOWR(ION_IOC_MAGIC, 1, struct ion_handle_data)
+ * This will come from the older kernels, so don't redefine here
+ */
+
+/**
+ * DOC: ION_IOC_SHARE - creates a file descriptor to use to share an allocation
+ *
+ * Takes an ion_fd_data struct with the handle field populated with a valid
+ * opaque handle.  Returns the struct with the fd field set to a file
+ * descriptor open in the current address space.  This file descriptor
+ * can then be passed to another process.  The corresponding opaque handle can
+ * be retrieved via ION_IOC_IMPORT.
+ *
+ * #define ION_IOC_SHARE		_IOWR(ION_IOC_MAGIC, 4, struct ion_fd_data)
+ * This will come from the older kernels, so don't redefine here
+ */
+
+/**
+ * DOC: ION_IOC_HEAP_QUERY - information about available heaps
+ *
+ * Takes an ion_heap_query structure and populates information about
+ * available Ion heaps.
+ */
+#define ION_IOC_HEAP_QUERY _IOWR(ION_IOC_MAGIC, 8, struct ion_heap_query)
+
+#endif /* _UAPI_LINUX_ION_NEW_H */
diff --git a/hikey32.mk b/hikey32.mk
new file mode 100644
index 0000000..a08ee8c
--- /dev/null
+++ b/hikey32.mk
@@ -0,0 +1,10 @@
+# Set zygote32 config before one in hikey.mk
+PRODUCT_DEFAULT_PROPERTY_OVERRIDES += ro.zygote=zygote32
+PRODUCT_COPY_FILES += system/core/rootdir/init.zygote32.rc:root/init.zygote32.rc
+
+$(call inherit-product, device/linaro/hikey/hikey.mk)
+
+#
+# Overrides
+PRODUCT_NAME := hikey32
+PRODUCT_DEVICE := hikey32
diff --git a/hikey32/BoardConfig.mk b/hikey32/BoardConfig.mk
new file mode 100644
index 0000000..9d1fd4f
--- /dev/null
+++ b/hikey32/BoardConfig.mk
@@ -0,0 +1,18 @@
+include device/linaro/hikey/hikey/BoardConfig.mk
+
+TARGET_ARCH := arm
+TARGET_ARCH_VARIANT := armv7-a-neon
+TARGET_CPU_ABI := armeabi-v7a
+TARGET_CPU_ABI2 := armeabi
+TARGET_CPU_VARIANT := generic
+
+TARGET_2ND_ARCH :=
+TARGET_2ND_ARCH_VARIANT :=
+TARGET_2ND_CPU_ABI :=
+TARGET_2ND_CPU_ABI2 :=
+TARGET_2ND_CPU_VARIANT :=
+
+TARGET_SUPPORTS_64_BIT_APPS := false
+
+
+
diff --git a/hikey32/board-info.txt b/hikey32/board-info.txt
new file mode 100644
index 0000000..f054721
--- /dev/null
+++ b/hikey32/board-info.txt
@@ -0,0 +1,2 @@
+require board=hikey
+require version-bootloader=0.4
diff --git a/installer/hikey/fip.bin b/installer/hikey/fip.bin
index 1dfc5fb..0164ad5 100644
--- a/installer/hikey/fip.bin
+++ b/installer/hikey/fip.bin
Binary files differ
diff --git a/installer/hikey/l-loader.bin b/installer/hikey/l-loader.bin
index 70e303e..7f4b1e6 100644
--- a/installer/hikey/l-loader.bin
+++ b/installer/hikey/l-loader.bin
Binary files differ
diff --git a/installer/hikey960/flash-all.sh b/installer/hikey960/flash-all.sh
index 7b7db51..f203c85 100755
--- a/installer/hikey960/flash-all.sh
+++ b/installer/hikey960/flash-all.sh
@@ -15,9 +15,10 @@
 
 echo "android out dir:${ANDROID_PRODUCT_OUT}"
 
-fastboot flash ptable "${INSTALLER_DIR}"/ptable.img
-fastboot flash xloader "${INSTALLER_DIR}"/sec_xloader.img
-fastboot flash fastboot "${INSTALLER_DIR}"/fastboot.img
+fastboot flash xloader "${INSTALLER_DIR}"/hisi-sec_xloader.img
+fastboot flash ptable "${INSTALLER_DIR}"/hisi-ptable.img
+fastboot flash fastboot "${INSTALLER_DIR}"/hisi-fastboot.img
+fastboot reboot-bootloader
 fastboot flash nvme "${INSTALLER_DIR}"/nvme.img
 fastboot flash fw_lpm3   "${INSTALLER_DIR}"/lpm3.img
 fastboot flash trustfirmware   "${INSTALLER_DIR}"/bl31.bin
diff --git a/installer/hikey960/fastboot.img b/installer/hikey960/hisi-fastboot.img
similarity index 100%
rename from installer/hikey960/fastboot.img
rename to installer/hikey960/hisi-fastboot.img
Binary files differ
diff --git a/installer/hikey960/ptable.img b/installer/hikey960/hisi-ptable.img
similarity index 98%
rename from installer/hikey960/ptable.img
rename to installer/hikey960/hisi-ptable.img
index 7c9e36e..3a4fe68 100644
--- a/installer/hikey960/ptable.img
+++ b/installer/hikey960/hisi-ptable.img
Binary files differ
diff --git a/installer/hikey960/sec_xloader.img b/installer/hikey960/hisi-sec_xloader.img
similarity index 100%
rename from installer/hikey960/sec_xloader.img
rename to installer/hikey960/hisi-sec_xloader.img
Binary files differ