From 45d4da355cbe5064ab2f60a007c022f15d30ae02 Mon Sep 17 00:00:00 2001 From: Hridya Valsaraju Date: Wed, 2 Oct 2019 21:16:57 -0700 Subject: [PATCH 1/4] create ion_4.19.h system/core/libion/original-kernel-headers/linux/ion_4.19.h was created manually and the following command was run to generate system/core/libion/ion_4.19.h bionic/libc/kernel/tools/clean_header.py -ksystem/core/libion/original-kernel-headers/linux ion_4.19.h > system/core/libion/ion_4.19.h Test: make Bug: 140916230 Change-Id: I2cd8cda903e77374cf85f38fe16a4e82156d5c67 --- libion/ion.c | 2 +- libion/ion_4.19.h | 51 +++++++ .../original-kernel-headers/linux/ion_4.19.h | 132 ++++++++++++++++++ 3 files changed, 184 insertions(+), 1 deletion(-) create mode 100644 libion/ion_4.19.h create mode 100644 libion/original-kernel-headers/linux/ion_4.19.h diff --git a/libion/ion.c b/libion/ion.c index 1ecfc785a..55979fa25 100644 --- a/libion/ion.c +++ b/libion/ion.c @@ -31,7 +31,7 @@ #include #include -#include "ion_4.12.h" +#include "ion_4.19.h" #include diff --git a/libion/ion_4.19.h b/libion/ion_4.19.h new file mode 100644 index 000000000..cc6ea9edc --- /dev/null +++ b/libion/ion_4.19.h @@ -0,0 +1,51 @@ +/**************************************************************************** + **************************************************************************** + *** + *** 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_NEW_H +#define _UAPI_LINUX_ION_NEW_H +#include +#include +#define ION_NUM_HEAP_IDS (sizeof(unsigned int) * 8) +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 { + char name[MAX_HEAP_NAME]; + __u32 type; + __u32 heap_id; + __u32 reserved0; + __u32 reserved1; + __u32 reserved2; +}; +struct ion_heap_query { + __u32 cnt; + __u32 reserved0; + __u64 heaps; + __u32 reserved1; + __u32 reserved2; +}; +#define ION_IOC_MAGIC 'I' +#define ION_IOC_NEW_ALLOC _IOWR(ION_IOC_MAGIC, 0, struct ion_new_allocation_data) +#define ION_IOC_HEAP_QUERY _IOWR(ION_IOC_MAGIC, 8, struct ion_heap_query) +#define ION_IOC_ABI_VERSION _IOR(ION_IOC_MAGIC, 9, __u32) +#endif diff --git a/libion/original-kernel-headers/linux/ion_4.19.h b/libion/original-kernel-headers/linux/ion_4.19.h new file mode 100644 index 000000000..5fdac0d13 --- /dev/null +++ b/libion/original-kernel-headers/linux/ion_4.19.h @@ -0,0 +1,132 @@ +/* + * Adapted from drivers/staging/android/uapi/ion.h + * + * Copyright (C) 2019 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 +#include + +#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) + +/** + * DOC: ION_IOC_HEAP_ABI_VERSION - return ABI version + * + * Returns ABI version for this driver + */ +#define ION_IOC_ABI_VERSION _IOR(ION_IOC_MAGIC, 9, __u32) + +#endif /* _UAPI_LINUX_ION_NEW_H */ From d43e79855301e3039a88bace941c66b02645f451 Mon Sep 17 00:00:00 2001 From: Hridya Valsaraju Date: Fri, 6 Sep 2019 12:20:32 -0700 Subject: [PATCH 2/4] Add libion API to check if modular heaps are being used Bug: 140916230 Test: ./ion-unit-tests Change-Id: Iabb050a70d9a838f2720c509df4017d6031ce1fe --- libion/include/ion/ion.h | 1 + libion/ion.c | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/libion/include/ion/ion.h b/libion/include/ion/ion.h index a60d24eaf..1480bd965 100644 --- a/libion/include/ion/ion.h +++ b/libion/include/ion/ion.h @@ -49,6 +49,7 @@ int ion_query_heap_cnt(int fd, int* cnt); int ion_query_get_heaps(int fd, int cnt, void* buffers); int ion_is_legacy(int fd); +int ion_is_using_modular_heaps(int fd); __END_DECLS diff --git a/libion/ion.c b/libion/ion.c index 55979fa25..5141ea874 100644 --- a/libion/ion.c +++ b/libion/ion.c @@ -35,6 +35,8 @@ #include +#define ION_ABI_VERSION_MODULAR_HEAPS 2 + enum ion_version { ION_VERSION_UNKNOWN, ION_VERSION_MODERN, ION_VERSION_LEGACY }; static atomic_int g_ion_version = ATOMIC_VAR_INIT(ION_VERSION_UNKNOWN); @@ -75,6 +77,14 @@ static int ion_ioctl(int fd, int req, void* arg) { return ret; } +int ion_is_using_modular_heaps(int fd) { + int ion_abi_version = 0; + int ret = 0; + + ret = ion_ioctl(fd, ION_IOC_ABI_VERSION, &ion_abi_version); + return (ret == 0 && ion_abi_version >= ION_ABI_VERSION_MODULAR_HEAPS); +} + int ion_alloc(int fd, size_t len, size_t align, unsigned int heap_mask, unsigned int flags, ion_user_handle_t* handle) { int ret = 0; From 25060535fac5ec166cd35a76a6a470ea8f7c492f Mon Sep 17 00:00:00 2001 From: Hridya Valsaraju Date: Thu, 3 Oct 2019 10:51:21 -0700 Subject: [PATCH 3/4] Add a test to invoke ion_is_using_modular_heaps() Bug: 140916230 Test: ./ion-unit-tests Change-Id: I0204b72e49f34d99df96b46229dda2e958a59bee --- libion/tests/Android.bp | 1 + libion/tests/modular_heap_check.cpp | 30 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 libion/tests/modular_heap_check.cpp diff --git a/libion/tests/Android.bp b/libion/tests/Android.bp index 560070291..989e0294c 100644 --- a/libion/tests/Android.bp +++ b/libion/tests/Android.bp @@ -29,5 +29,6 @@ cc_test { "invalid_values_test.cpp", "ion_test_fixture.cpp", "map_test.cpp", + "modular_heap_check.cpp", ], } diff --git a/libion/tests/modular_heap_check.cpp b/libion/tests/modular_heap_check.cpp new file mode 100644 index 000000000..5505c5a65 --- /dev/null +++ b/libion/tests/modular_heap_check.cpp @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include +#include "ion_test_fixture.h" + +class ModularHeapCheck : public IonTest {}; + +TEST_F(ModularHeapCheck, ModularHeapCheckSimple) { + if (ion_is_using_modular_heaps(ionfd)) { + std::cout << "Heaps are modular." << std::endl; + } else { + std::cout << "Heaps are built-in." << std::endl; + } +} From dbc80cc7efa2d816f8c3257f41138663516d4407 Mon Sep 17 00:00:00 2001 From: Hridya Valsaraju Date: Fri, 4 Oct 2019 10:34:05 -0700 Subject: [PATCH 4/4] Update ion_4.19.h with heap IDs system/core/libion/original-kernel-headers/linux/ion_4.19.h was edited manually and the following command was run to generate system/core/libion/ion_4.19.h bionic/libc/kernel/tools/clean_header.py -ksystem/core/libion/original-kernel-headers/linux ion_4.19.h > system/core/libion/ion_4.19.h Bug: 140916230 Test: make, ion-unit-tests Change-Id: I6b66bed7e3b11c5b7e80bb0b937d620510f0e172 --- libion/ion_4.19.h | 16 ++++++++ .../original-kernel-headers/linux/ion_4.19.h | 38 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/libion/ion_4.19.h b/libion/ion_4.19.h index cc6ea9edc..f5b59f1d7 100644 --- a/libion/ion_4.19.h +++ b/libion/ion_4.19.h @@ -21,6 +21,22 @@ #include #include #define ION_NUM_HEAP_IDS (sizeof(unsigned int) * 8) +enum ion_heap_type_ext { + ION_HEAP_TYPE_CUSTOM_EXT = 16, + ION_HEAP_TYPE_MAX = 31, +}; +enum ion_heap_id { + ION_HEAP_SYSTEM = (1 << ION_HEAP_TYPE_SYSTEM), + ION_HEAP_SYSTEM_CONTIG = (ION_HEAP_SYSTEM << 1), + ION_HEAP_CARVEOUT_START = (ION_HEAP_SYSTEM_CONTIG << 1), + ION_HEAP_CARVEOUT_END = (ION_HEAP_CARVEOUT_START << 4), + ION_HEAP_CHUNK = (ION_HEAP_CARVEOUT_END << 1), + ION_HEAP_DMA_START = (ION_HEAP_CHUNK << 1), + ION_HEAP_DMA_END = (ION_HEAP_DMA_START << 7), + ION_HEAP_CUSTOM_START = (ION_HEAP_DMA_END << 1), + ION_HEAP_CUSTOM_END = (ION_HEAP_CUSTOM_START << 15), +}; +#define ION_NUM_MAX_HEAPS (32) struct ion_new_allocation_data { __u64 len; __u32 heap_id_mask; diff --git a/libion/original-kernel-headers/linux/ion_4.19.h b/libion/original-kernel-headers/linux/ion_4.19.h index 5fdac0d13..75fef39ba 100644 --- a/libion/original-kernel-headers/linux/ion_4.19.h +++ b/libion/original-kernel-headers/linux/ion_4.19.h @@ -22,6 +22,44 @@ #define ION_NUM_HEAP_IDS (sizeof(unsigned int) * 8) +enum ion_heap_type_ext { + ION_HEAP_TYPE_CUSTOM_EXT = 16, + ION_HEAP_TYPE_MAX = 31, +}; + +/** + * ion_heap_id - list of standard heap ids that Android can use + * + * @ION_HEAP_SYSTEM Id for the ION_HEAP_TYPE_SYSTEM + * @ION_HEAP_SYSTEM_CONTIG Id for the ION_HEAP_TYPE_SYSTEM_CONTIG + * @ION_HEAP_CHUNK Id for the ION_HEAP_TYPE_CHUNK + * @ION_HEAP_CARVEOUT_START Start of reserved id range for heaps of type + * ION_HEAP_TYPE_CARVEOUT + * @ION_HEAP_CARVEOUT_END End of reserved id range for heaps of type + * ION_HEAP_TYPE_CARVEOUT + * @ION_HEAP_DMA_START Start of reserved id range for heaps of type + * ION_HEAP_TYPE_DMA + * @ION_HEAP_DMA_END End of reserved id range for heaps of type + * ION_HEAP_TYPE_DMA + * @ION_HEAP_CUSTOM_START Start of reserved id range for heaps of custom + * type + * @ION_HEAP_CUSTOM_END End of reserved id range for heaps of custom + * type + */ +enum ion_heap_id { + ION_HEAP_SYSTEM = (1 << ION_HEAP_TYPE_SYSTEM), + ION_HEAP_SYSTEM_CONTIG = (ION_HEAP_SYSTEM << 1), + ION_HEAP_CARVEOUT_START = (ION_HEAP_SYSTEM_CONTIG << 1), + ION_HEAP_CARVEOUT_END = (ION_HEAP_CARVEOUT_START << 4), + ION_HEAP_CHUNK = (ION_HEAP_CARVEOUT_END << 1), + ION_HEAP_DMA_START = (ION_HEAP_CHUNK << 1), + ION_HEAP_DMA_END = (ION_HEAP_DMA_START << 7), + ION_HEAP_CUSTOM_START = (ION_HEAP_DMA_END << 1), + ION_HEAP_CUSTOM_END = (ION_HEAP_CUSTOM_START << 15), +}; + +#define ION_NUM_MAX_HEAPS (32) + /** * DOC: Ion Userspace API *