msm_ion.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. #ifndef _LINUX_MSM_ION_H
  2. #define _LINUX_MSM_ION_H
  3. #include <linux/ion.h>
  4. enum msm_ion_heap_types {
  5. ION_HEAP_TYPE_MSM_START = ION_HEAP_TYPE_CUSTOM + 1,
  6. ION_HEAP_TYPE_IOMMUCA = ION_HEAP_TYPE_MSM_START,
  7. ION_HEAP_TYPE_DMA,
  8. ION_HEAP_TYPE_CP,
  9. ION_HEAP_TYPE_SECURE_DMA,
  10. ION_HEAP_TYPE_REMOVED,
  11. /*
  12. * if you add a heap type here you should also add it to
  13. * heap_types_info[] in msm_ion.c
  14. */
  15. };
  16. /**
  17. * These are the only ids that should be used for Ion heap ids.
  18. * The ids listed are the order in which allocation will be attempted
  19. * if specified. Don't swap the order of heap ids unless you know what
  20. * you are doing!
  21. * Id's are spaced by purpose to allow new Id's to be inserted in-between (for
  22. * possible fallbacks)
  23. */
  24. enum ion_heap_ids {
  25. INVALID_HEAP_ID = -1,
  26. ION_CP_MM_HEAP_ID = 8,
  27. ION_CP_MFC_HEAP_ID = 12,
  28. ION_CP_WB_HEAP_ID = 16, /* 8660 only */
  29. ION_IOMMUCA_HEAP_ID = 20,
  30. ION_SYSTEM_CONTIG_HEAP_ID = 21,
  31. ION_ADSP_HEAP_ID = 22,
  32. ION_PIL1_HEAP_ID = 23, /* Currently used for other PIL images */
  33. ION_SF_HEAP_ID = 24,
  34. ION_SYSTEM_HEAP_ID = 25,
  35. ION_PIL2_HEAP_ID = 26, /* Currently used for modem firmware images */
  36. ION_QSECOM_HEAP_ID = 27,
  37. ION_AUDIO_HEAP_ID = 28,
  38. ION_MM_FIRMWARE_HEAP_ID = 29,
  39. ION_CAMERA_HEAP_ID = 30,
  40. ION_HEAP_ID_RESERVED = 31 /** Bit reserved for ION_FLAG_SECURE flag */
  41. };
  42. /*
  43. * The IOMMU heap is deprecated! Here are some aliases for backwards
  44. * compatibility:
  45. */
  46. #define ION_IOMMU_HEAP_ID ION_SYSTEM_HEAP_ID
  47. #define ION_HEAP_TYPE_IOMMU ION_HEAP_TYPE_SYSTEM
  48. enum ion_fixed_position {
  49. NOT_FIXED,
  50. FIXED_LOW,
  51. FIXED_MIDDLE,
  52. FIXED_HIGH,
  53. };
  54. enum cp_mem_usage {
  55. VIDEO_BITSTREAM = 0x1,
  56. VIDEO_PIXEL = 0x2,
  57. VIDEO_NONPIXEL = 0x3,
  58. MAX_USAGE = 0x4,
  59. UNKNOWN = 0x7FFFFFFF,
  60. };
  61. #define ION_HEAP_CP_MASK (1 << ION_HEAP_TYPE_CP)
  62. #define ION_HEAP_TYPE_DMA_MASK (1 << ION_HEAP_TYPE_DMA)
  63. /**
  64. * Flag to use when allocating to indicate that a heap is secure.
  65. */
  66. #define ION_FLAG_SECURE (1 << ION_HEAP_ID_RESERVED)
  67. /**
  68. * Flag for clients to force contiguous memort allocation
  69. *
  70. * Use of this flag is carefully monitored!
  71. */
  72. #define ION_FLAG_FORCE_CONTIGUOUS (1 << 30)
  73. /*
  74. * Used in conjunction with heap which pool memory to force an allocation
  75. * to come from the page allocator directly instead of from the pool allocation
  76. */
  77. #define ION_FLAG_POOL_FORCE_ALLOC (1 << 16)
  78. /**
  79. * Deprecated! Please use the corresponding ION_FLAG_*
  80. */
  81. #define ION_SECURE ION_FLAG_SECURE
  82. #define ION_FORCE_CONTIGUOUS ION_FLAG_FORCE_CONTIGUOUS
  83. /**
  84. * Macro should be used with ion_heap_ids defined above.
  85. */
  86. #define ION_HEAP(bit) (1 << (bit))
  87. #define ION_ADSP_HEAP_NAME "adsp"
  88. #define ION_SYSTEM_HEAP_NAME "system"
  89. #define ION_VMALLOC_HEAP_NAME ION_SYSTEM_HEAP_NAME
  90. #define ION_KMALLOC_HEAP_NAME "kmalloc"
  91. #define ION_AUDIO_HEAP_NAME "audio"
  92. #define ION_SF_HEAP_NAME "sf"
  93. #define ION_MM_HEAP_NAME "mm"
  94. #define ION_CAMERA_HEAP_NAME "camera_preview"
  95. #define ION_IOMMU_HEAP_NAME "iommu"
  96. #define ION_IOMMUCA_HEAP_NAME "iommuca"
  97. #define ION_MFC_HEAP_NAME "mfc"
  98. #define ION_WB_HEAP_NAME "wb"
  99. #define ION_MM_FIRMWARE_HEAP_NAME "mm_fw"
  100. #define ION_PIL1_HEAP_NAME "pil_1"
  101. #define ION_PIL2_HEAP_NAME "pil_2"
  102. #define ION_QSECOM_HEAP_NAME "qsecom"
  103. #define ION_SET_CACHED(__cache) (__cache | ION_FLAG_CACHED)
  104. #define ION_SET_UNCACHED(__cache) (__cache & ~ION_FLAG_CACHED)
  105. #define ION_IS_CACHED(__flags) ((__flags) & ION_FLAG_CACHED)
  106. #ifdef __KERNEL__
  107. /*
  108. * This flag allows clients when mapping into the IOMMU to specify to
  109. * defer un-mapping from the IOMMU until the buffer memory is freed.
  110. */
  111. #define ION_IOMMU_UNMAP_DELAYED 1
  112. /*
  113. * This flag allows clients to defer unsecuring a buffer until the buffer
  114. * is actually freed.
  115. */
  116. #define ION_UNSECURE_DELAYED 1
  117. /**
  118. * struct ion_cp_heap_pdata - defines a content protection heap in the given
  119. * platform
  120. * @permission_type: Memory ID used to identify the memory to TZ
  121. * @align: Alignment requirement for the memory
  122. * @secure_base: Base address for securing the heap.
  123. * Note: This might be different from actual base address
  124. * of this heap in the case of a shared heap.
  125. * @secure_size: Memory size for securing the heap.
  126. * Note: This might be different from actual size
  127. * of this heap in the case of a shared heap.
  128. * @fixed_position If nonzero, position in the fixed area.
  129. * @iommu_map_all: Indicates whether we should map whole heap into IOMMU.
  130. * @iommu_2x_map_domain: Indicates the domain to use for overmapping.
  131. * @request_region: function to be called when the number of allocations
  132. * goes from 0 -> 1
  133. * @release_region: function to be called when the number of allocations
  134. * goes from 1 -> 0
  135. * @setup_region: function to be called upon ion registration
  136. * @memory_type:Memory type used for the heap
  137. * @allow_nonsecure_alloc: allow non-secure allocations from this heap. For
  138. * secure heaps, this flag must be set so allow non-secure
  139. * allocations. For non-secure heaps, this flag is ignored.
  140. *
  141. */
  142. struct ion_cp_heap_pdata {
  143. enum ion_permission_type permission_type;
  144. unsigned int align;
  145. ion_phys_addr_t secure_base; /* Base addr used when heap is shared */
  146. size_t secure_size; /* Size used for securing heap when heap is shared*/
  147. int is_cma;
  148. enum ion_fixed_position fixed_position;
  149. int iommu_map_all;
  150. int iommu_2x_map_domain;
  151. int (*request_region)(void *);
  152. int (*release_region)(void *);
  153. void *(*setup_region)(void);
  154. enum ion_memory_types memory_type;
  155. int allow_nonsecure_alloc;
  156. };
  157. /**
  158. * struct ion_co_heap_pdata - defines a carveout heap in the given platform
  159. * @adjacent_mem_id: Id of heap that this heap must be adjacent to.
  160. * @align: Alignment requirement for the memory
  161. * @fixed_position If nonzero, position in the fixed area.
  162. * @request_region: function to be called when the number of allocations
  163. * goes from 0 -> 1
  164. * @release_region: function to be called when the number of allocations
  165. * goes from 1 -> 0
  166. * @setup_region: function to be called upon ion registration
  167. * @memory_type:Memory type used for the heap
  168. *
  169. */
  170. struct ion_co_heap_pdata {
  171. int adjacent_mem_id;
  172. unsigned int align;
  173. enum ion_fixed_position fixed_position;
  174. int (*request_region)(void *);
  175. int (*release_region)(void *);
  176. void *(*setup_region)(void);
  177. enum ion_memory_types memory_type;
  178. };
  179. /*
  180. * struct ion_cma_pdata - extra data for CMA regions
  181. * @default_prefetch_size - default size to use for prefetching
  182. */
  183. struct ion_cma_pdata {
  184. unsigned long default_prefetch_size;
  185. };
  186. #ifdef CONFIG_ION
  187. /**
  188. * msm_ion_client_create - allocate a client using the ion_device specified in
  189. * drivers/gpu/ion/msm/msm_ion.c
  190. *
  191. * heap_mask and name are the same as ion_client_create, return values
  192. * are the same as ion_client_create.
  193. */
  194. struct ion_client *msm_ion_client_create(unsigned int heap_mask,
  195. const char *name);
  196. /**
  197. * ion_handle_get_flags - get the flags for a given handle
  198. *
  199. * @client - client who allocated the handle
  200. * @handle - handle to get the flags
  201. * @flags - pointer to store the flags
  202. *
  203. * Gets the current flags for a handle. These flags indicate various options
  204. * of the buffer (caching, security, etc.)
  205. */
  206. int ion_handle_get_flags(struct ion_client *client, struct ion_handle *handle,
  207. unsigned long *flags);
  208. /**
  209. * ion_map_iommu - map the given handle into an iommu
  210. *
  211. * @client - client who allocated the handle
  212. * @handle - handle to map
  213. * @domain_num - domain number to map to
  214. * @partition_num - partition number to allocate iova from
  215. * @align - alignment for the iova
  216. * @iova_length - length of iova to map. If the iova length is
  217. * greater than the handle length, the remaining
  218. * address space will be mapped to a dummy buffer.
  219. * @iova - pointer to store the iova address
  220. * @buffer_size - pointer to store the size of the buffer
  221. * @flags - flags for options to map
  222. * @iommu_flags - flags specific to the iommu.
  223. *
  224. * Maps the handle into the iova space specified via domain number. Iova
  225. * will be allocated from the partition specified via partition_num.
  226. * Returns 0 on success, negative value on error.
  227. */
  228. int ion_map_iommu(struct ion_client *client, struct ion_handle *handle,
  229. int domain_num, int partition_num, unsigned long align,
  230. unsigned long iova_length, unsigned long *iova,
  231. unsigned long *buffer_size,
  232. unsigned long flags, unsigned long iommu_flags);
  233. /**
  234. * ion_handle_get_size - get the allocated size of a given handle
  235. *
  236. * @client - client who allocated the handle
  237. * @handle - handle to get the size
  238. * @size - pointer to store the size
  239. *
  240. * gives the allocated size of a handle. returns 0 on success, negative
  241. * value on error
  242. *
  243. * NOTE: This is intended to be used only to get a size to pass to map_iommu.
  244. * You should *NOT* rely on this for any other usage.
  245. */
  246. int ion_handle_get_size(struct ion_client *client, struct ion_handle *handle,
  247. unsigned long *size);
  248. /**
  249. * ion_unmap_iommu - unmap the handle from an iommu
  250. *
  251. * @client - client who allocated the handle
  252. * @handle - handle to unmap
  253. * @domain_num - domain to unmap from
  254. * @partition_num - partition to unmap from
  255. *
  256. * Decrement the reference count on the iommu mapping. If the count is
  257. * 0, the mapping will be removed from the iommu.
  258. */
  259. void ion_unmap_iommu(struct ion_client *client, struct ion_handle *handle,
  260. int domain_num, int partition_num);
  261. /**
  262. * ion_secure_heap - secure a heap
  263. *
  264. * @client - a client that has allocated from the heap heap_id
  265. * @heap_id - heap id to secure.
  266. * @version - version of content protection
  267. * @data - extra data needed for protection
  268. *
  269. * Secure a heap
  270. * Returns 0 on success
  271. */
  272. int ion_secure_heap(struct ion_device *dev, int heap_id, int version,
  273. void *data);
  274. /**
  275. * ion_unsecure_heap - un-secure a heap
  276. *
  277. * @client - a client that has allocated from the heap heap_id
  278. * @heap_id - heap id to un-secure.
  279. * @version - version of content protection
  280. * @data - extra data needed for protection
  281. *
  282. * Un-secure a heap
  283. * Returns 0 on success
  284. */
  285. int ion_unsecure_heap(struct ion_device *dev, int heap_id, int version,
  286. void *data);
  287. /**
  288. * msm_ion_do_cache_op - do cache operations.
  289. *
  290. * @client - pointer to ION client.
  291. * @handle - pointer to buffer handle.
  292. * @vaddr - virtual address to operate on.
  293. * @len - Length of data to do cache operation on.
  294. * @cmd - Cache operation to perform:
  295. * ION_IOC_CLEAN_CACHES
  296. * ION_IOC_INV_CACHES
  297. * ION_IOC_CLEAN_INV_CACHES
  298. *
  299. * Returns 0 on success
  300. */
  301. int msm_ion_do_cache_op(struct ion_client *client, struct ion_handle *handle,
  302. void *vaddr, unsigned long len, unsigned int cmd);
  303. /**
  304. * msm_ion_secure_heap - secure a heap. Wrapper around ion_secure_heap.
  305. *
  306. * @heap_id - heap id to secure.
  307. *
  308. * Secure a heap
  309. * Returns 0 on success
  310. */
  311. int msm_ion_secure_heap(int heap_id);
  312. /**
  313. * msm_ion_unsecure_heap - unsecure a heap. Wrapper around ion_unsecure_heap.
  314. *
  315. * @heap_id - heap id to secure.
  316. *
  317. * Un-secure a heap
  318. * Returns 0 on success
  319. */
  320. int msm_ion_unsecure_heap(int heap_id);
  321. /**
  322. * msm_ion_secure_heap_2_0 - secure a heap using 2.0 APIs
  323. * Wrapper around ion_secure_heap.
  324. *
  325. * @heap_id - heap id to secure.
  326. * @usage - usage hint to TZ
  327. *
  328. * Secure a heap
  329. * Returns 0 on success
  330. */
  331. int msm_ion_secure_heap_2_0(int heap_id, enum cp_mem_usage usage);
  332. /**
  333. * msm_ion_unsecure_heap - unsecure a heap secured with 3.0 APIs.
  334. * Wrapper around ion_unsecure_heap.
  335. *
  336. * @heap_id - heap id to secure.
  337. * @usage - usage hint to TZ
  338. *
  339. * Un-secure a heap
  340. * Returns 0 on success
  341. */
  342. int msm_ion_unsecure_heap_2_0(int heap_id, enum cp_mem_usage usage);
  343. /**
  344. * msm_ion_secure_buffer - secure an individual buffer
  345. *
  346. * @client - client who has access to the buffer
  347. * @handle - buffer to secure
  348. * @usage - usage hint to TZ
  349. * @flags - flags for the securing
  350. */
  351. int msm_ion_secure_buffer(struct ion_client *client, struct ion_handle *handle,
  352. enum cp_mem_usage usage, int flags);
  353. /**
  354. * msm_ion_unsecure_buffer - unsecure an individual buffer
  355. *
  356. * @client - client who has access to the buffer
  357. * @handle - buffer to secure
  358. */
  359. int msm_ion_unsecure_buffer(struct ion_client *client,
  360. struct ion_handle *handle);
  361. #else
  362. static inline struct ion_client *msm_ion_client_create(unsigned int heap_mask,
  363. const char *name)
  364. {
  365. return ERR_PTR(-ENODEV);
  366. }
  367. static inline int ion_map_iommu(struct ion_client *client,
  368. struct ion_handle *handle, int domain_num,
  369. int partition_num, unsigned long align,
  370. unsigned long iova_length, unsigned long *iova,
  371. unsigned long *buffer_size,
  372. unsigned long flags,
  373. unsigned long iommu_flags)
  374. {
  375. return -ENODEV;
  376. }
  377. static inline int ion_handle_get_size(struct ion_client *client,
  378. struct ion_handle *handle, unsigned long *size)
  379. {
  380. return -ENODEV;
  381. }
  382. static inline void ion_unmap_iommu(struct ion_client *client,
  383. struct ion_handle *handle, int domain_num,
  384. int partition_num)
  385. {
  386. return;
  387. }
  388. static inline int ion_secure_heap(struct ion_device *dev, int heap_id,
  389. int version, void *data)
  390. {
  391. return -ENODEV;
  392. }
  393. static inline int ion_unsecure_heap(struct ion_device *dev, int heap_id,
  394. int version, void *data)
  395. {
  396. return -ENODEV;
  397. }
  398. static inline void ion_mark_dangling_buffers_locked(struct ion_device *dev)
  399. {
  400. }
  401. static inline int msm_ion_do_cache_op(struct ion_client *client,
  402. struct ion_handle *handle, void *vaddr,
  403. unsigned long len, unsigned int cmd)
  404. {
  405. return -ENODEV;
  406. }
  407. static inline int msm_ion_secure_heap(int heap_id)
  408. {
  409. return -ENODEV;
  410. }
  411. static inline int msm_ion_unsecure_heap(int heap_id)
  412. {
  413. return -ENODEV;
  414. }
  415. static inline int msm_ion_secure_heap_2_0(int heap_id, enum cp_mem_usage usage)
  416. {
  417. return -ENODEV;
  418. }
  419. static inline int msm_ion_unsecure_heap_2_0(int heap_id,
  420. enum cp_mem_usage usage)
  421. {
  422. return -ENODEV;
  423. }
  424. static inline int msm_ion_secure_buffer(struct ion_client *client,
  425. struct ion_handle *handle,
  426. enum cp_mem_usage usage,
  427. int flags)
  428. {
  429. return -ENODEV;
  430. }
  431. static inline int msm_ion_unsecure_buffer(struct ion_client *client,
  432. struct ion_handle *handle)
  433. {
  434. return -ENODEV;
  435. }
  436. #endif /* CONFIG_ION */
  437. #endif /* __KERNEL */
  438. /* struct ion_flush_data - data passed to ion for flushing caches
  439. *
  440. * @handle: handle with data to flush
  441. * @fd: fd to flush
  442. * @vaddr: userspace virtual address mapped with mmap
  443. * @offset: offset into the handle to flush
  444. * @length: length of handle to flush
  445. *
  446. * Performs cache operations on the handle. If p is the start address
  447. * of the handle, p + offset through p + offset + length will have
  448. * the cache operations performed
  449. */
  450. struct ion_flush_data {
  451. struct ion_handle *handle;
  452. int fd;
  453. void *vaddr;
  454. unsigned int offset;
  455. unsigned int length;
  456. };
  457. struct ion_prefetch_data {
  458. int heap_id;
  459. unsigned long len;
  460. };
  461. #if defined(CONFIG_MACH_KLTE_JPN) || defined(CONFIG_MACH_HLTEDCM) || defined(CONFIG_MACH_HLTEKDI) || defined(CONFIG_MACH_JS01LTEDCM) || defined(CONFIG_DTCP_ION_PHYS)
  462. /* struct ion_buffer_data
  463. *
  464. * @handle: handle for the buffer being queried
  465. * @paddr: The physical address of the buffer referenced by the handle
  466. * @length: The length of the buffer referenced by the handle
  467. *
  468. * Gets the physicial address of the given handle
  469. */
  470. struct ion_buffer_data {
  471. struct ion_handle *handle;
  472. unsigned long paddr;
  473. unsigned int length;
  474. };
  475. #endif
  476. #define ION_IOC_MSM_MAGIC 'M'
  477. /**
  478. * DOC: ION_IOC_CLEAN_CACHES - clean the caches
  479. *
  480. * Clean the caches of the handle specified.
  481. */
  482. #define ION_IOC_CLEAN_CACHES _IOWR(ION_IOC_MSM_MAGIC, 0, \
  483. struct ion_flush_data)
  484. /**
  485. * DOC: ION_IOC_INV_CACHES - invalidate the caches
  486. *
  487. * Invalidate the caches of the handle specified.
  488. */
  489. #define ION_IOC_INV_CACHES _IOWR(ION_IOC_MSM_MAGIC, 1, \
  490. struct ion_flush_data)
  491. /**
  492. * DOC: ION_IOC_CLEAN_INV_CACHES - clean and invalidate the caches
  493. *
  494. * Clean and invalidate the caches of the handle specified.
  495. */
  496. #define ION_IOC_CLEAN_INV_CACHES _IOWR(ION_IOC_MSM_MAGIC, 2, \
  497. struct ion_flush_data)
  498. #define ION_IOC_PREFETCH _IOWR(ION_IOC_MSM_MAGIC, 3, \
  499. struct ion_prefetch_data)
  500. #define ION_IOC_DRAIN _IOWR(ION_IOC_MSM_MAGIC, 4, \
  501. struct ion_prefetch_data)
  502. #if defined(CONFIG_MACH_KLTE_JPN) || defined(CONFIG_MACH_HLTEDCM) || defined(CONFIG_MACH_HLTEKDI) || defined(CONFIG_MACH_JS01LTEDCM) || defined(CONFIG_DTCP_ION_PHYS)
  503. /**
  504. * DOC: ION_IOC_GET_PHYS - get the physical address of the handle
  505. *
  506. * Gets the physicial address of the given handle
  507. */
  508. #define ION_IOC_GET_PHYS _IOWR(ION_IOC_MSM_MAGIC, 5, \
  509. struct ion_buffer_data)
  510. #endif
  511. #endif