msm_gem_prime.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Copyright (c) 2022 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. #if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 15, 0))
  29. #include <linux/ion.h>
  30. #include <linux/msm_ion.h>
  31. #endif
  32. struct sg_table *msm_gem_prime_get_sg_table(struct drm_gem_object *obj)
  33. {
  34. struct msm_gem_object *msm_obj = to_msm_bo(obj);
  35. int npages = obj->size >> PAGE_SHIFT;
  36. if (WARN_ON(!msm_obj->pages)) /* should have already pinned! */
  37. return NULL;
  38. return drm_prime_pages_to_sg(obj->dev, msm_obj->pages, npages);
  39. }
  40. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 19, 0))
  41. int msm_gem_prime_vmap(struct drm_gem_object *obj, struct iosys_map *map)
  42. {
  43. map->vaddr = msm_gem_get_vaddr(obj);
  44. return IS_ERR_OR_NULL(map->vaddr);
  45. }
  46. #elif (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0))
  47. int msm_gem_prime_vmap(struct drm_gem_object *obj, struct dma_buf_map *map)
  48. {
  49. map->vaddr = msm_gem_get_vaddr(obj);
  50. return IS_ERR_OR_NULL(map->vaddr);
  51. }
  52. #else
  53. void *msm_gem_prime_vmap(struct drm_gem_object *obj)
  54. {
  55. return msm_gem_get_vaddr(obj);
  56. }
  57. #endif
  58. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 19, 0))
  59. void msm_gem_prime_vunmap(struct drm_gem_object *obj, struct iosys_map *map)
  60. #elif (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0))
  61. void msm_gem_prime_vunmap(struct drm_gem_object *obj, struct dma_buf_map *map)
  62. #else
  63. void msm_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
  64. #endif
  65. {
  66. msm_gem_put_vaddr(obj);
  67. }
  68. int msm_gem_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
  69. {
  70. int ret;
  71. ret = drm_gem_mmap_obj(obj, obj->size, vma);
  72. if (ret < 0)
  73. return ret;
  74. return msm_gem_mmap_obj(vma->vm_private_data, vma);
  75. }
  76. struct drm_gem_object *msm_gem_prime_import_sg_table(struct drm_device *dev,
  77. struct dma_buf_attachment *attach, struct sg_table *sg)
  78. {
  79. return msm_gem_import(dev, attach->dmabuf, sg);
  80. }
  81. int msm_gem_prime_pin(struct drm_gem_object *obj)
  82. {
  83. if (!obj->import_attach)
  84. msm_gem_get_pages(obj);
  85. return 0;
  86. }
  87. void msm_gem_prime_unpin(struct drm_gem_object *obj)
  88. {
  89. if (!obj->import_attach)
  90. msm_gem_put_pages(obj);
  91. }
  92. struct drm_gem_object *msm_gem_prime_import(struct drm_device *dev,
  93. struct dma_buf *dma_buf)
  94. {
  95. struct dma_buf_attachment *attach;
  96. struct sg_table *sgt = NULL;
  97. struct drm_gem_object *obj;
  98. struct device *attach_dev = NULL;
  99. struct msm_drm_private *priv;
  100. struct msm_kms *kms;
  101. int ret;
  102. bool lazy_unmap = true;
  103. u32 domain;
  104. if (!dma_buf || !dev->dev_private)
  105. return ERR_PTR(-EINVAL);
  106. priv = dev->dev_private;
  107. kms = priv->kms;
  108. if (dma_buf->priv && !dma_buf->ops->begin_cpu_access) {
  109. obj = dma_buf->priv;
  110. if (obj->dev == dev) {
  111. /*
  112. * Importing dmabuf exported from out own gem increases
  113. * refcount on gem itself instead of f_count of dmabuf.
  114. */
  115. drm_gem_object_get(obj);
  116. return obj;
  117. }
  118. }
  119. if (!dev->driver->gem_prime_import_sg_table) {
  120. DRM_ERROR("NULL gem_prime_import_sg_table\n");
  121. return ERR_PTR(-EINVAL);
  122. }
  123. get_dma_buf(dma_buf);
  124. if (!kms || !kms->funcs->get_address_space_device) {
  125. DRM_ERROR("invalid kms ops\n");
  126. ret = -EINVAL;
  127. goto fail_put;
  128. }
  129. /*
  130. * - attach default drm device for all S2 only buffers or
  131. * when IOMMU is not available
  132. * - avoid using lazying unmap feature as it doesn't add
  133. * any value without nested translations
  134. */
  135. if (!iommu_present(&platform_bus_type)) {
  136. attach_dev = dev->dev;
  137. lazy_unmap = false;
  138. } else {
  139. domain = MSM_SMMU_DOMAIN_UNSECURE;
  140. attach_dev = kms->funcs->get_address_space_device(kms, domain);
  141. }
  142. /*
  143. * While transitioning from secure use-cases, the secure/non-secure
  144. * context bank might still not be attached back, while the
  145. * prime_fd_to_handle call is made for the next frame. Attach those
  146. * buffers to default drm device and reattaching with the correct
  147. * context-bank will be handled in msm_gem_delayed_import.
  148. */
  149. if (!attach_dev) {
  150. DRM_DEBUG("attaching dma buf with default drm device\n");
  151. attach_dev = dev->dev;
  152. }
  153. attach = dma_buf_attach(dma_buf, attach_dev);
  154. if (IS_ERR(attach)) {
  155. DRM_ERROR("dma_buf_attach failure, err=%ld\n", PTR_ERR(attach));
  156. return ERR_CAST(attach);
  157. }
  158. /*
  159. * For cached buffers where CPU access is required, dma_map_attachment
  160. * must be called now to allow user-space to perform cpu sync begin/end
  161. * otherwise do delayed mapping during the commit.
  162. */
  163. if (lazy_unmap)
  164. attach->dma_map_attrs |= DMA_ATTR_DELAYED_UNMAP;
  165. sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
  166. if (IS_ERR(sgt)) {
  167. ret = PTR_ERR(sgt);
  168. DRM_ERROR(
  169. "dma_buf_map_attachment failure, err=%d\n", ret);
  170. goto fail_detach;
  171. }
  172. /*
  173. * If importing a NULL sg table (i.e. for uncached buffers),
  174. * create a drm gem object with only the dma buf attachment.
  175. */
  176. obj = dev->driver->gem_prime_import_sg_table(dev, attach, sgt);
  177. if (IS_ERR(obj)) {
  178. ret = PTR_ERR(obj);
  179. DRM_ERROR("gem_prime_import_sg_table failure, err=%d\n", ret);
  180. goto fail_unmap;
  181. }
  182. obj->import_attach = attach;
  183. return obj;
  184. fail_unmap:
  185. if (sgt)
  186. dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
  187. fail_detach:
  188. dma_buf_detach(dma_buf, attach);
  189. fail_put:
  190. dma_buf_put(dma_buf);
  191. return ERR_PTR(ret);
  192. }
  193. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 19, 0))
  194. MODULE_IMPORT_NS(DMA_BUF);
  195. #endif