msm_gem_prime.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  3. * Copyright (c) 2018-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (C) 2013 Red Hat
  5. * Author: Rob Clark <[email protected]>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "msm_drv.h"
  20. #include "msm_gem.h"
  21. #include "msm_mmu.h"
  22. #include "msm_kms.h"
  23. #include <linux/module.h>
  24. #include <drm/drm_drv.h>
  25. #include <linux/qcom-dma-mapping.h>
  26. #include <linux/dma-buf.h>
  27. #include <linux/version.h>
  28. #include <linux/mem-buf.h>
  29. #include <soc/qcom/secure_buffer.h>
  30. #if (KERNEL_VERSION(6, 1, 0) <= LINUX_VERSION_CODE)
  31. #include <linux/qti-smmu-proxy-callbacks.h>
  32. #elif (KERNEL_VERSION(5, 15, 0) > LINUX_VERSION_CODE)
  33. #include <linux/ion.h>
  34. #include <linux/msm_ion.h>
  35. #endif
  36. struct sg_table *msm_gem_prime_get_sg_table(struct drm_gem_object *obj)
  37. {
  38. struct msm_gem_object *msm_obj = to_msm_bo(obj);
  39. int npages = obj->size >> PAGE_SHIFT;
  40. if (WARN_ON(!msm_obj->pages)) /* should have already pinned! */
  41. return NULL;
  42. return drm_prime_pages_to_sg(obj->dev, msm_obj->pages, npages);
  43. }
  44. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 19, 0))
  45. int msm_gem_prime_vmap(struct drm_gem_object *obj, struct iosys_map *map)
  46. {
  47. map->vaddr = msm_gem_get_vaddr(obj);
  48. return IS_ERR_OR_NULL(map->vaddr);
  49. }
  50. #elif (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0))
  51. int msm_gem_prime_vmap(struct drm_gem_object *obj, struct dma_buf_map *map)
  52. {
  53. map->vaddr = msm_gem_get_vaddr(obj);
  54. return IS_ERR_OR_NULL(map->vaddr);
  55. }
  56. #else
  57. void *msm_gem_prime_vmap(struct drm_gem_object *obj)
  58. {
  59. return msm_gem_get_vaddr(obj);
  60. }
  61. #endif
  62. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 19, 0))
  63. void msm_gem_prime_vunmap(struct drm_gem_object *obj, struct iosys_map *map)
  64. #elif (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0))
  65. void msm_gem_prime_vunmap(struct drm_gem_object *obj, struct dma_buf_map *map)
  66. #else
  67. void msm_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
  68. #endif
  69. {
  70. msm_gem_put_vaddr(obj);
  71. }
  72. int msm_gem_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
  73. {
  74. int ret;
  75. ret = drm_gem_mmap_obj(obj, obj->size, vma);
  76. if (ret < 0)
  77. return ret;
  78. return msm_gem_mmap_obj(vma->vm_private_data, vma);
  79. }
  80. struct drm_gem_object *msm_gem_prime_import_sg_table(struct drm_device *dev,
  81. struct dma_buf_attachment *attach, struct sg_table *sg)
  82. {
  83. return msm_gem_import(dev, attach->dmabuf, sg);
  84. }
  85. int msm_gem_prime_pin(struct drm_gem_object *obj)
  86. {
  87. if (!obj->import_attach)
  88. msm_gem_get_pages(obj);
  89. return 0;
  90. }
  91. void msm_gem_prime_unpin(struct drm_gem_object *obj)
  92. {
  93. if (!obj->import_attach)
  94. msm_gem_put_pages(obj);
  95. }
  96. struct drm_gem_object *msm_gem_prime_import(struct drm_device *dev,
  97. struct dma_buf *dma_buf)
  98. {
  99. struct dma_buf_attachment *attach;
  100. struct sg_table *sgt = NULL;
  101. struct drm_gem_object *obj;
  102. struct device *attach_dev = NULL;
  103. struct msm_drm_private *priv;
  104. struct msm_kms *kms;
  105. bool lazy_unmap = true;
  106. bool is_vmid_tvm = false, is_vmid_cp_pixel = false;
  107. bool is_vmid_sec_display = false, is_vmid_cam_preview = false;
  108. int *vmid_list, *perms_list;
  109. int nelems = 0, i, ret;
  110. unsigned long dma_map_attrs = 0;
  111. if (!dma_buf || !dev->dev_private)
  112. return ERR_PTR(-EINVAL);
  113. priv = dev->dev_private;
  114. kms = priv->kms;
  115. if (dma_buf->priv && !dma_buf->ops->begin_cpu_access) {
  116. obj = dma_buf->priv;
  117. if (obj->dev == dev) {
  118. /*
  119. * Importing dmabuf exported from out own gem increases
  120. * refcount on gem itself instead of f_count of dmabuf.
  121. */
  122. drm_gem_object_get(obj);
  123. return obj;
  124. }
  125. }
  126. if (!dev->driver->gem_prime_import_sg_table) {
  127. DRM_ERROR("NULL gem_prime_import_sg_table\n");
  128. return ERR_PTR(-EINVAL);
  129. }
  130. get_dma_buf(dma_buf);
  131. if (!kms || !kms->funcs->get_address_space_device) {
  132. DRM_ERROR("invalid kms ops\n");
  133. ret = -EINVAL;
  134. goto fail_put;
  135. }
  136. ret = mem_buf_dma_buf_copy_vmperm(dma_buf, &vmid_list, &perms_list, &nelems);
  137. if (ret) {
  138. DRM_ERROR("get vmid list failure, ret:%d", ret);
  139. goto fail_put;
  140. }
  141. for (i = 0; i < nelems; i++) {
  142. #if (KERNEL_VERSION(6, 1, 0) <= LINUX_VERSION_CODE)
  143. /* avoid VMID checks in trusted-vm, set flag in HLOS when only VMID_TVM is set */
  144. if ((vmid_list[i] == VMID_TVM) &&
  145. (!kms->funcs->in_trusted_vm || !kms->funcs->in_trusted_vm(kms))) {
  146. is_vmid_tvm = true;
  147. dma_map_attrs = DMA_ATTR_QTI_SMMU_PROXY_MAP;
  148. }
  149. #endif
  150. if (vmid_list[i] == VMID_CP_PIXEL) {
  151. is_vmid_cp_pixel = true;
  152. is_vmid_tvm = false;
  153. dma_map_attrs = 0;
  154. break;
  155. } else if (vmid_list[i] == VMID_CP_CAMERA_PREVIEW) {
  156. is_vmid_cam_preview = true;
  157. break;
  158. } else if (vmid_list[i] == VMID_CP_SEC_DISPLAY) {
  159. is_vmid_sec_display = true;
  160. break;
  161. }
  162. }
  163. /* mem_buf_dma_buf_copy_vmperm uses kmemdup, do kfree to free up the memory */
  164. kfree(vmid_list);
  165. kfree(perms_list);
  166. /*
  167. * - attach default drm device for VMID_TVM-only or when IOMMU is not available
  168. * - avoid using lazy unmap feature as it doesn't add value without nested translations
  169. */
  170. if (is_vmid_cp_pixel) {
  171. attach_dev = kms->funcs->get_address_space_device(kms, MSM_SMMU_DOMAIN_SECURE);
  172. } else if (!iommu_present(&platform_bus_type) || is_vmid_tvm || is_vmid_cam_preview
  173. || is_vmid_sec_display) {
  174. attach_dev = dev->dev;
  175. lazy_unmap = false;
  176. } else {
  177. attach_dev = kms->funcs->get_address_space_device(kms, MSM_SMMU_DOMAIN_UNSECURE);
  178. }
  179. /*
  180. * While transitioning from secure use-cases, the secure/non-secure
  181. * context bank might still not be attached back, while the
  182. * prime_fd_to_handle call is made for the next frame. Attach those
  183. * buffers to default drm device and reattaching with the correct
  184. * context-bank will be handled in msm_gem_delayed_import.
  185. */
  186. if (!attach_dev) {
  187. DRM_DEBUG("attaching dma buf with default drm device\n");
  188. attach_dev = dev->dev;
  189. }
  190. attach = dma_buf_attach(dma_buf, attach_dev);
  191. if (IS_ERR(attach)) {
  192. DRM_ERROR("dma_buf_attach failure, err=%ld\n", PTR_ERR(attach));
  193. return ERR_CAST(attach);
  194. }
  195. /*
  196. * For cached buffers where CPU access is required, dma_map_attachment
  197. * must be called now to allow user-space to perform cpu sync begin/end
  198. * otherwise do delayed mapping during the commit.
  199. */
  200. if (lazy_unmap)
  201. attach->dma_map_attrs |= DMA_ATTR_DELAYED_UNMAP;
  202. attach->dma_map_attrs |= dma_map_attrs;
  203. /*
  204. * avoid map_attachment for S2-only buffers and TVM buffers as it needs to be mapped
  205. * after the SID switch scm_call and will be handled during msm_gem_get_dma_addr
  206. */
  207. if (!is_vmid_tvm && !is_vmid_cam_preview && !is_vmid_sec_display) {
  208. sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
  209. if (IS_ERR(sgt)) {
  210. ret = PTR_ERR(sgt);
  211. DRM_ERROR("dma_buf_map_attachment failure, err=%d\n", ret);
  212. goto fail_detach;
  213. }
  214. } else {
  215. DRM_DEBUG("deferring dma_buf_map_attachment; tvm:%d, sec_cam:%d, sec_disp:%d\n",
  216. is_vmid_tvm, is_vmid_cam_preview, is_vmid_sec_display);
  217. }
  218. /*
  219. * If importing a NULL sg table (i.e. for uncached buffers),
  220. * create a drm gem object with only the dma buf attachment.
  221. */
  222. obj = dev->driver->gem_prime_import_sg_table(dev, attach, sgt);
  223. if (IS_ERR(obj)) {
  224. ret = PTR_ERR(obj);
  225. DRM_ERROR("gem_prime_import_sg_table failure, err=%d\n", ret);
  226. goto fail_unmap;
  227. }
  228. obj->import_attach = attach;
  229. return obj;
  230. fail_unmap:
  231. if (sgt)
  232. dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
  233. fail_detach:
  234. dma_buf_detach(dma_buf, attach);
  235. fail_put:
  236. dma_buf_put(dma_buf);
  237. return ERR_PTR(ret);
  238. }
  239. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 19, 0))
  240. MODULE_IMPORT_NS(DMA_BUF);
  241. #endif