msm_gem_prime.c 5.1 KB

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