ion.h 14 KB

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