exynos_drm_dmabuf.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /* exynos_drm_dmabuf.c
  2. *
  3. * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  4. * Author: Inki Dae <inki.dae@samsung.com>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the next
  14. * paragraph) shall be included in all copies or substantial portions of the
  15. * Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  21. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  22. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23. * OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #include <drm/drmP.h>
  26. #include <drm/exynos_drm.h>
  27. #include "exynos_drm_drv.h"
  28. #include "exynos_drm_gem.h"
  29. #include <linux/dma-buf.h>
  30. static struct sg_table *exynos_get_sgt(struct drm_device *drm_dev,
  31. struct exynos_drm_gem_buf *buf)
  32. {
  33. struct sg_table *sgt = NULL;
  34. int ret;
  35. sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
  36. if (!sgt)
  37. goto out;
  38. ret = dma_get_sgtable(drm_dev->dev, sgt, buf->kvaddr,
  39. buf->dma_addr, buf->size);
  40. if (ret < 0) {
  41. DRM_ERROR("failed to get sgtable.\n");
  42. goto err_free_sgt;
  43. }
  44. return sgt;
  45. err_free_sgt:
  46. kfree(sgt);
  47. sgt = NULL;
  48. out:
  49. return NULL;
  50. }
  51. static struct sg_table *
  52. exynos_gem_map_dma_buf(struct dma_buf_attachment *attach,
  53. enum dma_data_direction dir)
  54. {
  55. struct exynos_drm_gem_obj *gem_obj = attach->dmabuf->priv;
  56. struct drm_device *dev = gem_obj->base.dev;
  57. struct exynos_drm_gem_buf *buf;
  58. struct sg_table *sgt = NULL;
  59. int nents;
  60. DRM_DEBUG_PRIME("%s\n", __FILE__);
  61. buf = gem_obj->buffer;
  62. if (!buf) {
  63. DRM_ERROR("buffer is null.\n");
  64. return sgt;
  65. }
  66. mutex_lock(&dev->struct_mutex);
  67. sgt = exynos_get_sgt(dev, buf);
  68. if (!sgt)
  69. goto err_unlock;
  70. nents = dma_map_sg(attach->dev, sgt->sgl, sgt->nents, dir);
  71. if (!nents) {
  72. DRM_ERROR("failed to map sgl with iommu.\n");
  73. sgt = NULL;
  74. goto err_unlock;
  75. }
  76. DRM_DEBUG_PRIME("buffer size = 0x%lx\n", buf->size);
  77. err_unlock:
  78. mutex_unlock(&dev->struct_mutex);
  79. return sgt;
  80. }
  81. static void exynos_gem_unmap_dma_buf(struct dma_buf_attachment *attach,
  82. struct sg_table *sgt,
  83. enum dma_data_direction dir)
  84. {
  85. dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents, dir);
  86. sg_free_table(sgt);
  87. kfree(sgt);
  88. sgt = NULL;
  89. }
  90. static void exynos_dmabuf_release(struct dma_buf *dmabuf)
  91. {
  92. struct exynos_drm_gem_obj *exynos_gem_obj = dmabuf->priv;
  93. DRM_DEBUG_PRIME("%s\n", __FILE__);
  94. /*
  95. * exynos_dmabuf_release() call means that file object's
  96. * f_count is 0 and it calls drm_gem_object_handle_unreference()
  97. * to drop the references that these values had been increased
  98. * at drm_prime_handle_to_fd()
  99. */
  100. if (exynos_gem_obj->base.export_dma_buf == dmabuf) {
  101. exynos_gem_obj->base.export_dma_buf = NULL;
  102. /*
  103. * drop this gem object refcount to release allocated buffer
  104. * and resources.
  105. */
  106. drm_gem_object_unreference_unlocked(&exynos_gem_obj->base);
  107. }
  108. }
  109. static void *exynos_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf,
  110. unsigned long page_num)
  111. {
  112. /* TODO */
  113. return NULL;
  114. }
  115. static void exynos_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf,
  116. unsigned long page_num,
  117. void *addr)
  118. {
  119. /* TODO */
  120. }
  121. static void *exynos_gem_dmabuf_kmap(struct dma_buf *dma_buf,
  122. unsigned long page_num)
  123. {
  124. /* TODO */
  125. return NULL;
  126. }
  127. static void exynos_gem_dmabuf_kunmap(struct dma_buf *dma_buf,
  128. unsigned long page_num, void *addr)
  129. {
  130. /* TODO */
  131. }
  132. static int exynos_gem_dmabuf_mmap(struct dma_buf *dma_buf,
  133. struct vm_area_struct *vma)
  134. {
  135. return -ENOTTY;
  136. }
  137. static struct dma_buf_ops exynos_dmabuf_ops = {
  138. .map_dma_buf = exynos_gem_map_dma_buf,
  139. .unmap_dma_buf = exynos_gem_unmap_dma_buf,
  140. .kmap = exynos_gem_dmabuf_kmap,
  141. .kmap_atomic = exynos_gem_dmabuf_kmap_atomic,
  142. .kunmap = exynos_gem_dmabuf_kunmap,
  143. .kunmap_atomic = exynos_gem_dmabuf_kunmap_atomic,
  144. .mmap = exynos_gem_dmabuf_mmap,
  145. .release = exynos_dmabuf_release,
  146. };
  147. struct dma_buf *exynos_dmabuf_prime_export(struct drm_device *drm_dev,
  148. struct drm_gem_object *obj, int flags)
  149. {
  150. struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj);
  151. return dma_buf_export(exynos_gem_obj, &exynos_dmabuf_ops,
  152. exynos_gem_obj->base.size, 0600);
  153. }
  154. struct drm_gem_object *exynos_dmabuf_prime_import(struct drm_device *drm_dev,
  155. struct dma_buf *dma_buf)
  156. {
  157. struct dma_buf_attachment *attach;
  158. struct sg_table *sgt;
  159. struct scatterlist *sgl;
  160. struct exynos_drm_gem_obj *exynos_gem_obj;
  161. struct exynos_drm_gem_buf *buffer;
  162. int ret;
  163. DRM_DEBUG_PRIME("%s\n", __FILE__);
  164. /* is this one of own objects? */
  165. if (dma_buf->ops == &exynos_dmabuf_ops) {
  166. struct drm_gem_object *obj;
  167. exynos_gem_obj = dma_buf->priv;
  168. obj = &exynos_gem_obj->base;
  169. /* is it from our device? */
  170. if (obj->dev == drm_dev) {
  171. drm_gem_object_reference(obj);
  172. return obj;
  173. }
  174. }
  175. attach = dma_buf_attach(dma_buf, drm_dev->dev);
  176. if (IS_ERR(attach))
  177. return ERR_PTR(-EINVAL);
  178. sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
  179. if (IS_ERR_OR_NULL(sgt)) {
  180. ret = PTR_ERR(sgt);
  181. goto err_buf_detach;
  182. }
  183. buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
  184. if (!buffer) {
  185. DRM_ERROR("failed to allocate exynos_drm_gem_buf.\n");
  186. ret = -ENOMEM;
  187. goto err_unmap_attach;
  188. }
  189. exynos_gem_obj = exynos_drm_gem_init(drm_dev, dma_buf->size);
  190. if (!exynos_gem_obj) {
  191. ret = -ENOMEM;
  192. goto err_free_buffer;
  193. }
  194. sgl = sgt->sgl;
  195. buffer->size = dma_buf->size;
  196. buffer->dma_addr = sg_dma_address(sgl);
  197. if (sgt->nents == 1) {
  198. /* always physically continuous memory if sgt->nents is 1. */
  199. exynos_gem_obj->flags |= EXYNOS_BO_CONTIG;
  200. } else {
  201. /*
  202. * this case could be CONTIG or NONCONTIG type but for now
  203. * sets NONCONTIG.
  204. * TODO. we have to find a way that exporter can notify
  205. * the type of its own buffer to importer.
  206. */
  207. exynos_gem_obj->flags |= EXYNOS_BO_NONCONTIG;
  208. }
  209. exynos_gem_obj->buffer = buffer;
  210. buffer->sgt = sgt;
  211. exynos_gem_obj->base.import_attach = attach;
  212. DRM_DEBUG_PRIME("dma_addr = 0x%x, size = 0x%lx\n", buffer->dma_addr,
  213. buffer->size);
  214. return &exynos_gem_obj->base;
  215. err_free_buffer:
  216. kfree(buffer);
  217. buffer = NULL;
  218. err_unmap_attach:
  219. dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
  220. err_buf_detach:
  221. dma_buf_detach(dma_buf, attach);
  222. return ERR_PTR(ret);
  223. }
  224. MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
  225. MODULE_DESCRIPTION("Samsung SoC DRM DMABUF Module");
  226. MODULE_LICENSE("GPL");