hikey: gralloc: Allow use of CMA heap instead of fbdev

This allows the hikey gralloc to use the ion CMA heap instead of
the fbdev device. This is needed to move to the drm_hwcomposer
with FBDEV_EMULATION disabled.

This enables support for both legacy kernels using older ion
interface and 4.12+ and newer kernels with the newer ion
interface.

Change-Id: I87d86d7d4f21a40e5c64c2473aea5bb4c5947de0
Signed-off-by: John Stultz <john.stultz@linaro.org>
diff --git a/gralloc/Android.mk b/gralloc/Android.mk
index 8c41842..23a5a82 100644
--- a/gralloc/Android.mk
+++ b/gralloc/Android.mk
@@ -29,6 +29,7 @@
 MALI_DDK_TEST_PATH := hardware/arm/
 
 LOCAL_MODULE := gralloc.hikey
+LOCAL_MODULE_RELATIVE_PATH := hw
 #LOCAL_MODULE_TAGS := optional
 
 # Mali-200/300/400MP DDK
@@ -49,4 +50,10 @@
 	framebuffer_device.cpp
 
 #LOCAL_CFLAGS+= -DMALI_VSYNC_EVENT_REPORT_ENABLE
+
+
+ifeq ($(HIKEY_USE_DRM_HWCOMPOSER), true)
+LOCAL_CFLAGS += -DDISABLE_FRAMEBUFFER_HAL
+endif
+
 include $(BUILD_SHARED_LIBRARY)
diff --git a/gralloc/alloc_device.cpp b/gralloc/alloc_device.cpp
index 397f95e..b6bd69e 100644
--- a/gralloc/alloc_device.cpp
+++ b/gralloc/alloc_device.cpp
@@ -41,6 +41,10 @@
 #if GRALLOC_ARM_DMA_BUF_MODULE
 #include <ion/ion.h>
 #include "ion_4.12.h"
+
+#define ION_SYSTEM	(char*)"ion_system_heap"
+#define ION_CMA		(char*)"linux,cma"
+
 #endif
 
 #if GRALLOC_SIMULATE_FAILURES
@@ -144,7 +148,10 @@
 
 		if (m->gralloc_legacy_ion)
 		{
-			ret = ion_alloc(m->ion_client, size, 0, ION_HEAP_SYSTEM_MASK, 0, &(ion_hnd));
+			if (usage & GRALLOC_USAGE_HW_FB)
+				ret = ion_alloc(m->ion_client, size, 0, ION_HEAP_TYPE_DMA_MASK, 0, &(ion_hnd));
+			else
+				ret = ion_alloc(m->ion_client, size, 0, ION_HEAP_SYSTEM_MASK, 0, &(ion_hnd));
 
 			if (ret != 0)
 			{
@@ -175,7 +182,10 @@
 		}
 		else
 		{
-			ret = ion_alloc_fd(m->ion_client, size, 0, 1 << m->system_heap_id, 0, &(shared_fd));
+			if (usage & GRALLOC_USAGE_HW_FB)
+				ret = ion_alloc_fd(m->ion_client, size, 0, 1 << m->cma_heap_id, 0, &(shared_fd));
+			else
+				ret = ion_alloc_fd(m->ion_client, size, 0, 1 << m->system_heap_id, 0, &(shared_fd));
 
 			if (ret != 0)
 			{
@@ -318,6 +328,7 @@
 
 }
 
+#ifndef DISABLE_FRAMEBUFFER_HAL
 static int gralloc_alloc_framebuffer_locked(alloc_device_t *dev, size_t size, int usage, buffer_handle_t *pHandle)
 {
 	private_module_t *m = reinterpret_cast<private_module_t *>(dev->common.module);
@@ -427,6 +438,7 @@
 	pthread_mutex_unlock(&m->lock);
 	return err;
 }
+#endif /* DISABLE_FRAMEBUFFER_HAL */
 
 static int alloc_device_alloc(alloc_device_t *dev, int w, int h, int format, int usage, buffer_handle_t *pHandle, int *pStride)
 {
@@ -534,7 +546,7 @@
 
 	int err;
 
-#ifndef MALI_600
+#ifndef DISABLE_FRAMEBUFFER_HAL
 
 	if (usage & GRALLOC_USAGE_HW_FB)
 	{
@@ -681,9 +693,9 @@
 }
 
 #if GRALLOC_ARM_DMA_BUF_MODULE
-static int find_system_heap_id(int ion_client)
+static int find_ion_heap_id(int ion_client, char* name)
 {
-	int i, ret, cnt, system_heap_id = -1;
+	int i, ret, cnt, heap_id = -1;
 	struct ion_heap_data *data;
 
 	ret = ion_query_heap_cnt(ion_client, &cnt);
@@ -710,8 +722,8 @@
 	{
 		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;
+			if (strcmp(dat[i].name, name) == 0) {
+				heap_id = dat[i].heap_id;
 				break;
 			}
 		}
@@ -719,12 +731,12 @@
 		if (i > cnt)
 		{
 			AERR("No System Heap Found amongst %d heaps\n", cnt);
-			system_heap_id = -1;
+			heap_id = -1;
 		}
 	}
 
 	free(data);
-	return system_heap_id;
+	return heap_id;
 }
 #endif
 
@@ -778,8 +790,9 @@
 
 	if (!m->gralloc_legacy_ion)
 	{
-		m->system_heap_id = find_system_heap_id(m->ion_client);
-		if (m->system_heap_id < 0)
+		m->system_heap_id = find_ion_heap_id(m->ion_client, ION_SYSTEM);
+		m->cma_heap_id = find_ion_heap_id(m->ion_client, ION_CMA);
+		if (m->system_heap_id < 0 || m->cma_heap_id < 0)
 		{
 			delete dev;
 			ion_close(m->ion_client);
diff --git a/gralloc/gralloc_priv.h b/gralloc/gralloc_priv.h
index a5f8c8b..3330eae 100644
--- a/gralloc/gralloc_priv.h
+++ b/gralloc/gralloc_priv.h
@@ -113,6 +113,7 @@
 	buffer_handle_t currentBuffer;
 	int ion_client;
 	int system_heap_id;
+	int cma_heap_id;
 	bool gralloc_legacy_ion;
 
 	struct fb_var_screeninfo info;