1 /* 2 * include/linux/ion.h 3 * 4 * Copyright (C) 2011 Google, Inc. 5 * 6 * This software is licensed under the terms of the GNU General Public 7 * License version 2, as published by the Free Software Foundation, and 8 * may be copied, distributed, and modified under those terms. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 */ 16 17 #ifndef _LINUX_ION_H 18 #define _LINUX_ION_H 19 20 #include <linux/types.h> 21 22 struct ion_handle; 23 /** 24 * enum ion_heap_types - list of all possible types of heaps 25 * @ION_HEAP_TYPE_SYSTEM: memory allocated via vmalloc 26 * @ION_HEAP_TYPE_SYSTEM_CONTIG: memory allocated via kmalloc 27 * @ION_HEAP_TYPE_CARVEOUT: memory allocated from a prereserved 28 * carveout heap, allocations are physically 29 * contiguous 30 * @ION_NUM_HEAPS: helper for iterating over heaps, a bit mask 31 * is used to identify the heaps, so only 32 32 * total heap types are supported 33 */ 34 enum ion_heap_type { 35 ION_HEAP_TYPE_SYSTEM, 36 ION_HEAP_TYPE_SYSTEM_CONTIG, 37 ION_HEAP_TYPE_CARVEOUT, 38 ION_HEAP_TYPE_CUSTOM, /* must be last so device specific heaps always 39 are at the end of this enum */ 40 ION_NUM_HEAPS = 16, 41 }; 42 43 #define ION_HEAP_SYSTEM_MASK (1 << ION_HEAP_TYPE_SYSTEM) 44 #define ION_HEAP_SYSTEM_CONTIG_MASK (1 << ION_HEAP_TYPE_SYSTEM_CONTIG) 45 #define ION_HEAP_CARVEOUT_MASK (1 << ION_HEAP_TYPE_CARVEOUT) 46 47 /** 48 * heap flags - the lower 16 bits are used by core ion, the upper 16 49 * bits are reserved for use by the heaps themselves. 50 */ 51 #define ION_FLAG_CACHED 1 /* mappings of this buffer should be 52 cached, ion will do cache 53 maintenance when the buffer is 54 mapped for dma */ 55 #define ION_FLAG_CACHED_NEEDS_SYNC 2 /* mappings of this buffer will created 56 at mmap time, if this is set 57 caches must be managed manually */ 58 59 #ifdef __KERNEL__ 60 struct ion_device; 61 struct ion_heap; 62 struct ion_mapper; 63 struct ion_client; 64 struct ion_buffer; 65 66 /* This should be removed some day when phys_addr_t's are fully 67 plumbed in the kernel, and all instances of ion_phys_addr_t should 68 be converted to phys_addr_t. For the time being many kernel interfaces 69 do not accept phys_addr_t's that would have to */ 70 #define ion_phys_addr_t unsigned long 71 72 /** 73 * struct ion_platform_heap - defines a heap in the given platform 74 * @type: type of the heap from ion_heap_type enum 75 * @id: unique identifier for heap. When allocating (lower numbers 76 * will be allocated from first) 77 * @name: used for debug purposes 78 * @base: base address of heap in physical memory if applicable 79 * @size: size of the heap in bytes if applicable 80 * 81 * Provided by the board file. 82 */ 83 struct ion_platform_heap { 84 enum ion_heap_type type; 85 unsigned int id; 86 const char *name; 87 ion_phys_addr_t base; 88 size_t size; 89 }; 90 91 /** 92 * struct ion_platform_data - array of platform heaps passed from board file 93 * @nr: number of structures in the array 94 * @heaps: array of platform_heap structions 95 * 96 * Provided by the board file in the form of platform data to a platform device. 97 */ 98 struct ion_platform_data { 99 int nr; 100 struct ion_platform_heap heaps[]; 101 }; 102 103 /** 104 * ion_reserve() - reserve memory for ion heaps if applicable 105 * @data: platform data specifying starting physical address and 106 * size 107 * 108 * Calls memblock reserve to set aside memory for heaps that are 109 * located at specific memory addresses or of specfic sizes not 110 * managed by the kernel 111 */ 112 void ion_reserve(struct ion_platform_data *data); 113 114 /** 115 * ion_client_create() - allocate a client and returns it 116 * @dev: the global ion device 117 * @heap_mask: mask of heaps this client can allocate from 118 * @name: used for debugging 119 */ 120 struct ion_client *ion_client_create(struct ion_device *dev, 121 unsigned int heap_mask, const char *name); 122 123 /** 124 * ion_client_destroy() - free's a client and all it's handles 125 * @client: the client 126 * 127 * Free the provided client and all it's resources including 128 * any handles it is holding. 129 */ 130 void ion_client_destroy(struct ion_client *client); 131 132 /** 133 * ion_alloc - allocate ion memory 134 * @client: the client 135 * @len: size of the allocation 136 * @align: requested allocation alignment, lots of hardware blocks have 137 * alignment requirements of some kind 138 * @heap_mask: mask of heaps to allocate from, if multiple bits are set 139 * heaps will be tried in order from lowest to highest order bit 140 * @flags: heap flags, the low 16 bits are consumed by ion, the high 16 141 * bits are passed on to the respective heap and can be heap 142 * custom 143 * 144 * Allocate memory in one of the heaps provided in heap mask and return 145 * an opaque handle to it. 146 */ 147 struct ion_handle *ion_alloc(struct ion_client *client, size_t len, 148 size_t align, unsigned int heap_mask, 149 unsigned int flags); 150 151 /** 152 * ion_free - free a handle 153 * @client: the client 154 * @handle: the handle to free 155 * 156 * Free the provided handle. 157 */ 158 void ion_free(struct ion_client *client, struct ion_handle *handle); 159 160 /** 161 * ion_phys - returns the physical address and len of a handle 162 * @client: the client 163 * @handle: the handle 164 * @addr: a pointer to put the address in 165 * @len: a pointer to put the length in 166 * 167 * This function queries the heap for a particular handle to get the 168 * handle's physical address. It't output is only correct if 169 * a heap returns physically contiguous memory -- in other cases 170 * this api should not be implemented -- ion_sg_table should be used 171 * instead. Returns -EINVAL if the handle is invalid. This has 172 * no implications on the reference counting of the handle -- 173 * the returned value may not be valid if the caller is not 174 * holding a reference. 175 */ 176 int ion_phys(struct ion_client *client, struct ion_handle *handle, 177 ion_phys_addr_t *addr, size_t *len); 178 179 /** 180 * ion_map_dma - return an sg_table describing a handle 181 * @client: the client 182 * @handle: the handle 183 * 184 * This function returns the sg_table describing 185 * a particular ion handle. 186 */ 187 struct sg_table *ion_sg_table(struct ion_client *client, 188 struct ion_handle *handle); 189 190 /** 191 * ion_map_kernel - create mapping for the given handle 192 * @client: the client 193 * @handle: handle to map 194 * 195 * Map the given handle into the kernel and return a kernel address that 196 * can be used to access this address. 197 */ 198 void *ion_map_kernel(struct ion_client *client, struct ion_handle *handle); 199 200 /** 201 * ion_unmap_kernel() - destroy a kernel mapping for a handle 202 * @client: the client 203 * @handle: handle to unmap 204 */ 205 void ion_unmap_kernel(struct ion_client *client, struct ion_handle *handle); 206 207 /** 208 * ion_share_dma_buf() - given an ion client, create a dma-buf fd 209 * @client: the client 210 * @handle: the handle 211 */ 212 int ion_share_dma_buf(struct ion_client *client, struct ion_handle *handle); 213 214 /** 215 * ion_import_dma_buf() - given an dma-buf fd from the ion exporter get handle 216 * @client: the client 217 * @fd: the dma-buf fd 218 * 219 * Given an dma-buf fd that was allocated through ion via ion_share_dma_buf, 220 * import that fd and return a handle representing it. If a dma-buf from 221 * another exporter is passed in this function will return ERR_PTR(-EINVAL) 222 */ 223 struct ion_handle *ion_import_dma_buf(struct ion_client *client, int fd); 224 225 #endif /* __KERNEL__ */ 226 227 /** 228 * DOC: Ion Userspace API 229 * 230 * create a client by opening /dev/ion 231 * most operations handled via following ioctls 232 * 233 */ 234 235 /** 236 * struct ion_allocation_data - metadata passed from userspace for allocations 237 * @len: size of the allocation 238 * @align: required alignment of the allocation 239 * @heap_mask: mask of heaps to allocate from 240 * @flags: flags passed to heap 241 * @handle: pointer that will be populated with a cookie to use to refer 242 * to this allocation 243 * 244 * Provided by userspace as an argument to the ioctl 245 */ 246 struct ion_allocation_data { 247 size_t len; 248 size_t align; 249 unsigned int heap_mask; 250 unsigned int flags; 251 struct ion_handle *handle; 252 }; 253 254 /** 255 * struct ion_fd_data - metadata passed to/from userspace for a handle/fd pair 256 * @handle: a handle 257 * @fd: a file descriptor representing that handle 258 * 259 * For ION_IOC_SHARE or ION_IOC_MAP userspace populates the handle field with 260 * the handle returned from ion alloc, and the kernel returns the file 261 * descriptor to share or map in the fd field. For ION_IOC_IMPORT, userspace 262 * provides the file descriptor and the kernel returns the handle. 263 */ 264 struct ion_fd_data { 265 struct ion_handle *handle; 266 int fd; 267 }; 268 269 /** 270 * struct ion_handle_data - a handle passed to/from the kernel 271 * @handle: a handle 272 */ 273 struct ion_handle_data { 274 struct ion_handle *handle; 275 }; 276 277 /** 278 * struct ion_custom_data - metadata passed to/from userspace for a custom ioctl 279 * @cmd: the custom ioctl function to call 280 * @arg: additional data to pass to the custom ioctl, typically a user 281 * pointer to a predefined structure 282 * 283 * This works just like the regular cmd and arg fields of an ioctl. 284 */ 285 struct ion_custom_data { 286 unsigned int cmd; 287 unsigned long arg; 288 }; 289 290 #define ION_IOC_MAGIC 'I' 291 292 /** 293 * DOC: ION_IOC_ALLOC - allocate memory 294 * 295 * Takes an ion_allocation_data struct and returns it with the handle field 296 * populated with the opaque handle for the allocation. 297 */ 298 #define ION_IOC_ALLOC _IOWR(ION_IOC_MAGIC, 0, \ 299 struct ion_allocation_data) 300 301 /** 302 * DOC: ION_IOC_FREE - free memory 303 * 304 * Takes an ion_handle_data struct and frees the handle. 305 */ 306 #define ION_IOC_FREE _IOWR(ION_IOC_MAGIC, 1, struct ion_handle_data) 307 308 /** 309 * DOC: ION_IOC_MAP - get a file descriptor to mmap 310 * 311 * Takes an ion_fd_data struct with the handle field populated with a valid 312 * opaque handle. Returns the struct with the fd field set to a file 313 * descriptor open in the current address space. This file descriptor 314 * can then be used as an argument to mmap. 315 */ 316 #define ION_IOC_MAP _IOWR(ION_IOC_MAGIC, 2, struct ion_fd_data) 317 318 /** 319 * DOC: ION_IOC_SHARE - creates a file descriptor to use to share an allocation 320 * 321 * Takes an ion_fd_data struct with the handle field populated with a valid 322 * opaque handle. Returns the struct with the fd field set to a file 323 * descriptor open in the current address space. This file descriptor 324 * can then be passed to another process. The corresponding opaque handle can 325 * be retrieved via ION_IOC_IMPORT. 326 */ 327 #define ION_IOC_SHARE _IOWR(ION_IOC_MAGIC, 4, struct ion_fd_data) 328 329 /** 330 * DOC: ION_IOC_IMPORT - imports a shared file descriptor 331 * 332 * Takes an ion_fd_data struct with the fd field populated with a valid file 333 * descriptor obtained from ION_IOC_SHARE and returns the struct with the handle 334 * filed set to the corresponding opaque handle. 335 */ 336 #define ION_IOC_IMPORT _IOWR(ION_IOC_MAGIC, 5, struct ion_fd_data) 337 338 /** 339 * DOC: ION_IOC_SYNC - syncs a shared file descriptors to memory 340 * 341 * Deprecated in favor of using the dma_buf api's correctly (syncing 342 * will happend automatically when the buffer is mapped to a device). 343 * If necessary should be used after touching a cached buffer from the cpu, 344 * this will make the buffer in memory coherent. 345 */ 346 #define ION_IOC_SYNC _IOWR(ION_IOC_MAGIC, 7, struct ion_fd_data) 347 348 /** 349 * DOC: ION_IOC_CUSTOM - call architecture specific ion ioctl 350 * 351 * Takes the argument of the architecture specific ioctl to call and 352 * passes appropriate userdata for that ioctl 353 */ 354 #define ION_IOC_CUSTOM _IOWR(ION_IOC_MAGIC, 6, struct ion_custom_data) 355 356 #endif /* _LINUX_ION_H */ 357