mem-buf.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /* SPDX-License-Identifier: GPL-2.0-only WITH Linux-syscall-note */
  2. /*
  3. * Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #ifndef _UAPI_LINUX_MEM_BUF_H
  7. #define _UAPI_LINUX_MEM_BUF_H
  8. #include <linux/ioctl.h>
  9. #include <linux/types.h>
  10. #define MEM_BUF_IOC_MAGIC 'M'
  11. /**
  12. * enum mem_buf_mem_type: Types of memory that can be allocated from and to
  13. * @MEM_BUF_ION_MEM_TYPE: The memory for the source or destination is ION memory
  14. */
  15. enum mem_buf_mem_type {
  16. MEM_BUF_ION_MEM_TYPE,
  17. MEM_BUF_MAX_MEM_TYPE,
  18. };
  19. #define MEM_BUF_DMAHEAP_MEM_TYPE (MEM_BUF_ION_MEM_TYPE + 1)
  20. /* RESERVED for MEM_BUF_BUDDY_MEM_TYPE: MEM_BUF_ION_MEM_TYPE + 2 */
  21. /* The mem-buf values that represent VMIDs for an ACL. */
  22. #define MEM_BUF_VMID_PRIMARY_VM 0
  23. #define MEM_BUF_VMID_TRUSTED_VM 1
  24. #define MEM_BUF_PERM_FLAG_READ (1U << 0)
  25. #define MEM_BUF_PERM_FLAG_WRITE (1U << 1)
  26. #define MEM_BUF_PERM_FLAG_EXEC (1U << 2)
  27. #define MEM_BUF_PERM_VALID_FLAGS\
  28. (MEM_BUF_PERM_FLAG_READ | MEM_BUF_PERM_FLAG_WRITE |\
  29. MEM_BUF_PERM_FLAG_EXEC)
  30. #define MEM_BUF_MAX_NR_ACL_ENTS 16
  31. /**
  32. * struct acl_entry: Represents the access control permissions for a VMID.
  33. * @vmid: The mem-buf VMID specifier associated with the VMID that will access
  34. * the memory.
  35. * @perms: The access permissions for the VMID in @vmid. This flag is
  36. * interpreted as a bitmap, and thus, should be a combination of one or more
  37. * of the MEM_BUF_PERM_FLAG_* flags.
  38. */
  39. struct acl_entry {
  40. __u32 vmid;
  41. __u32 perms;
  42. };
  43. /**
  44. * struct mem_buf_ion_data: Data that is unique to memory that is of type
  45. * MEM_BUF_ION_MEM_TYPE.
  46. * @heap_id: The heap ID of where memory should be allocated from or added to.
  47. */
  48. struct mem_buf_ion_data {
  49. __u32 heap_id;
  50. };
  51. #define MEM_BUF_MAX_DMAHEAP_NAME_LEN 128
  52. /**
  53. * struct mem_buf_dmaheap_data: Data that is unique to memory that is of type
  54. * MEM_BUF_DMAHEAP_MEM_TYPE.
  55. * @heap_name: array of characters containing the heap name.
  56. */
  57. struct mem_buf_dmaheap_data {
  58. __u64 heap_name;
  59. };
  60. /**
  61. * struct mem_buf_alloc_ioctl_arg: A request to allocate memory from another
  62. * VM to other VMs.
  63. * @size: The size of the allocation.
  64. * @acl_list: An array of structures, where each structure specifies a VMID
  65. * and the access permissions that the VMID will have to the memory to be
  66. * allocated.
  67. * @nr_acl_entries: The number of ACL entries in @acl_list.
  68. * @src_mem_type: The type of memory that the source VM should allocate from.
  69. * This should be one of the mem_buf_mem_type enum values.
  70. * @src_data: A pointer to data that the source VM should interpret when
  71. * performing the allocation.
  72. * @dst_mem_type: The type of memory that the destination VM should treat the
  73. * incoming allocation from the source VM as. This should be one of the
  74. * mem_buf_mem_type enum values.
  75. * @mem_buf_fd: A file descriptor representing the memory that was allocated
  76. * from the source VM and added to the current VM. Calling close() on this file
  77. * descriptor will deallocate the memory from the current VM, and return it
  78. * to the source VM.
  79. * * @dst_data: A pointer to data that the destination VM should interpret when
  80. * adding the memory to the current VM.
  81. *
  82. * All reserved fields must be zeroed out by the caller prior to invoking the
  83. * allocation IOCTL command with this argument.
  84. */
  85. struct mem_buf_alloc_ioctl_arg {
  86. __u64 size;
  87. __u64 acl_list;
  88. __u32 nr_acl_entries;
  89. __u32 src_mem_type;
  90. __u64 src_data;
  91. __u32 dst_mem_type;
  92. __u32 mem_buf_fd;
  93. __u64 dst_data;
  94. __u64 reserved0;
  95. __u64 reserved1;
  96. __u64 reserved2;
  97. };
  98. #define MEM_BUF_IOC_ALLOC _IOWR(MEM_BUF_IOC_MAGIC, 0,\
  99. struct mem_buf_alloc_ioctl_arg)
  100. /**
  101. * struct mem_buf_lend_ioctl_arg: A request to lend memory from the local VM
  102. * VM to one or more remote VMs.
  103. * @dma_buf_fd: The fd of the dma-buf that will be exported to another VM.
  104. * @nr_acl_entries: The number of ACL entries in @acl_list.
  105. * @acl_list: An array of structures, where each structure specifies a VMID
  106. * and the access permissions that the VMID will have to the memory to be
  107. * exported. Must not include the local VMID.
  108. * @memparcel_hdl: The handle associated with the memparcel that was created by
  109. * granting access to the dma-buf for the VMIDs specified in @acl_list.
  110. *
  111. * All reserved fields must be zeroed out by the caller prior to invoking the
  112. * import IOCTL command with this argument.
  113. */
  114. struct mem_buf_lend_ioctl_arg {
  115. __u32 dma_buf_fd;
  116. __u32 nr_acl_entries;
  117. __u64 acl_list;
  118. __u64 memparcel_hdl;
  119. __u64 reserved0;
  120. __u64 reserved1;
  121. __u64 reserved2;
  122. };
  123. #define MEM_BUF_IOC_LEND _IOWR(MEM_BUF_IOC_MAGIC, 3,\
  124. struct mem_buf_lend_ioctl_arg)
  125. #define MEM_BUF_VALID_FD_FLAGS (O_CLOEXEC | O_ACCMODE)
  126. /**
  127. * struct mem_buf_retrieve_ioctl_arg: A request to retrieve memory from another
  128. * VM as a dma-buf
  129. * @sender_vm_fd: An open file descriptor identifing the VM who sent the handle.
  130. * @nr_acl_entries: The number of ACL entries in @acl_list.
  131. * @acl_list: An array of structures, where each structure specifies a VMID
  132. * and the access permissions that the VMID should have for the memparcel.
  133. * @memparcel_hdl: The handle that corresponds to the memparcel we are
  134. * importing.
  135. * @dma_buf_import_fd: A dma-buf file descriptor that the client can use to
  136. * access the buffer. This fd must be closed to release the memory.
  137. * @fd_flags: file descriptor flags used when allocating
  138. *
  139. * All reserved fields must be zeroed out by the caller prior to invoking the
  140. * import IOCTL command with this argument.
  141. */
  142. struct mem_buf_retrieve_ioctl_arg {
  143. __u32 sender_vm_fd;
  144. __u32 nr_acl_entries;
  145. __u64 acl_list;
  146. __u64 memparcel_hdl;
  147. __u32 dma_buf_import_fd;
  148. __u32 fd_flags;
  149. __u64 reserved0;
  150. __u64 reserved1;
  151. __u64 reserved2;
  152. };
  153. #define MEM_BUF_IOC_RETRIEVE _IOWR(MEM_BUF_IOC_MAGIC, 4,\
  154. struct mem_buf_retrieve_ioctl_arg)
  155. /**
  156. * struct mem_buf_reclaim_ioctl_arg: A request to reclaim memory from another
  157. * VM. The other VM must have relinquished access, and the current VM must be
  158. * the original owner of the memory. The dma-buf file will not be closed by
  159. * this operation.
  160. * @memparcel_hdl: The handle that corresponds to the memparcel we are
  161. * reclaiming.
  162. * @dma_buf_fd: A dma-buf file descriptor that the client can use to
  163. * access the buffer.
  164. *
  165. * All reserved fields must be zeroed out by the caller prior to invoking the
  166. * import IOCTL command with this argument.
  167. */
  168. struct mem_buf_reclaim_ioctl_arg {
  169. __u64 memparcel_hdl;
  170. __u32 dma_buf_fd;
  171. __u32 reserved0;
  172. __u64 reserved1;
  173. __u64 reserved2;
  174. };
  175. #define MEM_BUF_IOC_RECLAIM _IOWR(MEM_BUF_IOC_MAGIC, 3,\
  176. struct mem_buf_reclaim_ioctl_arg)
  177. /**
  178. * struct mem_buf_share_ioctl_arg: An request to share memory between the
  179. * local VM and one or more remote VMs.
  180. * @dma_buf_fd: The fd of the dma-buf that will be exported to another VM.
  181. * @nr_acl_entries: The number of ACL entries in @acl_list.
  182. * @acl_list: An array of structures, where each structure specifies a VMID
  183. * and the access permissions that the VMID will have to the memory to be
  184. * exported. Must include the local VMID.
  185. * @memparcel_hdl: The handle associated with the memparcel that was created by
  186. * granting access to the dma-buf for the VMIDs specified in @acl_list.
  187. *
  188. * All reserved fields must be zeroed out by the caller prior to invoking the
  189. * import IOCTL command with this argument.
  190. */
  191. struct mem_buf_share_ioctl_arg {
  192. __u32 dma_buf_fd;
  193. __u32 nr_acl_entries;
  194. __u64 acl_list;
  195. __u64 memparcel_hdl;
  196. __u64 reserved0;
  197. __u64 reserved1;
  198. __u64 reserved2;
  199. };
  200. #define MEM_BUF_IOC_SHARE _IOWR(MEM_BUF_IOC_MAGIC, 6,\
  201. struct mem_buf_share_ioctl_arg)
  202. /**
  203. * struct mem_buf_exclusive_owner_ioctl_arg: A request to see if a DMA-BUF
  204. * is owned by and belongs exclusively to this VM.
  205. * @dma_buf_fd: The fd of the dma-buf the user wants to obtain information on
  206. * @is_exclusive_owner:
  207. */
  208. struct mem_buf_exclusive_owner_ioctl_arg {
  209. __u32 dma_buf_fd;
  210. __u32 is_exclusive_owner;
  211. };
  212. #define MEM_BUF_IOC_EXCLUSIVE_OWNER _IOWR(MEM_BUF_IOC_MAGIC, 2,\
  213. struct mem_buf_exclusive_owner_ioctl_arg)
  214. /**
  215. * struct mem_buf_get_memparcel_hdl_ioctl_arg: A request to get the Gunyah
  216. * memparcel handle from a DMA-BUF, given that it has one.
  217. * @memparcel_hdl: The handle associated with the DMA-BUF, if it exists
  218. * @dma_buf_fd: The fd of the dma-buf the user wants to obtain a handle from
  219. */
  220. struct mem_buf_get_memparcel_hdl_ioctl_arg {
  221. __u64 memparcel_hdl;
  222. __u32 dma_buf_fd;
  223. __u32 padding;
  224. };
  225. #define MEM_BUF_IOC_GET_MEMPARCEL_HDL _IOWR(MEM_BUF_IOC_MAGIC, 5,\
  226. struct mem_buf_get_memparcel_hdl_ioctl_arg)
  227. #endif /* _UAPI_LINUX_MEM_BUF_H */