msm_gem_prime.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (c) 2018-2021, The Linux Foundation. All rights reserved.
  3. * Copyright (C) 2013 Red Hat
  4. * Author: Rob Clark <[email protected]>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "msm_drv.h"
  19. #include "msm_gem.h"
  20. #include "msm_mmu.h"
  21. #include "msm_kms.h"
  22. #include <drm/drm_drv.h>
  23. #include <linux/qcom-dma-mapping.h>
  24. #include <linux/dma-buf.h>
  25. #include <linux/ion.h>
  26. #include <linux/msm_ion.h>
  27. struct sg_table *msm_gem_prime_get_sg_table(struct drm_gem_object *obj)
  28. {
  29. struct msm_gem_object *msm_obj = to_msm_bo(obj);
  30. int npages = obj->size >> PAGE_SHIFT;
  31. if (WARN_ON(!msm_obj->pages)) /* should have already pinned! */
  32. return NULL;
  33. return drm_prime_pages_to_sg(obj->dev, msm_obj->pages, npages);
  34. }
  35. void *msm_gem_prime_vmap(struct drm_gem_object *obj)
  36. {
  37. return msm_gem_get_vaddr(obj);
  38. }
  39. void msm_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
  40. {
  41. msm_gem_put_vaddr(obj);
  42. }
  43. int msm_gem_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
  44. {
  45. int ret;
  46. ret = drm_gem_mmap_obj(obj, obj->size, vma);
  47. if (ret < 0)
  48. return ret;
  49. return msm_gem_mmap_obj(vma->vm_private_data, vma);
  50. }
  51. struct drm_gem_object *msm_gem_prime_import_sg_table(struct drm_device *dev,
  52. struct dma_buf_attachment *attach, struct sg_table *sg)
  53. {
  54. return msm_gem_import(dev, attach->dmabuf, sg);
  55. }
  56. int msm_gem_prime_pin(struct drm_gem_object *obj)
  57. {
  58. if (!obj->import_attach)
  59. msm_gem_get_pages(obj);
  60. return 0;
  61. }
  62. void msm_gem_prime_unpin(struct drm_gem_object *obj)
  63. {
  64. if (!obj->import_attach)
  65. msm_gem_put_pages(obj);
  66. }
  67. struct dma_resv *msm_gem_prime_res_obj(struct drm_gem_object *obj)
  68. {
  69. struct msm_gem_object *msm_obj = to_msm_bo(obj);
  70. return msm_obj->resv;
  71. }
  72. struct drm_gem_object *msm_gem_prime_import(struct drm_device *dev,
  73. struct dma_buf *dma_buf)
  74. {
  75. struct dma_buf_attachment *attach;
  76. struct sg_table *sgt = NULL;
  77. struct drm_gem_object *obj;
  78. struct device *attach_dev = NULL;
  79. struct msm_drm_private *priv;
  80. struct msm_kms *kms;
  81. int ret;
  82. bool lazy_unmap = true;
  83. u32 domain;
  84. if (!dma_buf || !dev->dev_private)
  85. return ERR_PTR(-EINVAL);
  86. priv = dev->dev_private;
  87. kms = priv->kms;
  88. if (dma_buf->priv && !dma_buf->ops->begin_cpu_access) {
  89. obj = dma_buf->priv;
  90. if (obj->dev == dev) {
  91. /*
  92. * Importing dmabuf exported from out own gem increases
  93. * refcount on gem itself instead of f_count of dmabuf.
  94. */
  95. drm_gem_object_get(obj);
  96. return obj;
  97. }
  98. }
  99. if (!dev->driver->gem_prime_import_sg_table) {
  100. DRM_ERROR("NULL gem_prime_import_sg_table\n");
  101. return ERR_PTR(-EINVAL);
  102. }
  103. get_dma_buf(dma_buf);
  104. if (!kms || !kms->funcs->get_address_space_device) {
  105. DRM_ERROR("invalid kms ops\n");
  106. ret = -EINVAL;
  107. goto fail_put;
  108. }
  109. /*
  110. * - attach default drm device for all S2 only buffers or
  111. * when IOMMU is not available
  112. * - avoid using lazying unmap feature as it doesn't add
  113. * any value without nested translations
  114. */
  115. if (!iommu_present(&platform_bus_type)) {
  116. attach_dev = dev->dev;
  117. lazy_unmap = false;
  118. } else {
  119. domain = MSM_SMMU_DOMAIN_UNSECURE;
  120. attach_dev = kms->funcs->get_address_space_device(kms, domain);
  121. }
  122. /*
  123. * While transitioning from secure use-cases, the secure/non-secure
  124. * context bank might still not be attached back, while the
  125. * prime_fd_to_handle call is made for the next frame. Attach those
  126. * buffers to default drm device and reattaching with the correct
  127. * context-bank will be handled in msm_gem_delayed_import.
  128. */
  129. if (!attach_dev) {
  130. DRM_DEBUG("attaching dma buf with default drm device\n");
  131. attach_dev = dev->dev;
  132. }
  133. attach = dma_buf_attach(dma_buf, attach_dev);
  134. if (IS_ERR(attach)) {
  135. DRM_ERROR("dma_buf_attach failure, err=%ld\n", PTR_ERR(attach));
  136. return ERR_CAST(attach);
  137. }
  138. /*
  139. * For cached buffers where CPU access is required, dma_map_attachment
  140. * must be called now to allow user-space to perform cpu sync begin/end
  141. * otherwise do delayed mapping during the commit.
  142. */
  143. if (lazy_unmap)
  144. attach->dma_map_attrs |= DMA_ATTR_DELAYED_UNMAP;
  145. sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
  146. if (IS_ERR(sgt)) {
  147. ret = PTR_ERR(sgt);
  148. DRM_ERROR(
  149. "dma_buf_map_attachment failure, err=%d\n", ret);
  150. goto fail_detach;
  151. }
  152. /*
  153. * If importing a NULL sg table (i.e. for uncached buffers),
  154. * create a drm gem object with only the dma buf attachment.
  155. */
  156. obj = dev->driver->gem_prime_import_sg_table(dev, attach, sgt);
  157. if (IS_ERR(obj)) {
  158. ret = PTR_ERR(obj);
  159. DRM_ERROR("gem_prime_import_sg_table failure, err=%d\n", ret);
  160. goto fail_unmap;
  161. }
  162. obj->import_attach = attach;
  163. return obj;
  164. fail_unmap:
  165. if (sgt)
  166. dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
  167. fail_detach:
  168. dma_buf_detach(dma_buf, attach);
  169. fail_put:
  170. dma_buf_put(dma_buf);
  171. return ERR_PTR(ret);
  172. }