Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 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> |
Laura Abbott | a2e9363 | 2011-08-19 13:36:32 -0700 | [diff] [blame] | 21 | #include <mach/ion.h> |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 22 | |
| 23 | struct ion_handle; |
| 24 | /** |
| 25 | * enum ion_heap_types - list of all possible types of heaps |
Iliyan Malchev | f2230156 | 2011-07-06 16:53:21 -0700 | [diff] [blame] | 26 | * @ION_HEAP_TYPE_SYSTEM: memory allocated via vmalloc |
| 27 | * @ION_HEAP_TYPE_SYSTEM_CONTIG: memory allocated via kmalloc |
| 28 | * @ION_HEAP_TYPE_CARVEOUT: memory allocated from a prereserved |
| 29 | * carveout heap, allocations are physically |
| 30 | * contiguous |
| 31 | * @ION_HEAP_END: helper for iterating over heaps |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 32 | */ |
| 33 | enum ion_heap_type { |
| 34 | ION_HEAP_TYPE_SYSTEM, |
| 35 | ION_HEAP_TYPE_SYSTEM_CONTIG, |
| 36 | ION_HEAP_TYPE_CARVEOUT, |
| 37 | ION_HEAP_TYPE_CUSTOM, /* must be last so device specific heaps always |
| 38 | are at the end of this enum */ |
Rebecca Schultz Zavin | e6ee124 | 2011-06-30 12:19:55 -0700 | [diff] [blame] | 39 | ION_NUM_HEAPS, |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 40 | }; |
| 41 | |
Iliyan Malchev | f2230156 | 2011-07-06 16:53:21 -0700 | [diff] [blame] | 42 | #define ION_HEAP_SYSTEM_MASK (1 << ION_HEAP_TYPE_SYSTEM) |
| 43 | #define ION_HEAP_SYSTEM_CONTIG_MASK (1 << ION_HEAP_TYPE_SYSTEM_CONTIG) |
| 44 | #define ION_HEAP_CARVEOUT_MASK (1 << ION_HEAP_TYPE_CARVEOUT) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 45 | |
Laura Abbott | a2e9363 | 2011-08-19 13:36:32 -0700 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * These are the only ids that should be used for Ion heap ids. |
| 49 | * The ids listed are the order in which allocation will be attempted |
| 50 | * if specified. Don't swap the order of heap ids unless you know what |
| 51 | * you are doing! |
| 52 | */ |
| 53 | |
| 54 | enum ion_heap_ids { |
| 55 | ION_HEAP_SYSTEM_ID, |
| 56 | ION_HEAP_SYSTEM_CONTIG_ID, |
| 57 | ION_HEAP_EBI_ID, |
| 58 | ION_HEAP_SMI_ID, |
| 59 | }; |
| 60 | |
| 61 | #define ION_KMALLOC_HEAP_NAME "kmalloc" |
| 62 | #define ION_VMALLOC_HEAP_NAME "vmalloc" |
| 63 | #define ION_EBI1_HEAP_NAME "EBI1" |
| 64 | |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 65 | #define CACHED 1 |
| 66 | #define UNCACHED 0 |
| 67 | |
| 68 | #define ION_CACHE_SHIFT 0 |
| 69 | |
| 70 | #define ION_SET_CACHE(__cache) ((__cache) << ION_CACHE_SHIFT) |
| 71 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 72 | #ifdef __KERNEL__ |
| 73 | struct ion_device; |
| 74 | struct ion_heap; |
| 75 | struct ion_mapper; |
| 76 | struct ion_client; |
| 77 | struct ion_buffer; |
| 78 | |
| 79 | /* This should be removed some day when phys_addr_t's are fully |
| 80 | plumbed in the kernel, and all instances of ion_phys_addr_t should |
| 81 | be converted to phys_addr_t. For the time being many kernel interfaces |
| 82 | do not accept phys_addr_t's that would have to */ |
| 83 | #define ion_phys_addr_t unsigned long |
| 84 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 85 | /** |
| 86 | * struct ion_platform_heap - defines a heap in the given platform |
| 87 | * @type: type of the heap from ion_heap_type enum |
Rebecca Schultz Zavin | e6ee124 | 2011-06-30 12:19:55 -0700 | [diff] [blame] | 88 | * @id: unique identifier for heap. When allocating (lower numbers |
| 89 | * will be allocated from first) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 90 | * @name: used for debug purposes |
| 91 | * @base: base address of heap in physical memory if applicable |
| 92 | * @size: size of the heap in bytes if applicable |
| 93 | * |
| 94 | * Provided by the board file. |
| 95 | */ |
| 96 | struct ion_platform_heap { |
| 97 | enum ion_heap_type type; |
Rebecca Schultz Zavin | e6ee124 | 2011-06-30 12:19:55 -0700 | [diff] [blame] | 98 | unsigned int id; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 99 | const char *name; |
| 100 | ion_phys_addr_t base; |
| 101 | size_t size; |
Laura Abbott | a2e9363 | 2011-08-19 13:36:32 -0700 | [diff] [blame] | 102 | enum ion_memory_types memory_type; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | /** |
| 106 | * struct ion_platform_data - array of platform heaps passed from board file |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 107 | * @nr: number of structures in the array |
Rebecca Schultz Zavin | e6ee124 | 2011-06-30 12:19:55 -0700 | [diff] [blame] | 108 | * @heaps: array of platform_heap structions |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 109 | * |
| 110 | * Provided by the board file in the form of platform data to a platform device. |
| 111 | */ |
| 112 | struct ion_platform_data { |
| 113 | int nr; |
| 114 | struct ion_platform_heap heaps[]; |
| 115 | }; |
| 116 | |
| 117 | /** |
| 118 | * ion_client_create() - allocate a client and returns it |
| 119 | * @dev: the global ion device |
| 120 | * @heap_mask: mask of heaps this client can allocate from |
| 121 | * @name: used for debugging |
| 122 | */ |
| 123 | struct ion_client *ion_client_create(struct ion_device *dev, |
| 124 | unsigned int heap_mask, const char *name); |
| 125 | |
| 126 | /** |
Laura Abbott | 302911d | 2011-08-15 17:12:57 -0700 | [diff] [blame] | 127 | * msm_ion_client_create - allocate a client using the ion_device specified in |
| 128 | * drivers/gpu/ion/msm/msm_ion.c |
| 129 | * |
| 130 | * heap_mask and name are the same as ion_client_create, return values |
| 131 | * are the same as ion_client_create. |
| 132 | */ |
| 133 | |
| 134 | struct ion_client *msm_ion_client_create(unsigned int heap_mask, |
| 135 | const char *name); |
| 136 | |
| 137 | /** |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 138 | * ion_client_destroy() - free's a client and all it's handles |
| 139 | * @client: the client |
| 140 | * |
| 141 | * Free the provided client and all it's resources including |
| 142 | * any handles it is holding. |
| 143 | */ |
| 144 | void ion_client_destroy(struct ion_client *client); |
| 145 | |
| 146 | /** |
| 147 | * ion_alloc - allocate ion memory |
| 148 | * @client: the client |
| 149 | * @len: size of the allocation |
| 150 | * @align: requested allocation alignment, lots of hardware blocks have |
| 151 | * alignment requirements of some kind |
Rebecca Schultz Zavin | e6ee124 | 2011-06-30 12:19:55 -0700 | [diff] [blame] | 152 | * @flags: mask of heaps to allocate from, if multiple bits are set |
| 153 | * heaps will be tried in order from lowest to highest order bit |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 154 | * |
| 155 | * Allocate memory in one of the heaps provided in heap mask and return |
| 156 | * an opaque handle to it. |
| 157 | */ |
| 158 | struct ion_handle *ion_alloc(struct ion_client *client, size_t len, |
| 159 | size_t align, unsigned int flags); |
| 160 | |
| 161 | /** |
| 162 | * ion_free - free a handle |
| 163 | * @client: the client |
| 164 | * @handle: the handle to free |
| 165 | * |
| 166 | * Free the provided handle. |
| 167 | */ |
| 168 | void ion_free(struct ion_client *client, struct ion_handle *handle); |
| 169 | |
| 170 | /** |
| 171 | * ion_phys - returns the physical address and len of a handle |
| 172 | * @client: the client |
| 173 | * @handle: the handle |
| 174 | * @addr: a pointer to put the address in |
| 175 | * @len: a pointer to put the length in |
| 176 | * |
| 177 | * This function queries the heap for a particular handle to get the |
| 178 | * handle's physical address. It't output is only correct if |
| 179 | * a heap returns physically contiguous memory -- in other cases |
| 180 | * this api should not be implemented -- ion_map_dma should be used |
| 181 | * instead. Returns -EINVAL if the handle is invalid. This has |
| 182 | * no implications on the reference counting of the handle -- |
| 183 | * the returned value may not be valid if the caller is not |
| 184 | * holding a reference. |
| 185 | */ |
| 186 | int ion_phys(struct ion_client *client, struct ion_handle *handle, |
| 187 | ion_phys_addr_t *addr, size_t *len); |
| 188 | |
| 189 | /** |
| 190 | * ion_map_kernel - create mapping for the given handle |
| 191 | * @client: the client |
| 192 | * @handle: handle to map |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 193 | * @flags: flags for this mapping |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 194 | * |
| 195 | * Map the given handle into the kernel and return a kernel address that |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 196 | * can be used to access this address. If no flags are specified, this |
| 197 | * will return a non-secure uncached mapping. |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 198 | */ |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 199 | void *ion_map_kernel(struct ion_client *client, struct ion_handle *handle, |
| 200 | unsigned long flags); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 201 | |
| 202 | /** |
| 203 | * ion_unmap_kernel() - destroy a kernel mapping for a handle |
| 204 | * @client: the client |
| 205 | * @handle: handle to unmap |
| 206 | */ |
| 207 | void ion_unmap_kernel(struct ion_client *client, struct ion_handle *handle); |
| 208 | |
| 209 | /** |
| 210 | * ion_map_dma - create a dma mapping for a given handle |
| 211 | * @client: the client |
| 212 | * @handle: handle to map |
| 213 | * |
| 214 | * Return an sglist describing the given handle |
| 215 | */ |
| 216 | struct scatterlist *ion_map_dma(struct ion_client *client, |
Laura Abbott | 894fd58 | 2011-08-19 13:33:56 -0700 | [diff] [blame] | 217 | struct ion_handle *handle, |
| 218 | unsigned long flags); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 219 | |
| 220 | /** |
| 221 | * ion_unmap_dma() - destroy a dma mapping for a handle |
| 222 | * @client: the client |
| 223 | * @handle: handle to unmap |
| 224 | */ |
| 225 | void ion_unmap_dma(struct ion_client *client, struct ion_handle *handle); |
| 226 | |
| 227 | /** |
| 228 | * ion_share() - given a handle, obtain a buffer to pass to other clients |
| 229 | * @client: the client |
| 230 | * @handle: the handle to share |
| 231 | * |
Iliyan Malchev | 3fe2436 | 2011-08-09 14:42:08 -0700 | [diff] [blame] | 232 | * Given a handle, return a buffer, which exists in a global name |
| 233 | * space, and can be passed to other clients. Should be passed into ion_import |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 234 | * to obtain a new handle for this buffer. |
Iliyan Malchev | 3fe2436 | 2011-08-09 14:42:08 -0700 | [diff] [blame] | 235 | * |
| 236 | * NOTE: This function does do not an extra reference. The burden is on the |
| 237 | * caller to make sure the buffer doesn't go away while it's being passed to |
| 238 | * another client. That is, ion_free should not be called on this handle until |
| 239 | * the buffer has been imported into the other client. |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 240 | */ |
| 241 | struct ion_buffer *ion_share(struct ion_client *client, |
| 242 | struct ion_handle *handle); |
| 243 | |
| 244 | /** |
| 245 | * ion_import() - given an buffer in another client, import it |
| 246 | * @client: this blocks client |
| 247 | * @buffer: the buffer to import (as obtained from ion_share) |
| 248 | * |
| 249 | * Given a buffer, add it to the client and return the handle to use to refer |
| 250 | * to it further. This is called to share a handle from one kernel client to |
| 251 | * another. |
| 252 | */ |
| 253 | struct ion_handle *ion_import(struct ion_client *client, |
| 254 | struct ion_buffer *buffer); |
| 255 | |
| 256 | /** |
| 257 | * ion_import_fd() - given an fd obtained via ION_IOC_SHARE ioctl, import it |
| 258 | * @client: this blocks client |
| 259 | * @fd: the fd |
| 260 | * |
| 261 | * A helper function for drivers that will be recieving ion buffers shared |
| 262 | * with them from userspace. These buffers are represented by a file |
| 263 | * descriptor obtained as the return from the ION_IOC_SHARE ioctl. |
| 264 | * This function coverts that fd into the underlying buffer, and returns |
| 265 | * the handle to use to refer to it further. |
| 266 | */ |
| 267 | struct ion_handle *ion_import_fd(struct ion_client *client, int fd); |
| 268 | #endif /* __KERNEL__ */ |
| 269 | |
| 270 | /** |
| 271 | * DOC: Ion Userspace API |
| 272 | * |
| 273 | * create a client by opening /dev/ion |
| 274 | * most operations handled via following ioctls |
| 275 | * |
| 276 | */ |
| 277 | |
| 278 | /** |
| 279 | * struct ion_allocation_data - metadata passed from userspace for allocations |
| 280 | * @len: size of the allocation |
| 281 | * @align: required alignment of the allocation |
| 282 | * @flags: flags passed to heap |
| 283 | * @handle: pointer that will be populated with a cookie to use to refer |
| 284 | * to this allocation |
| 285 | * |
| 286 | * Provided by userspace as an argument to the ioctl |
| 287 | */ |
| 288 | struct ion_allocation_data { |
| 289 | size_t len; |
| 290 | size_t align; |
| 291 | unsigned int flags; |
| 292 | struct ion_handle *handle; |
| 293 | }; |
| 294 | |
| 295 | /** |
| 296 | * struct ion_fd_data - metadata passed to/from userspace for a handle/fd pair |
| 297 | * @handle: a handle |
| 298 | * @fd: a file descriptor representing that handle |
| 299 | * |
| 300 | * For ION_IOC_SHARE or ION_IOC_MAP userspace populates the handle field with |
| 301 | * the handle returned from ion alloc, and the kernel returns the file |
| 302 | * descriptor to share or map in the fd field. For ION_IOC_IMPORT, userspace |
| 303 | * provides the file descriptor and the kernel returns the handle. |
| 304 | */ |
| 305 | struct ion_fd_data { |
| 306 | struct ion_handle *handle; |
| 307 | int fd; |
| 308 | }; |
| 309 | |
| 310 | /** |
| 311 | * struct ion_handle_data - a handle passed to/from the kernel |
| 312 | * @handle: a handle |
| 313 | */ |
| 314 | struct ion_handle_data { |
| 315 | struct ion_handle *handle; |
| 316 | }; |
| 317 | |
Rebecca Schultz Zavin | e6ee124 | 2011-06-30 12:19:55 -0700 | [diff] [blame] | 318 | /** |
| 319 | * struct ion_custom_data - metadata passed to/from userspace for a custom ioctl |
| 320 | * @cmd: the custom ioctl function to call |
| 321 | * @arg: additional data to pass to the custom ioctl, typically a user |
| 322 | * pointer to a predefined structure |
| 323 | * |
| 324 | * This works just like the regular cmd and arg fields of an ioctl. |
| 325 | */ |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 326 | struct ion_custom_data { |
| 327 | unsigned int cmd; |
| 328 | unsigned long arg; |
| 329 | }; |
| 330 | |
| 331 | #define ION_IOC_MAGIC 'I' |
| 332 | |
| 333 | /** |
| 334 | * DOC: ION_IOC_ALLOC - allocate memory |
| 335 | * |
| 336 | * Takes an ion_allocation_data struct and returns it with the handle field |
| 337 | * populated with the opaque handle for the allocation. |
| 338 | */ |
| 339 | #define ION_IOC_ALLOC _IOWR(ION_IOC_MAGIC, 0, \ |
| 340 | struct ion_allocation_data) |
| 341 | |
| 342 | /** |
| 343 | * DOC: ION_IOC_FREE - free memory |
| 344 | * |
| 345 | * Takes an ion_handle_data struct and frees the handle. |
| 346 | */ |
| 347 | #define ION_IOC_FREE _IOWR(ION_IOC_MAGIC, 1, struct ion_handle_data) |
| 348 | |
| 349 | /** |
| 350 | * DOC: ION_IOC_MAP - get a file descriptor to mmap |
| 351 | * |
| 352 | * Takes an ion_fd_data struct with the handle field populated with a valid |
| 353 | * opaque handle. Returns the struct with the fd field set to a file |
| 354 | * descriptor open in the current address space. This file descriptor |
| 355 | * can then be used as an argument to mmap. |
| 356 | */ |
| 357 | #define ION_IOC_MAP _IOWR(ION_IOC_MAGIC, 2, struct ion_fd_data) |
| 358 | |
| 359 | /** |
| 360 | * DOC: ION_IOC_SHARE - creates a file descriptor to use to share an allocation |
| 361 | * |
| 362 | * Takes an ion_fd_data struct with the handle field populated with a valid |
| 363 | * opaque handle. Returns the struct with the fd field set to a file |
| 364 | * descriptor open in the current address space. This file descriptor |
| 365 | * can then be passed to another process. The corresponding opaque handle can |
| 366 | * be retrieved via ION_IOC_IMPORT. |
| 367 | */ |
| 368 | #define ION_IOC_SHARE _IOWR(ION_IOC_MAGIC, 4, struct ion_fd_data) |
| 369 | |
| 370 | /** |
| 371 | * DOC: ION_IOC_IMPORT - imports a shared file descriptor |
| 372 | * |
| 373 | * Takes an ion_fd_data struct with the fd field populated with a valid file |
| 374 | * descriptor obtained from ION_IOC_SHARE and returns the struct with the handle |
| 375 | * filed set to the corresponding opaque handle. |
| 376 | */ |
| 377 | #define ION_IOC_IMPORT _IOWR(ION_IOC_MAGIC, 5, int) |
| 378 | |
| 379 | /** |
| 380 | * DOC: ION_IOC_CUSTOM - call architecture specific ion ioctl |
| 381 | * |
| 382 | * Takes the argument of the architecture specific ioctl to call and |
| 383 | * passes appropriate userdata for that ioctl |
| 384 | */ |
| 385 | #define ION_IOC_CUSTOM _IOWR(ION_IOC_MAGIC, 6, struct ion_custom_data) |
| 386 | |
| 387 | #endif /* _LINUX_ION_H */ |