am cd8b953e: Merge changes I2d3fef08,Idb828aa4,Ica764b1f,I9ae8887b,I376fad86,I4d00a9dd

* commit 'cd8b953ede50f68dff5ea049e72aee130dc4a3cb':
  libion: update to latest uapi header
  libion: return -errno from ion_close
  libion: add include for size_t
  libion: add NULL checks
  libion: clean up whitespace
  libion: move ion.h into local include directory
This commit is contained in:
Colin Cross 2013-12-19 13:55:17 -08:00 committed by Android Git Automerger
commit c4f4e5e255
8 changed files with 686 additions and 289 deletions

View file

@ -5,11 +5,14 @@ LOCAL_SRC_FILES := ion.c
LOCAL_MODULE := libion LOCAL_MODULE := libion
LOCAL_MODULE_TAGS := optional LOCAL_MODULE_TAGS := optional
LOCAL_SHARED_LIBRARIES := liblog LOCAL_SHARED_LIBRARIES := liblog
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include $(LOCAL_PATH)/kernel-headers
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include $(LOCAL_PATH)/kernel-headers
include $(BUILD_SHARED_LIBRARY) include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS) include $(CLEAR_VARS)
LOCAL_SRC_FILES := ion.c ion_test.c LOCAL_SRC_FILES := ion.c ion_test.c
LOCAL_MODULE := iontest LOCAL_MODULE := iontest
LOCAL_MODULE_TAGS := optional tests LOCAL_MODULE_TAGS := optional tests
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include $(LOCAL_PATH)/kernel-headers
LOCAL_SHARED_LIBRARIES := liblog LOCAL_SHARED_LIBRARIES := liblog
include $(BUILD_EXECUTABLE) include $(BUILD_EXECUTABLE)

View file

@ -21,6 +21,7 @@
#ifndef __SYS_CORE_ION_H #ifndef __SYS_CORE_ION_H
#define __SYS_CORE_ION_H #define __SYS_CORE_ION_H
#include <sys/types.h>
#include <linux/ion.h> #include <linux/ion.h>
__BEGIN_DECLS __BEGIN_DECLS

View file

@ -40,7 +40,10 @@ int ion_open()
int ion_close(int fd) int ion_close(int fd)
{ {
return close(fd); int ret = close(fd);
if (ret < 0)
return -errno;
return ret;
} }
static int ion_ioctl(int fd, int req, void *arg) static int ion_ioctl(int fd, int req, void *arg)
@ -65,6 +68,9 @@ int ion_alloc(int fd, size_t len, size_t align, unsigned int heap_mask,
.flags = flags, .flags = flags,
}; };
if (handle == NULL)
return -EINVAL;
ret = ion_ioctl(fd, ION_IOC_ALLOC, &data); ret = ion_ioctl(fd, ION_IOC_ALLOC, &data);
if (ret < 0) if (ret < 0)
return ret; return ret;
@ -83,11 +89,17 @@ int ion_free(int fd, ion_user_handle_t handle)
int ion_map(int fd, ion_user_handle_t handle, size_t length, int prot, int ion_map(int fd, ion_user_handle_t handle, size_t length, int prot,
int flags, off_t offset, unsigned char **ptr, int *map_fd) int flags, off_t offset, unsigned char **ptr, int *map_fd)
{ {
int ret;
struct ion_fd_data data = { struct ion_fd_data data = {
.handle = handle, .handle = handle,
}; };
int ret = ion_ioctl(fd, ION_IOC_MAP, &data); if (map_fd == NULL)
return -EINVAL;
if (ptr == NULL)
return -EINVAL;
ret = ion_ioctl(fd, ION_IOC_MAP, &data);
if (ret < 0) if (ret < 0)
return ret; return ret;
*map_fd = data.fd; *map_fd = data.fd;
@ -106,11 +118,15 @@ int ion_map(int fd, ion_user_handle_t handle, size_t length, int prot,
int ion_share(int fd, ion_user_handle_t handle, int *share_fd) int ion_share(int fd, ion_user_handle_t handle, int *share_fd)
{ {
int map_fd; int map_fd;
int ret;
struct ion_fd_data data = { struct ion_fd_data data = {
.handle = handle, .handle = handle,
}; };
int ret = ion_ioctl(fd, ION_IOC_SHARE, &data); if (share_fd == NULL)
return -EINVAL;
ret = ion_ioctl(fd, ION_IOC_SHARE, &data);
if (ret < 0) if (ret < 0)
return ret; return ret;
*share_fd = data.fd; *share_fd = data.fd;
@ -136,11 +152,15 @@ int ion_alloc_fd(int fd, size_t len, size_t align, unsigned int heap_mask,
int ion_import(int fd, int share_fd, ion_user_handle_t *handle) int ion_import(int fd, int share_fd, ion_user_handle_t *handle)
{ {
int ret;
struct ion_fd_data data = { struct ion_fd_data data = {
.fd = share_fd, .fd = share_fd,
}; };
int ret = ion_ioctl(fd, ION_IOC_IMPORT, &data); if (handle == NULL)
return -EINVAL;
ret = ion_ioctl(fd, ION_IOC_IMPORT, &data);
if (ret < 0) if (ret < 0)
return ret; return ret;
*handle = data.handle; *handle = data.handle;

View file

@ -224,21 +224,15 @@ int main(int argc, char* argv[]) {
break; break;
case 'z': case 'z':
map_flags = 0; map_flags = 0;
map_flags |= strstr(optarg, "PROT_EXEC") ? map_flags |= strstr(optarg, "PROT_EXEC") ? PROT_EXEC : 0;
PROT_EXEC : 0; map_flags |= strstr(optarg, "PROT_READ") ? PROT_READ: 0;
map_flags |= strstr(optarg, "PROT_READ") ? map_flags |= strstr(optarg, "PROT_WRITE") ? PROT_WRITE: 0;
PROT_READ: 0; map_flags |= strstr(optarg, "PROT_NONE") ? PROT_NONE: 0;
map_flags |= strstr(optarg, "PROT_WRITE") ?
PROT_WRITE: 0;
map_flags |= strstr(optarg, "PROT_NONE") ?
PROT_NONE: 0;
break; break;
case 'p': case 'p':
prot = 0; prot = 0;
prot |= strstr(optarg, "MAP_PRIVATE") ? prot |= strstr(optarg, "MAP_PRIVATE") ? MAP_PRIVATE : 0;
MAP_PRIVATE : 0; prot |= strstr(optarg, "MAP_SHARED") ? MAP_PRIVATE : 0;
prot |= strstr(optarg, "MAP_SHARED") ?
MAP_PRIVATE : 0;
break; break;
case 'f': case 'f':
alloc_flags = atol(optarg); alloc_flags = atol(optarg);

View file

@ -0,0 +1,78 @@
/****************************************************************************
****************************************************************************
***
*** This header was automatically generated from a Linux kernel header
*** of the same name, to make information necessary for userspace to
*** call into the kernel available to libc. It contains only constants,
*** structures, and macros generated from the original header, and thus,
*** contains no copyrightable information.
***
*** To edit the content of this header, modify the corresponding
*** source file (e.g. under external/kernel-headers/original/) then
*** run bionic/libc/kernel/tools/update_all.py
***
*** Any manual change here will be lost the next time this script will
*** be run. You've been warned!
***
****************************************************************************
****************************************************************************/
#ifndef _UAPI_LINUX_ION_H
#define _UAPI_LINUX_ION_H
#include <linux/ioctl.h>
#include <linux/types.h>
/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
typedef int ion_user_handle_t;
enum ion_heap_type {
ION_HEAP_TYPE_SYSTEM,
ION_HEAP_TYPE_SYSTEM_CONTIG,
/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
ION_HEAP_TYPE_CARVEOUT,
ION_HEAP_TYPE_CHUNK,
ION_HEAP_TYPE_DMA,
ION_HEAP_TYPE_CUSTOM,
/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
ION_NUM_HEAPS = 16,
};
#define ION_HEAP_SYSTEM_MASK (1 << ION_HEAP_TYPE_SYSTEM)
#define ION_HEAP_SYSTEM_CONTIG_MASK (1 << ION_HEAP_TYPE_SYSTEM_CONTIG)
/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
#define ION_HEAP_CARVEOUT_MASK (1 << ION_HEAP_TYPE_CARVEOUT)
#define ION_HEAP_TYPE_DMA_MASK (1 << ION_HEAP_TYPE_DMA)
#define ION_NUM_HEAP_IDS sizeof(unsigned int) * 8
#define ION_FLAG_CACHED 1
/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
#define ION_FLAG_CACHED_NEEDS_SYNC 2
struct ion_allocation_data {
size_t len;
size_t align;
/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
unsigned int heap_id_mask;
unsigned int flags;
ion_user_handle_t handle;
};
/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
struct ion_fd_data {
ion_user_handle_t handle;
int fd;
};
/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
struct ion_handle_data {
ion_user_handle_t handle;
};
struct ion_custom_data {
/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
unsigned int cmd;
unsigned long arg;
};
#define ION_IOC_MAGIC 'I'
/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
#define ION_IOC_ALLOC _IOWR(ION_IOC_MAGIC, 0, struct ion_allocation_data)
#define ION_IOC_FREE _IOWR(ION_IOC_MAGIC, 1, struct ion_handle_data)
#define ION_IOC_MAP _IOWR(ION_IOC_MAGIC, 2, struct ion_fd_data)
#define ION_IOC_SHARE _IOWR(ION_IOC_MAGIC, 4, struct ion_fd_data)
/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
#define ION_IOC_IMPORT _IOWR(ION_IOC_MAGIC, 5, struct ion_fd_data)
#define ION_IOC_SYNC _IOWR(ION_IOC_MAGIC, 7, struct ion_fd_data)
#define ION_IOC_CUSTOM _IOWR(ION_IOC_MAGIC, 6, struct ion_custom_data)
#endif
/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */

View file

@ -0,0 +1,36 @@
/****************************************************************************
****************************************************************************
***
*** This header was automatically generated from a Linux kernel header
*** of the same name, to make information necessary for userspace to
*** call into the kernel available to libc. It contains only constants,
*** structures, and macros generated from the original header, and thus,
*** contains no copyrightable information.
***
*** To edit the content of this header, modify the corresponding
*** source file (e.g. under external/kernel-headers/original/) then
*** run bionic/libc/kernel/tools/update_all.py
***
*** Any manual change here will be lost the next time this script will
*** be run. You've been warned!
***
****************************************************************************
****************************************************************************/
#ifndef _UAPI_LINUX_ION_TEST_H
#define _UAPI_LINUX_ION_TEST_H
#include <linux/ioctl.h>
#include <linux/types.h>
/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
struct ion_test_rw_data {
__u64 ptr;
__u64 offset;
__u64 size;
/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
int write;
};
#define ION_IOC_MAGIC 'I'
#define ION_IOC_TEST_SET_FD _IO(ION_IOC_MAGIC, 0xf0)
/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
#define ION_IOC_TEST_DMA_MAPPING _IOW(ION_IOC_MAGIC, 0xf1, struct ion_test_rw_data)
#define ION_IOC_TEST_KERNEL_MAPPING _IOW(ION_IOC_MAGIC, 0xf2, struct ion_test_rw_data)
#endif

View file

@ -0,0 +1,196 @@
/*
* 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_H
#define _UAPI_LINUX_ION_H
#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
* @ION_HEAP_TYPE_SYSTEM_CONTIG: memory allocated via kmalloc
* @ION_HEAP_TYPE_CARVEOUT: memory allocated from a prereserved
* carveout heap, allocations are physically
* contiguous
* @ION_HEAP_TYPE_DMA: memory allocated via DMA API
* @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
*/
enum ion_heap_type {
ION_HEAP_TYPE_SYSTEM,
ION_HEAP_TYPE_SYSTEM_CONTIG,
ION_HEAP_TYPE_CARVEOUT,
ION_HEAP_TYPE_CHUNK,
ION_HEAP_TYPE_DMA,
ION_HEAP_TYPE_CUSTOM, /* must be last so device specific heaps always
are at the end of this enum */
ION_NUM_HEAPS = 16,
};
#define ION_HEAP_SYSTEM_MASK (1 << ION_HEAP_TYPE_SYSTEM)
#define ION_HEAP_SYSTEM_CONTIG_MASK (1 << ION_HEAP_TYPE_SYSTEM_CONTIG)
#define ION_HEAP_CARVEOUT_MASK (1 << ION_HEAP_TYPE_CARVEOUT)
#define ION_HEAP_TYPE_DMA_MASK (1 << ION_HEAP_TYPE_DMA)
#define ION_NUM_HEAP_IDS sizeof(unsigned int) * 8
/**
* allocation flags - the lower 16 bits are used by core ion, the upper 16
* bits are reserved for use by the heaps themselves.
*/
#define ION_FLAG_CACHED 1 /* mappings of this buffer should be
cached, ion will do cache
maintenance when the buffer is
mapped for dma */
#define ION_FLAG_CACHED_NEEDS_SYNC 2 /* mappings of this buffer will created
at mmap time, if this is set
caches must be managed manually */
/**
* DOC: Ion Userspace API
*
* create a client by opening /dev/ion
* most operations handled via following ioctls
*
*/
/**
* 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
* refer to this allocation
*
* 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;
};
#define ION_IOC_MAGIC 'I'
/**
* DOC: ION_IOC_ALLOC - allocate memory
*
* Takes an ion_allocation_data struct and returns it with the handle field
* populated with the opaque handle for the allocation.
*/
#define ION_IOC_ALLOC _IOWR(ION_IOC_MAGIC, 0, \
struct ion_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)
/**
* 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 happend 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)
#endif /* _UAPI_LINUX_ION_H */

View file

@ -0,0 +1,69 @@
/*
* 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_TEST_H
#define _UAPI_LINUX_ION_TEST_H
#include <linux/ioctl.h>
#include <linux/types.h>
/**
* struct ion_test_rw_data - metadata passed to the kernel to read handle
* @ptr: a pointer to an area at least as large as size
* @offset: offset into the ion buffer to start reading
* @size: size to read or write
* @write: 1 to write, 0 to read
*/
struct ion_test_rw_data {
__u64 ptr;
__u64 offset;
__u64 size;
int write;
};
#define ION_IOC_MAGIC 'I'
/**
* DOC: ION_IOC_TEST_SET_DMA_BUF - attach a dma buf to the test driver
*
* Attaches a dma buf fd to the test driver. Passing a second fd or -1 will
* release the first fd.
*/
#define ION_IOC_TEST_SET_FD \
_IO(ION_IOC_MAGIC, 0xf0)
/**
* DOC: ION_IOC_TEST_DMA_MAPPING - read or write memory from a handle as DMA
*
* Reads or writes the memory from a handle using an uncached mapping. Can be
* used by unit tests to emulate a DMA engine as close as possible. Only
* expected to be used for debugging and testing, may not always be available.
*/
#define ION_IOC_TEST_DMA_MAPPING \
_IOW(ION_IOC_MAGIC, 0xf1, struct ion_test_rw_data)
/**
* DOC: ION_IOC_TEST_KERNEL_MAPPING - read or write memory from a handle
*
* Reads or writes the memory from a handle using a kernel mapping. Can be
* used by unit tests to test heap map_kernel functions. Only expected to be
* used for debugging and testing, may not always be available.
*/
#define ION_IOC_TEST_KERNEL_MAPPING \
_IOW(ION_IOC_MAGIC, 0xf2, struct ion_test_rw_data)
#endif /* _UAPI_LINUX_ION_H */