Skip to content

Commit

Permalink
xtest/sdp: sync with ION udpates from LSK 4.9/ion
Browse files Browse the repository at this point in the history
Sync with updated ION driver from Linaro 4.9 ION topic branch (branch
v4.9/topic/ion) merge aug'17:
- Userland should query ION for the ID of the target heap.
- Ioctl ION_IOC_ALLOC straight returns a dmabuf file descriptor.
- ION buffer is freed once its file descriptor is closed.

Signed-off-by: Etienne Carriere <etienne.carriere@linaro.org>
  • Loading branch information
etienne-lms committed Aug 14, 2017
1 parent e0ab180 commit bce6c3d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 122 deletions.
106 changes: 7 additions & 99 deletions host/xtest/include/uapi/linux/ion.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,7 +28,8 @@ typedef int ion_user_handle_t;
* 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
Expand Down Expand Up @@ -61,12 +60,6 @@ enum ion_heap_type {
*/
#define ION_FLAG_CACHED 1

/*
* mappings of this buffer will created at mmap time, if this is set
* caches must be managed manually
*/
#define ION_FLAG_CACHED_NEEDS_SYNC 2

/**
* DOC: Ion Userspace API
*
Expand All @@ -78,7 +71,6 @@ enum ion_heap_type {
/**
* 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
Expand All @@ -87,47 +79,11 @@ enum ion_heap_type {
* 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
Expand Down Expand Up @@ -178,54 +134,6 @@ struct ion_heap_query {
*/
#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
* 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)

/**
* 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
*
Expand Down
59 changes: 36 additions & 23 deletions host/xtest/sdp_basic.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,49 +76,62 @@ struct tee_ctx {
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");
verbose("linux kernel configuration.\n");
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;
Expand Down

0 comments on commit bce6c3d

Please sign in to comment.