mem-buf.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  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 _MEM_BUF_H
  7. #define _MEM_BUF_H
  8. #include <linux/err.h>
  9. #include <linux/errno.h>
  10. #include <linux/gunyah/gh_rm_drv.h>
  11. #include <linux/types.h>
  12. #include <linux/dma-buf.h>
  13. #include <uapi/linux/mem-buf.h>
  14. /* For in-kernel use only, not allowed for userspace ioctl */
  15. #define MEM_BUF_BUDDY_MEM_TYPE (MEM_BUF_ION_MEM_TYPE + 2)
  16. /* Used to obtain the underlying vmperm struct of a DMA-BUF */
  17. struct mem_buf_vmperm *to_mem_buf_vmperm(struct dma_buf *dmabuf);
  18. /* Returns true if the local VM has exclusive access and is the owner */
  19. bool mem_buf_dma_buf_exclusive_owner(struct dma_buf *dmabuf);
  20. /*
  21. * Returns the Virtual Machine vmids & permissions of the dmabuf. Can't be
  22. * modified.
  23. */
  24. int mem_buf_dma_buf_get_vmperm(struct dma_buf *dmabuf, const int **vmids,
  25. const int **perms, int *nr_acl_entries);
  26. /*
  27. * Returns a copy of the Virtual Machine vmids & permissions of the dmabuf.
  28. * The caller must kfree() when finished.
  29. */
  30. int mem_buf_dma_buf_copy_vmperm(struct dma_buf *dmabuf, int **vmids, int **perms,
  31. int *nr_acl_entries);
  32. /*
  33. * Returns 0 if @dmabuf has a valid memparcel handle and stores it in
  34. * memparcel_hdl
  35. */
  36. int mem_buf_dma_buf_get_memparcel_hdl(struct dma_buf *dmabuf,
  37. gh_memparcel_handle_t *memparcel_hdl);
  38. typedef int (*mem_buf_dma_buf_destructor)(void *dtor_data);
  39. int mem_buf_dma_buf_set_destructor(struct dma_buf *dmabuf,
  40. mem_buf_dma_buf_destructor dtor,
  41. void *dtor_data);
  42. /**
  43. * struct mem_buf_allocation_data - Data structure that contains information
  44. * about a memory buffer allocation request.
  45. * @size: The size (in bytes) of the memory to be requested from a remote VM
  46. * @nr_acl_entries: The number of ACL entries in @acl_list
  47. * @acl_list: A list of VMID and permission pairs that describe what VMIDs will
  48. * have access to the memory, and with what permissions
  49. * @trans_type: One of GH_RM_TRANS_TYPE_DONATE/LEND/SHARE
  50. * @sgl_desc: Optional. Requests a specific set of IPA addresses.
  51. * @src_mem_type: The type of memory that the remote VM should allocate
  52. * (e.g. ION memory)
  53. * @src_data: A pointer to memory type specific data that the remote VM may need
  54. * when performing an allocation (e.g. ION memory allocations require a heap ID)
  55. * @dst_mem_type: The type of memory that the native VM wants (e.g. ION memory)
  56. * @dst_data: A pointer to memory type specific data that the native VM may
  57. * need when adding the memory from the remote VM (e.g. ION memory requires a
  58. * heap ID to add the memory to).
  59. */
  60. struct mem_buf_allocation_data {
  61. size_t size;
  62. unsigned int nr_acl_entries;
  63. int *vmids;
  64. int *perms;
  65. u32 trans_type;
  66. struct gh_sgl_desc *sgl_desc;
  67. enum mem_buf_mem_type src_mem_type;
  68. void *src_data;
  69. enum mem_buf_mem_type dst_mem_type;
  70. void *dst_data;
  71. };
  72. struct mem_buf_lend_kernel_arg {
  73. unsigned int nr_acl_entries;
  74. int *vmids;
  75. int *perms;
  76. gh_memparcel_handle_t memparcel_hdl;
  77. u32 flags;
  78. u64 label;
  79. };
  80. int mem_buf_lend(struct dma_buf *dmabuf,
  81. struct mem_buf_lend_kernel_arg *arg);
  82. /*
  83. * mem_buf_share
  84. * Grant the local VM, as well as one or more remote VMs access
  85. * to the dmabuf. The permissions of the local VM default to RWX
  86. * unless otherwise specified.
  87. */
  88. int mem_buf_share(struct dma_buf *dmabuf,
  89. struct mem_buf_lend_kernel_arg *arg);
  90. struct mem_buf_retrieve_kernel_arg {
  91. int sender_vmid;
  92. unsigned int nr_acl_entries;
  93. int *vmids;
  94. int *perms;
  95. gh_memparcel_handle_t memparcel_hdl;
  96. int fd_flags;
  97. };
  98. struct dma_buf *mem_buf_retrieve(struct mem_buf_retrieve_kernel_arg *arg);
  99. int mem_buf_reclaim(struct dma_buf *dmabuf);
  100. #if IS_ENABLED(CONFIG_QCOM_MEM_BUF)
  101. void *mem_buf_alloc(struct mem_buf_allocation_data *alloc_data);
  102. void mem_buf_free(void *membuf);
  103. struct gh_sgl_desc *mem_buf_get_sgl(void *membuf);
  104. int mem_buf_current_vmid(void);
  105. #else
  106. static inline void *mem_buf_alloc(struct mem_buf_allocation_data *alloc_data)
  107. {
  108. return ERR_PTR(-ENODEV);
  109. }
  110. static inline void mem_buf_free(void *membuf) {}
  111. static inline struct gh_sgl_desc *mem_buf_get_sgl(void *membuf)
  112. {
  113. return ERR_PTR(-EINVAL);
  114. }
  115. static inline int mem_buf_current_vmid(void)
  116. {
  117. return -EINVAL;
  118. }
  119. #endif /* CONFIG_QCOM_MEM_BUF */
  120. #ifdef CONFIG_QCOM_MEM_BUF_DEV_GH
  121. int mem_buf_map_mem_s1(struct gh_sgl_desc *sgl_desc);
  122. int mem_buf_unmap_mem_s1(struct gh_sgl_desc *sgl_desc);
  123. #else
  124. static inline int mem_buf_map_mem_s1(struct gh_sgl_desc *sgl_desc)
  125. {
  126. return -EINVAL;
  127. }
  128. static inline int mem_buf_unmap_mem_s1(struct gh_sgl_desc *sgl_desc)
  129. {
  130. return -EINVAL;
  131. }
  132. #endif /* CONFIG_QCOM_MEM_BUF_DEV_GH */
  133. #endif /* _MEM_BUF_H */