xtest/sdp: sync with ION udpates from linux kernel 4.12

Userland should query ION for the ID of the target heap.

Ioctls ION_IOC_FREE and ION_IOC_SHARE are still defined in the uapi
but no more supported by the kernel driver. Ioctl ION_IOC_ALLOC
returns returns a generic dmabuf file descriptor that references
the target buffer.

Signed-off-by: Etienne Carriere <etienne.carriere@linaro.org>
Tested-by: Jerome Forissier <jerome.forissier@linaro.org> (HiKey)
Acked-by: Jerome Forissier <jerome.forissier@linaro.org>
diff --git a/host/xtest/include/uapi/linux/ion.h b/host/xtest/include/uapi/linux/ion.h
index e7e20ff..4173d81 100644
--- a/host/xtest/include/uapi/linux/ion.h
+++ b/host/xtest/include/uapi/linux/ion.h
@@ -20,8 +20,6 @@
 #include <linux/ioctl.h>
 #include <linux/types.h>
 
-typedef int ion_user_handle_t;
-
 /**
  * enum ion_heap_types - list of all possible types of heaps
  * @ION_HEAP_TYPE_SYSTEM:	 memory allocated via vmalloc
@@ -30,7 +28,8 @@
  *				 carveout heap, allocations are physically
  *				 contiguous
  * @ION_HEAP_TYPE_DMA:		 memory allocated via DMA API
- * @ION_HEAP_TYPE_UNMAPPED:	 memory not mappable into linux address space
+ * @ION_HEAP_TYPE_UNMAPPED:	 memory not intended to be mapped into the
+ *				 linux address space unless for debug cases
  * @ION_NUM_HEAPS:		 helper for iterating over heaps, a bit mask
  *				 is used to identify the heaps, so only 32
  *				 total heap types are supported
@@ -78,7 +77,6 @@
 /**
  * struct ion_allocation_data - metadata passed from userspace for allocations
  * @len:		size of the allocation
- * @align:		required alignment 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
@@ -87,47 +85,11 @@
  * Provided by userspace as an argument to the ioctl
  */
 struct ion_allocation_data {
-	size_t len;
-	size_t align;
-	unsigned int heap_id_mask;
-	unsigned int flags;
-	ion_user_handle_t handle;
-};
-
-/**
- * struct ion_fd_data - metadata passed to/from userspace for a handle/fd pair
- * @handle:	a handle
- * @fd:		a file descriptor representing that handle
- *
- * For ION_IOC_SHARE or ION_IOC_MAP userspace populates the handle field with
- * the handle returned from ion alloc, and the kernel returns the file
- * descriptor to share or map in the fd field.  For ION_IOC_IMPORT, userspace
- * provides the file descriptor and the kernel returns the handle.
- */
-struct ion_fd_data {
-	ion_user_handle_t handle;
-	int fd;
-};
-
-/**
- * struct ion_handle_data - a handle passed to/from the kernel
- * @handle:	a handle
- */
-struct ion_handle_data {
-	ion_user_handle_t handle;
-};
-
-/**
- * struct ion_custom_data - metadata passed to/from userspace for a custom ioctl
- * @cmd:	the custom ioctl function to call
- * @arg:	additional data to pass to the custom ioctl, typically a user
- *		pointer to a predefined structure
- *
- * This works just like the regular cmd and arg fields of an ioctl.
- */
-struct ion_custom_data {
-	unsigned int cmd;
-	unsigned long arg;
+	__u64 len;
+	__u32 heap_id_mask;
+	__u32 flags;
+	__u32 fd;
+	__u32 unused;
 };
 
 #define MAX_HEAP_NAME			32
@@ -179,16 +141,6 @@
 #define ION_IOC_FREE		_IOWR(ION_IOC_MAGIC, 1, struct ion_handle_data)
 
 /**
- * DOC: ION_IOC_MAP - get a file descriptor to mmap
- *
- * 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 used as an argument to mmap.
- */
-#define ION_IOC_MAP		_IOWR(ION_IOC_MAGIC, 2, struct ion_fd_data)
-
-/**
  * 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
@@ -200,33 +152,6 @@
 #define ION_IOC_SHARE		_IOWR(ION_IOC_MAGIC, 4, struct ion_fd_data)
 
 /**
- * DOC: ION_IOC_IMPORT - imports a shared file descriptor
- *
- * Takes an ion_fd_data struct with the fd field populated with a valid file
- * descriptor obtained from ION_IOC_SHARE and returns the struct with the handle
- * filed set to the corresponding opaque handle.
- */
-#define ION_IOC_IMPORT		_IOWR(ION_IOC_MAGIC, 5, struct ion_fd_data)
-
-/**
- * DOC: ION_IOC_SYNC - syncs a shared file descriptors to memory
- *
- * Deprecated in favor of using the dma_buf api's correctly (syncing
- * will happen automatically when the buffer is mapped to a device).
- * If necessary should be used after touching a cached buffer from the cpu,
- * this will make the buffer in memory coherent.
- */
-#define ION_IOC_SYNC		_IOWR(ION_IOC_MAGIC, 7, struct ion_fd_data)
-
-/**
- * DOC: ION_IOC_CUSTOM - call architecture specific ion ioctl
- *
- * Takes the argument of the architecture specific ioctl to call and
- * passes appropriate userdata for that ioctl
- */
-#define ION_IOC_CUSTOM		_IOWR(ION_IOC_MAGIC, 6, struct ion_custom_data)
-
-/**
  * DOC: ION_IOC_HEAP_QUERY - information about available heaps
  *
  * Takes an ion_heap_query structure and populates information about
diff --git a/host/xtest/sdp_basic.c b/host/xtest/sdp_basic.c
index 7c2a98a..84de4b1 100644
--- a/host/xtest/sdp_basic.c
+++ b/host/xtest/sdp_basic.c
@@ -76,17 +76,18 @@
 	TEEC_Session sess;
 };
 
-int allocate_ion_buffer(size_t size, int heap_id, int verbosity)
+int allocate_ion_buffer(size_t size, int heap_type_id, int verbosity)
 {
+	struct ion_heap_query query_data;
+	struct ion_heap_data heap_data[32];
 	struct ion_allocation_data alloc_data;
-	struct ion_handle_data hdl_data;
-	struct ion_fd_data fd_data;
 	int ion;
 	int fd = -1;
+	unsigned int idx;
 
 	ion = open("/dev/ion", O_RDWR);
 	if (ion < 0) {
-		fprintf(stderr, "Error; failed to open /dev/ion\n");
+		fprintf(stderr, "Error: failed to open /dev/ion\n");
 		verbose("Seems no ION heap is available.\n");
 		verbose("To test ION allocation you can enable\n");
 		verbose("CONFIG_ION and CONFIG_ION_DUMMY in your\n");
@@ -94,31 +95,43 @@
 		return fd;
 	}
 
-	if (heap_id < 0)
-		heap_id = DEFAULT_ION_HEAP_TYPE;
+	if (heap_type_id < 0)
+		heap_type_id = DEFAULT_ION_HEAP_TYPE;
 
-	verbose("Allocate in ION heap '%s'\n",
-		heap_id == ION_HEAP_TYPE_SYSTEM ? "system" :
-		heap_id == ION_HEAP_TYPE_SYSTEM_CONTIG ? "system contig" :
-		heap_id == ION_HEAP_TYPE_CARVEOUT ? "carveout" :
-		heap_id == ION_HEAP_TYPE_CHUNK ? "chunk" :
-		heap_id == ION_HEAP_TYPE_DMA ? "dma" :
-		heap_id == ION_HEAP_TYPE_UNMAPPED ? "unmapped" :
-		"custom");
+	memset(&query_data, 0, sizeof(query_data));
+	if (ioctl(ion, ION_IOC_HEAP_QUERY, &query_data) < 0) {
+		fprintf(stderr, "Error: failed to query the number of heaps\n");
+		goto out;
+	}
+
+	query_data.heaps = (__u64)(unsigned long)&heap_data;
+	if (ioctl(ion, ION_IOC_HEAP_QUERY, &query_data) < 0) {
+		fprintf(stderr, "Error: failed to query heaps data\n");
+		goto out;
+	}
+
+	for (idx = 0; idx < query_data.cnt; idx++)
+		if (heap_data[idx].type == (unsigned int)heap_type_id)
+			break;
+	if (idx == query_data.cnt) {
+		fprintf(stderr, "Error: target heap type %d not found\n",
+				heap_type_id);
+		goto out;
+	}
+
+	verbose("Allocate in ION heap '%s' (type=%u, id=%u)\n",
+		heap_data[idx].name, heap_data[idx].type,
+		heap_data[idx].heap_id);
 
 	alloc_data.len = size;
-	alloc_data.align = 0;
 	alloc_data.flags = 0;
-	alloc_data.heap_id_mask = 1 << heap_id;
-	if (ioctl(ion, ION_IOC_ALLOC, &alloc_data) == -1)
+	alloc_data.heap_id_mask = 1 << heap_data[idx].heap_id;
+	if (ioctl(ion, ION_IOC_ALLOC, &alloc_data) < 0) {
+		fprintf(stderr, "Error: failed to allocate in target heap\n");
 		goto out;
+	}
 
-	fd_data.handle = alloc_data.handle;
-	if (ioctl(ion, ION_IOC_SHARE, &fd_data) != -1)
-		fd = fd_data.fd;
-
-	hdl_data.handle = alloc_data.handle;
-	(void)ioctl(ion, ION_IOC_FREE, &hdl_data);
+	fd = alloc_data.fd;
 out:
 	close(ion);
 	return fd;