2
0

ion.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. #ifndef _LINUX_ION_H
  17. #define _LINUX_ION_H
  18. #include <linux/types.h>
  19. #define ION_VERSION "1.0"
  20. typedef int ion_user_handle_t;
  21. /**
  22. * enum ion_heap_types - list of all possible types of heaps
  23. * @ION_HEAP_TYPE_SYSTEM: memory allocated via vmalloc
  24. * @ION_HEAP_TYPE_SYSTEM_CONTIG: memory allocated via kmalloc
  25. * @ION_HEAP_TYPE_CARVEOUT: memory allocated from a prereserved
  26. * carveout heap, allocations are physically
  27. * contiguous
  28. * @ION_HEAP_TYPE_DMA: memory allocated via DMA API
  29. * @ION_NUM_HEAPS: helper for iterating over heaps, a bit mask
  30. * is used to identify the heaps, so only 32
  31. * total heap types are supported
  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_CHUNK,
  38. ION_HEAP_TYPE_DMA,
  39. ION_HEAP_TYPE_CUSTOM, /* must be last so device specific heaps always
  40. are at the end of this enum */
  41. ION_NUM_HEAPS = 16,
  42. };
  43. enum ion_heap_ids {
  44. ION_NOR_HEAP_ID = 0,
  45. ION_CMA_HEAP_ID = 1,
  46. ION_VPU_ID = 16,
  47. ION_CAM_ID = 17,
  48. ION_UI_ID = 18,
  49. };
  50. #define ION_HEAP_SYSTEM_MASK (1 << ION_HEAP_TYPE_SYSTEM)
  51. #define ION_HEAP_SYSTEM_CONTIG_MASK (1 << ION_HEAP_TYPE_SYSTEM_CONTIG)
  52. #define ION_HEAP_CARVEOUT_MASK (1 << ION_HEAP_TYPE_CARVEOUT)
  53. #define ION_HEAP_TYPE_DMA_MASK (1 << ION_HEAP_TYPE_DMA)
  54. #ifdef __KERNEL__
  55. struct ion_device;
  56. struct ion_heap;
  57. struct ion_mapper;
  58. struct ion_client;
  59. struct ion_buffer;
  60. /* This should be removed some day when phys_addr_t's are fully
  61. plumbed in the kernel, and all instances of ion_phys_addr_t should
  62. be converted to phys_addr_t. For the time being many kernel interfaces
  63. do not accept phys_addr_t's that would have to */
  64. #define ion_phys_addr_t unsigned long
  65. /**
  66. * struct ion_platform_heap - defines a heap in the given platform
  67. * @type: type of the heap from ion_heap_type enum
  68. * @id: unique identifier for heap. When allocating (lower numbers
  69. * will be allocated from first)
  70. * @name: used for debug purposes
  71. * @base: base address of heap in physical memory if applicable
  72. * @size: size of the heap in bytes if applicable
  73. *
  74. * Provided by the board file.
  75. */
  76. struct ion_platform_heap {
  77. enum ion_heap_type type;
  78. unsigned int id;
  79. const char *name;
  80. ion_phys_addr_t base;
  81. size_t size;
  82. };
  83. /**
  84. * struct ion_platform_data - array of platform heaps passed from board file
  85. * @nr: number of structures in the array
  86. * @heaps: array of platform_heap structions
  87. *
  88. * Provided by the board file in the form of platform data to a platform device.
  89. */
  90. struct ion_platform_data {
  91. int nr;
  92. struct ion_platform_heap heaps[];
  93. };
  94. /**
  95. * ion_client_create() - allocate a client and returns it
  96. * @dev: the global ion device
  97. * @heap_mask: mask of heaps this client can allocate from
  98. * @name: used for debugging
  99. */
  100. struct ion_client *ion_client_create(struct ion_device *dev,
  101. unsigned int heap_mask, const char *name);
  102. /**
  103. * ion_client_destroy() - free's a client and all it's handles
  104. * @client: the client
  105. *
  106. * Free the provided client and all it's resources including
  107. * any handles it is holding.
  108. */
  109. void ion_client_destroy(struct ion_client *client);
  110. /**
  111. * ion_alloc - allocate ion memory
  112. * @client: the client
  113. * @len: size of the allocation
  114. * @align: requested allocation alignment, lots of hardware blocks have
  115. * alignment requirements of some kind
  116. * @flags: mask of heaps to allocate from, if multiple bits are set
  117. * heaps will be tried in order from lowest to highest order bit
  118. *
  119. * Allocate memory in one of the heaps provided in heap mask and return
  120. * an opaque handle to it.
  121. */
  122. struct ion_handle *ion_alloc(struct ion_client *client, size_t len,
  123. size_t align, unsigned int flags);
  124. /**
  125. * ion_free - free a handle
  126. * @client: the client
  127. * @handle: the handle to free
  128. *
  129. * Free the provided handle.
  130. */
  131. void ion_free(struct ion_client *client, struct ion_handle *handle);
  132. /**
  133. * ion_phys - returns the physical address and len of a handle
  134. * @client: the client
  135. * @handle: the handle
  136. * @addr: a pointer to put the address in
  137. * @len: a pointer to put the length in
  138. *
  139. * This function queries the heap for a particular handle to get the
  140. * handle's physical address. It't output is only correct if
  141. * a heap returns physically contiguous memory -- in other cases
  142. * this api should not be implemented -- ion_map_dma should be used
  143. * instead. Returns -EINVAL if the handle is invalid. This has
  144. * no implications on the reference counting of the handle --
  145. * the returned value may not be valid if the caller is not
  146. * holding a reference.
  147. */
  148. int ion_phys(struct ion_client *client, struct ion_handle *handle,
  149. ion_phys_addr_t *addr, size_t *len);
  150. /**
  151. * ion_map_kernel - create mapping for the given handle
  152. * @client: the client
  153. * @handle: handle to map
  154. *
  155. * Map the given handle into the kernel and return a kernel address that
  156. * can be used to access this address.
  157. */
  158. void *ion_map_kernel(struct ion_client *client, struct ion_handle *handle);
  159. /**
  160. * ion_unmap_kernel() - destroy a kernel mapping for a handle
  161. * @client: the client
  162. * @handle: handle to unmap
  163. */
  164. void ion_unmap_kernel(struct ion_client *client, struct ion_handle *handle);
  165. /**
  166. * ion_map_dma - create a dma mapping for a given handle
  167. * @client: the client
  168. * @handle: handle to map
  169. *
  170. * Return an sglist describing the given handle
  171. */
  172. struct scatterlist *ion_map_dma(struct ion_client *client,
  173. struct ion_handle *handle);
  174. /**
  175. * ion_unmap_dma() - destroy a dma mapping for a handle
  176. * @client: the client
  177. * @handle: handle to unmap
  178. */
  179. void ion_unmap_dma(struct ion_client *client, struct ion_handle *handle);
  180. /**
  181. * ion_share() - given a handle, obtain a buffer to pass to other clients
  182. * @client: the client
  183. * @handle: the handle to share
  184. *
  185. * Given a handle, return a buffer, which exists in a global name
  186. * space, and can be passed to other clients. Should be passed into ion_import
  187. * to obtain a new handle for this buffer.
  188. *
  189. * NOTE: This function does do not an extra reference. The burden is on the
  190. * caller to make sure the buffer doesn't go away while it's being passed to
  191. * another client. That is, ion_free should not be called on this handle until
  192. * the buffer has been imported into the other client.
  193. */
  194. struct ion_buffer *ion_share(struct ion_client *client,
  195. struct ion_handle *handle);
  196. /**
  197. * ion_import() - given an buffer in another client, import it
  198. * @client: this blocks client
  199. * @buffer: the buffer to import (as obtained from ion_share)
  200. *
  201. * Given a buffer, add it to the client and return the handle to use to refer
  202. * to it further. This is called to share a handle from one kernel client to
  203. * another.
  204. */
  205. struct ion_handle *ion_import(struct ion_client *client,
  206. struct ion_buffer *buffer);
  207. /**
  208. * ion_import_fd() - given an fd obtained via ION_IOC_SHARE ioctl, import it
  209. * @client: this blocks client
  210. * @fd: the fd
  211. *
  212. * A helper function for drivers that will be recieving ion buffers shared
  213. * with them from userspace. These buffers are represented by a file
  214. * descriptor obtained as the return from the ION_IOC_SHARE ioctl.
  215. * This function coverts that fd into the underlying buffer, and returns
  216. * the handle to use to refer to it further.
  217. */
  218. struct ion_handle *ion_import_fd(struct ion_client *client, int fd);
  219. #endif /* __KERNEL__ */
  220. /**
  221. * DOC: Ion Userspace API
  222. *
  223. * create a client by opening /dev/ion
  224. * most operations handled via following ioctls
  225. *
  226. */
  227. /**
  228. * struct ion_allocation_data - metadata passed from userspace for allocations
  229. * @len: size of the allocation
  230. * @align: required alignment of the allocation
  231. * @flags: flags passed to heap
  232. * @handle: pointer that will be populated with a cookie to use to refer
  233. * to this allocation
  234. *
  235. * Provided by userspace as an argument to the ioctl
  236. */
  237. struct ion_allocation_data {
  238. size_t len;
  239. size_t align;
  240. unsigned int heap_id_mask;
  241. unsigned int flags;
  242. ion_user_handle_t handle;
  243. };
  244. /**
  245. * struct ion_fd_data - metadata passed to/from userspace for a handle/fd pair
  246. * @handle: a handle
  247. * @fd: a file descriptor representing that handle
  248. *
  249. * For ION_IOC_SHARE or ION_IOC_MAP userspace populates the handle field with
  250. * the handle returned from ion alloc, and the kernel returns the file
  251. * descriptor to share or map in the fd field. For ION_IOC_IMPORT, userspace
  252. * provides the file descriptor and the kernel returns the handle.
  253. */
  254. struct ion_fd_data {
  255. ion_user_handle_t handle;
  256. int fd;
  257. };
  258. /**
  259. * struct ion_handle_data - a handle passed to/from the kernel
  260. * @handle: a handle
  261. */
  262. struct ion_handle_data {
  263. ion_user_handle_t handle;
  264. };
  265. /**
  266. * struct ion_custom_data - metadata passed to/from userspace for a custom ioctl
  267. * @cmd: the custom ioctl function to call
  268. * @arg: additional data to pass to the custom ioctl, typically a user
  269. * pointer to a predefined structure
  270. *
  271. * This works just like the regular cmd and arg fields of an ioctl.
  272. */
  273. struct ion_custom_data {
  274. unsigned int cmd;
  275. unsigned long arg;
  276. };
  277. /* struct ion_flush_data - data passed to ion for flushing caches
  278. *
  279. * @handle: handle with data to flush
  280. * @fd: fd to flush
  281. * @vaddr: userspace virtual address mapped with mmap
  282. * @offset: offset into the handle to flush
  283. * @length: length of handle to flush
  284. *
  285. * Performs cache operations on the handle. If p is the start address
  286. * of the handle, p + offset through p + offset + length will have
  287. * the cache operations performed
  288. */
  289. struct ion_flush_data {
  290. //struct ion_handle *handle;
  291. ion_user_handle_t handle;
  292. int fd;
  293. void *vaddr;
  294. unsigned int offset;
  295. unsigned int length;
  296. };
  297. /// no available in new ion-kernel
  298. struct ion_phys_data {
  299. //struct ion_handle *handle;
  300. ion_user_handle_t handle;
  301. unsigned long phys;
  302. unsigned long size;
  303. };
  304. struct ion_cacheop_data {
  305. #define ION_CACHE_FLUSH 0
  306. #define ION_CACHE_CLEAN 1
  307. #define ION_CACHE_INV 2
  308. unsigned int type;
  309. struct ion_handle *handle;
  310. void *virt;
  311. };
  312. struct ion_buffer_info {
  313. unsigned long phys;
  314. unsigned long size;
  315. };
  316. struct ion_client_info {
  317. #define MAX_BUFFER_COUNT 127
  318. unsigned int count;
  319. unsigned long total_size;
  320. struct ion_buffer_info buf[MAX_BUFFER_COUNT];
  321. };
  322. struct ion_heap_info {
  323. unsigned int id;
  324. unsigned long allocated_size;
  325. unsigned long max_allocated;
  326. unsigned long total_size;
  327. };
  328. struct ion_share_obj_data {
  329. int fd;
  330. void *obj;
  331. };
  332. ///////////////////////////////////////////////////
  333. #define ION_IOC_MAGIC 'I'
  334. /**
  335. * DOC: ION_IOC_ALLOC - allocate memory
  336. *
  337. * Takes an ion_allocation_data struct and returns it with the handle field
  338. * populated with the opaque handle for the allocation.
  339. */
  340. #define ION_IOC_ALLOC _IOWR(ION_IOC_MAGIC, 0, \
  341. struct ion_allocation_data)
  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. * DOC: ION_IOC_MAP - get a file descriptor to mmap
  350. *
  351. * Takes an ion_fd_data struct with the handle field populated with a valid
  352. * opaque handle. Returns the struct with the fd field set to a file
  353. * descriptor open in the current address space. This file descriptor
  354. * can then be used as an argument to mmap.
  355. */
  356. #define ION_IOC_MAP _IOWR(ION_IOC_MAGIC, 2, struct ion_fd_data)
  357. /**
  358. * DOC: ION_IOC_SHARE - creates a file descriptor to use to share an allocation
  359. *
  360. * Takes an ion_fd_data struct with the handle field populated with a valid
  361. * opaque handle. Returns the struct with the fd field set to a file
  362. * descriptor open in the current address space. This file descriptor
  363. * can then be passed to another process. The corresponding opaque handle can
  364. * be retrieved via ION_IOC_IMPORT.
  365. */
  366. #define ION_IOC_SHARE _IOWR(ION_IOC_MAGIC, 4, struct ion_fd_data)
  367. /**
  368. * DOC: ION_IOC_IMPORT - imports a shared file descriptor
  369. *
  370. * Takes an ion_fd_data struct with the fd field populated with a valid file
  371. * descriptor obtained from ION_IOC_SHARE and returns the struct with the handle
  372. * filed set to the corresponding opaque handle.
  373. */
  374. #define ION_IOC_IMPORT _IOWR(ION_IOC_MAGIC, 5, struct ion_fd_data)
  375. /**
  376. * DOC: ION_IOC_SYNC - syncs a shared file descriptors to memory
  377. *
  378. * Deprecated in favor of using the dma_buf api's correctly (syncing
  379. * will happend automatically when the buffer is mapped to a device).
  380. * If necessary should be used after touching a cached buffer from the cpu,
  381. * this will make the buffer in memory coherent.
  382. */
  383. #define ION_IOC_SYNC _IOWR(ION_IOC_MAGIC, 7, struct ion_fd_data)
  384. /**
  385. * DOC: ION_IOC_CUSTOM - call architecture specific ion ioctl
  386. *
  387. * Takes the argument of the architecture specific ioctl to call and
  388. * passes appropriate userdata for that ioctl
  389. */
  390. #define ION_IOC_CUSTOM _IOWR(ION_IOC_MAGIC, 6, struct ion_custom_data)
  391. #if 1
  392. #define ION_IOC_ROCKCHIP_MAGIC 'R'
  393. /**
  394. * Clean the caches of the handle specified.
  395. */
  396. #define ION_IOC_CLEAN_CACHES _IOWR(ION_IOC_ROCKCHIP_MAGIC, 0, \
  397. struct ion_flush_data)
  398. /**
  399. * Invalidate the caches of the handle specified.
  400. */
  401. #define ION_IOC_INV_CACHES _IOWR(ION_IOC_ROCKCHIP_MAGIC, 1, \
  402. struct ion_flush_data)
  403. /**
  404. * Clean and invalidate the caches of the handle specified.
  405. */
  406. #define ION_IOC_CLEAN_INV_CACHES _IOWR(ION_IOC_ROCKCHIP_MAGIC, 2, \
  407. struct ion_flush_data)
  408. /**
  409. * Get phys addr of the handle specified.
  410. */
  411. #define ION_IOC_GET_PHYS _IOWR(ION_IOC_ROCKCHIP_MAGIC, 3, \
  412. struct ion_phys_data)
  413. #define ION_IOC_CLEAN_CACHES _IOWR(ION_IOC_ROCKCHIP_MAGIC, 0, struct ion_flush_data)
  414. #define ION_IOC_INV_CACHES _IOWR(ION_IOC_ROCKCHIP_MAGIC, 1, struct ion_flush_data)
  415. /* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
  416. #define ION_IOC_CLEAN_INV_CACHES _IOWR(ION_IOC_ROCKCHIP_MAGIC, 2, struct ion_flush_data)
  417. #define ION_IOC_GET_SHARE _IOWR(ION_IOC_ROCKCHIP_MAGIC, 4, struct ion_share_obj_data)
  418. #define ION_IOC_SET_SHARE _IOWR(ION_IOC_ROCKCHIP_MAGIC, 5, struct ion_share_obj_data)
  419. #else
  420. /// no available in new ion-kernel.
  421. #define ION_CUSTOM_GET_PHYS _IOWR(ION_IOC_MAGIC, 15, \
  422. struct ion_phys_data)
  423. #define ION_CUSTOM_CACHE_OP _IOWR(ION_IOC_MAGIC, 8, \
  424. struct ion_cacheop_data)
  425. #define ION_CUSTOM_GET_CLIENT_INFO _IOWR(ION_IOC_MAGIC, 9, \
  426. struct ion_client_info)
  427. #define ION_CUSTOM_GET_HEAP_INFO _IOWR(ION_IOC_MAGIC, 10, \
  428. struct ion_heap_info)
  429. /* Compatible with pmem */
  430. struct ion_pmem_region {
  431. unsigned long offset;
  432. unsigned long len;
  433. };
  434. #define ION_PMEM_GET_PHYS _IOW('p', 1, unsigned int)
  435. #define ION_PMEM_CACHE_FLUSH _IOW('p', 8, unsigned int)
  436. #endif
  437. ///////////////////////////////////////////
  438. #endif /* _LINUX_ION_H */