angelsl | ee21f4b | 2013-02-18 18:48:53 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Samsung Electronics Co., Ltd. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "libsecion" |
| 18 | |
| 19 | #include <secion.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <sys/mman.h> |
| 22 | #include <sys/ioctl.h> |
| 23 | #include <cutils/log.h> |
| 24 | |
| 25 | #define ION_IOC_MAGIC 'I' |
| 26 | #define ION_IOC_ALLOC _IOWR(ION_IOC_MAGIC, 0, struct ion_allocation_data) |
| 27 | #define ION_IOC_FREE _IOWR(ION_IOC_MAGIC, 1, struct ion_handle_data) |
| 28 | #define ION_IOC_MAP _IOWR(ION_IOC_MAGIC, 2, struct ion_fd_data) |
| 29 | #define ION_IOC_SHARE _IOWR(ION_IOC_MAGIC, 4, struct ion_fd_data) |
| 30 | #define ION_IOC_IMPORT _IOWR(ION_IOC_MAGIC, 5, int) |
| 31 | #define ION_IOC_CUSTOM _IOWR(ION_IOC_MAGIC, 6, struct ion_custom_data) |
| 32 | |
| 33 | typedef unsigned long ion_handle; |
| 34 | |
| 35 | struct ion_allocation_data { |
| 36 | size_t len; |
| 37 | size_t align; |
| 38 | unsigned int flags; |
| 39 | ion_handle *handle; |
| 40 | }; |
| 41 | |
| 42 | struct ion_fd_data { |
| 43 | ion_handle *handle; |
| 44 | int fd; |
| 45 | }; |
| 46 | |
| 47 | struct ion_handle_data { |
| 48 | ion_handle *handle; |
| 49 | }; |
| 50 | |
| 51 | struct ion_custom_data { |
| 52 | unsigned int cmd; |
| 53 | unsigned long arg; |
| 54 | }; |
| 55 | |
| 56 | struct ion_msync_data { |
| 57 | enum ION_MSYNC_FLAGS dir; |
| 58 | int fd; |
| 59 | size_t size; |
| 60 | off_t offset; |
| 61 | }; |
| 62 | |
| 63 | struct ion_phys_data { |
| 64 | int fd; |
| 65 | ion_phys_addr_t phys; |
| 66 | size_t size; |
| 67 | }; |
| 68 | |
| 69 | enum ION_EXYNOS_CUSTOM_CMD { |
| 70 | ION_EXYNOS_CUSTOM_MSYNC, |
| 71 | ION_EXYNOS_CUSTOM_PHYS |
| 72 | }; |
| 73 | |
| 74 | ion_client ion_client_create(void) |
| 75 | { |
| 76 | return open("/dev/ion", O_RDWR); |
| 77 | } |
| 78 | |
| 79 | void ion_client_destroy(ion_client client) |
| 80 | { |
| 81 | close(client); |
| 82 | } |
| 83 | |
| 84 | ion_buffer ion_alloc(ion_client client, size_t len, size_t align, unsigned int flags) |
| 85 | { |
| 86 | int ret; |
| 87 | struct ion_handle_data arg_free; |
| 88 | struct ion_fd_data arg_share; |
| 89 | struct ion_allocation_data arg_alloc; |
| 90 | |
| 91 | arg_alloc.len = len; |
| 92 | arg_alloc.align = align; |
| 93 | arg_alloc.flags = flags; |
| 94 | |
| 95 | ret = ioctl(client, ION_IOC_ALLOC, &arg_alloc); |
| 96 | if (ret < 0) |
| 97 | return ret; |
| 98 | |
| 99 | arg_share.handle = arg_alloc.handle; |
| 100 | ret = ioctl(client, ION_IOC_SHARE, &arg_share); |
| 101 | |
| 102 | arg_free.handle = arg_alloc.handle; |
| 103 | ioctl(client, ION_IOC_FREE, &arg_free); |
| 104 | |
| 105 | if (ret < 0) |
| 106 | return ret; |
| 107 | |
| 108 | return arg_share.fd; |
| 109 | } |
| 110 | |
| 111 | void ion_free(ion_buffer buffer) |
| 112 | { |
| 113 | close(buffer); |
| 114 | } |
| 115 | |
| 116 | void *ion_map(ion_buffer buffer, size_t len, off_t offset) |
| 117 | { |
| 118 | return mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, |
| 119 | buffer, offset); |
| 120 | } |
| 121 | |
| 122 | int ion_unmap(void *addr, size_t len) |
| 123 | { |
| 124 | return munmap(addr, len); |
| 125 | } |
| 126 | |
Javi Ferrer | cc48856 | 2014-11-21 04:05:51 +0530 | [diff] [blame^] | 127 | int ion_msync(ion_client client, ion_buffer buffer, long flags, size_t size, off_t offset) |
angelsl | ee21f4b | 2013-02-18 18:48:53 +0800 | [diff] [blame] | 128 | { |
| 129 | struct ion_msync_data arg_cdata; |
| 130 | arg_cdata.size = size; |
Javi Ferrer | cc48856 | 2014-11-21 04:05:51 +0530 | [diff] [blame^] | 131 | arg_cdata.dir = (ION_MSYNC_FLAGS) flags; |
angelsl | ee21f4b | 2013-02-18 18:48:53 +0800 | [diff] [blame] | 132 | arg_cdata.fd = buffer; |
| 133 | arg_cdata.offset = offset; |
| 134 | |
| 135 | struct ion_custom_data arg_custom; |
| 136 | arg_custom.cmd = ION_EXYNOS_CUSTOM_MSYNC; |
| 137 | arg_custom.arg = (unsigned long) &arg_cdata; |
| 138 | |
| 139 | return ioctl(client, ION_IOC_CUSTOM, &arg_custom); |
| 140 | } |
| 141 | |
| 142 | ion_phys_addr_t ion_getphys(ion_client client, ion_buffer buffer) |
| 143 | { |
| 144 | struct ion_phys_data arg_cdata; |
| 145 | arg_cdata.fd = buffer; |
| 146 | |
| 147 | struct ion_custom_data arg_custom; |
| 148 | arg_custom.cmd = ION_EXYNOS_CUSTOM_PHYS; |
| 149 | arg_custom.arg = (unsigned long) &arg_cdata; |
| 150 | |
| 151 | if(ioctl(client, ION_IOC_CUSTOM, &arg_custom) < 0) |
| 152 | return 0; |
| 153 | |
| 154 | return arg_cdata.phys; |
| 155 | } |
| 156 | |
| 157 | int createIONMem(struct secion_param *param, size_t size, unsigned int flags) |
| 158 | { |
| 159 | if(param->client < 0 && (param->client = ion_client_create()) < 0) { |
| 160 | ALOGE("createIONMem:: ion_client_create fail\n"); |
| 161 | goto fail; |
| 162 | } |
| 163 | |
| 164 | if(param->buffer < 0 && (param->buffer = ion_alloc(param->client, size, 0x10000, flags)) < 0) { |
| 165 | ALOGE("createIONMem:: ion_alloc fail\n"); |
| 166 | goto fail; |
| 167 | } |
| 168 | |
| 169 | if((param->physaddr = ion_getphys(param->client, param->buffer)) == 0) { |
| 170 | ALOGE("createIONMem:: ion_getphys fail, phys_addr = 0\n"); |
| 171 | goto fail; |
| 172 | } |
| 173 | |
| 174 | if((param->memory = ion_map(param->buffer, size, 0)) == (void*)-1) { |
| 175 | ALOGE("createIONMem:: ion_map fail\n"); |
| 176 | goto fail; |
| 177 | } else { |
| 178 | param->size = size; |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | fail: |
| 183 | if(param->memory > 0) munmap(param->memory, size); |
| 184 | if(param->buffer > 0) ion_free(param->buffer); |
| 185 | param->buffer = -1; |
| 186 | param->size = 0; |
| 187 | param->memory = 0; |
| 188 | param->physaddr = 0; |
| 189 | return -1; |
| 190 | } |
| 191 | |
| 192 | int destroyIONMem(struct secion_param *param) |
| 193 | { |
| 194 | if(param->memory != 0) munmap(param->memory, param->size); |
| 195 | if(param->buffer >= 0) ion_free(param->buffer); |
| 196 | param->buffer = -1; |
| 197 | param->size = 0; |
| 198 | param->memory = 0; |
| 199 | param->physaddr = 0; |
| 200 | return 0; |
| 201 | } |