msm_gem_prime.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright (c) 2018-2019, 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 <linux/dma-buf.h>
  23. #include <linux/ion.h>
  24. #include <linux/msm_ion.h>
  25. struct sg_table *msm_gem_prime_get_sg_table(struct drm_gem_object *obj)
  26. {
  27. struct msm_gem_object *msm_obj = to_msm_bo(obj);
  28. int npages = obj->size >> PAGE_SHIFT;
  29. if (WARN_ON(!msm_obj->pages)) /* should have already pinned! */
  30. return NULL;
  31. return drm_prime_pages_to_sg(msm_obj->pages, npages);
  32. }
  33. void *msm_gem_prime_vmap(struct drm_gem_object *obj)
  34. {
  35. return msm_gem_get_vaddr(obj);
  36. }
  37. void msm_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
  38. {
  39. msm_gem_put_vaddr(obj);
  40. }
  41. int msm_gem_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
  42. {
  43. int ret;
  44. ret = drm_gem_mmap_obj(obj, obj->size, vma);
  45. if (ret < 0)
  46. return ret;
  47. return msm_gem_mmap_obj(vma->vm_private_data, vma);
  48. }
  49. struct drm_gem_object *msm_gem_prime_import_sg_table(struct drm_device *dev,
  50. struct dma_buf_attachment *attach, struct sg_table *sg)
  51. {
  52. return msm_gem_import(dev, attach->dmabuf, sg);
  53. }
  54. int msm_gem_prime_pin(struct drm_gem_object *obj)
  55. {
  56. if (!obj->import_attach)
  57. msm_gem_get_pages(obj);
  58. return 0;
  59. }
  60. void msm_gem_prime_unpin(struct drm_gem_object *obj)
  61. {
  62. if (!obj->import_attach)
  63. msm_gem_put_pages(obj);
  64. }
  65. struct dma_resv *msm_gem_prime_res_obj(struct drm_gem_object *obj)
  66. {
  67. struct msm_gem_object *msm_obj = to_msm_bo(obj);
  68. return msm_obj->resv;
  69. }
  70. struct drm_gem_object *msm_gem_prime_import(struct drm_device *dev,
  71. struct dma_buf *dma_buf)
  72. {
  73. struct dma_buf_attachment *attach;
  74. struct sg_table *sgt = NULL;
  75. struct drm_gem_object *obj;
  76. struct device *attach_dev = NULL;
  77. unsigned long flags = 0;
  78. struct msm_drm_private *priv;
  79. struct msm_kms *kms;
  80. int ret;
  81. if (!dma_buf || !dev->dev_private)
  82. return ERR_PTR(-EINVAL);
  83. priv = dev->dev_private;
  84. kms = priv->kms;
  85. if (dma_buf->priv && !dma_buf->ops->begin_cpu_access) {
  86. obj = dma_buf->priv;
  87. if (obj->dev == dev) {
  88. /*
  89. * Importing dmabuf exported from out own gem increases
  90. * refcount on gem itself instead of f_count of dmabuf.
  91. */
  92. drm_gem_object_get(obj);
  93. return obj;
  94. }
  95. }
  96. if (!dev->driver->gem_prime_import_sg_table) {
  97. DRM_ERROR("NULL gem_prime_import_sg_table\n");
  98. return ERR_PTR(-EINVAL);
  99. }
  100. get_dma_buf(dma_buf);
  101. ret = dma_buf_get_flags(dma_buf, &flags);
  102. if (ret) {
  103. DRM_ERROR("dma_buf_get_flags failure, err=%d\n", ret);
  104. goto fail_put;
  105. }
  106. if (!kms || !kms->funcs->get_address_space_device) {
  107. DRM_ERROR("invalid kms ops\n");
  108. goto fail_put;
  109. }
  110. if (flags & ION_FLAG_SECURE) {
  111. if (flags & ION_FLAG_CP_PIXEL)
  112. attach_dev = kms->funcs->get_address_space_device(kms,
  113. MSM_SMMU_DOMAIN_SECURE);
  114. else if ((flags & ION_FLAG_CP_SEC_DISPLAY)
  115. || (flags & ION_FLAG_CP_CAMERA_PREVIEW))
  116. attach_dev = dev->dev;
  117. else
  118. DRM_ERROR("invalid ion secure flag: 0x%lx\n", flags);
  119. } else {
  120. attach_dev = kms->funcs->get_address_space_device(kms,
  121. MSM_SMMU_DOMAIN_UNSECURE);
  122. }
  123. if (!attach_dev) {
  124. DRM_ERROR("aspace device not found for domain\n");
  125. ret = -EINVAL;
  126. goto fail_put;
  127. }
  128. attach = dma_buf_attach(dma_buf, attach_dev);
  129. if (IS_ERR(attach)) {
  130. DRM_ERROR("dma_buf_attach failure, err=%ld\n", PTR_ERR(attach));
  131. return ERR_CAST(attach);
  132. }
  133. /*
  134. * For cached buffers where CPU access is required, dma_map_attachment
  135. * must be called now to allow user-space to perform cpu sync begin/end
  136. * otherwise do delayed mapping during the commit.
  137. */
  138. if (flags & ION_FLAG_CACHED) {
  139. attach->dma_map_attrs |= DMA_ATTR_DELAYED_UNMAP;
  140. sgt = dma_buf_map_attachment(
  141. attach, DMA_BIDIRECTIONAL);
  142. if (IS_ERR(sgt)) {
  143. ret = PTR_ERR(sgt);
  144. DRM_ERROR(
  145. "dma_buf_map_attachment failure, err=%d\n",
  146. ret);
  147. goto fail_detach;
  148. }
  149. }
  150. /*
  151. * If importing a NULL sg table (i.e. for uncached buffers),
  152. * create a drm gem object with only the dma buf attachment.
  153. */
  154. obj = dev->driver->gem_prime_import_sg_table(dev, attach, sgt);
  155. if (IS_ERR(obj)) {
  156. ret = PTR_ERR(obj);
  157. DRM_ERROR("gem_prime_import_sg_table failure, err=%d\n", ret);
  158. goto fail_unmap;
  159. }
  160. obj->import_attach = attach;
  161. return obj;
  162. fail_unmap:
  163. if (sgt)
  164. dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
  165. fail_detach:
  166. dma_buf_detach(dma_buf, attach);
  167. fail_put:
  168. dma_buf_put(dma_buf);
  169. return ERR_PTR(ret);
  170. }