exynos_drm_dmabuf.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /* exynos_drm_dmabuf.c
  2. *
  3. * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  4. * Author: Inki Dae <inki.dae@samsung.com>
  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 as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. */
  11. #include <drm/drmP.h>
  12. #include <drm/exynos_drm.h>
  13. #include "exynos_drm_drv.h"
  14. #include "exynos_drm_gem.h"
  15. #include <linux/dma-buf.h>
  16. struct exynos_drm_dmabuf_attachment {
  17. struct sg_table sgt;
  18. enum dma_data_direction dir;
  19. };
  20. static int exynos_gem_attach_dma_buf(struct dma_buf *dmabuf,
  21. struct device *dev,
  22. struct dma_buf_attachment *attach)
  23. {
  24. struct exynos_drm_dmabuf_attachment *exynos_attach;
  25. exynos_attach = kzalloc(sizeof(*exynos_attach), GFP_KERNEL);
  26. if (!exynos_attach)
  27. return -ENOMEM;
  28. exynos_attach->dir = DMA_NONE;
  29. attach->priv = exynos_attach;
  30. return 0;
  31. }
  32. static void exynos_gem_detach_dma_buf(struct dma_buf *dmabuf,
  33. struct dma_buf_attachment *attach)
  34. {
  35. struct exynos_drm_dmabuf_attachment *exynos_attach = attach->priv;
  36. struct sg_table *sgt;
  37. if (!exynos_attach)
  38. return;
  39. sgt = &exynos_attach->sgt;
  40. if (exynos_attach->dir != DMA_NONE)
  41. dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents,
  42. exynos_attach->dir);
  43. sg_free_table(sgt);
  44. kfree(exynos_attach);
  45. attach->priv = NULL;
  46. }
  47. static struct sg_table *
  48. exynos_gem_map_dma_buf(struct dma_buf_attachment *attach,
  49. enum dma_data_direction dir)
  50. {
  51. struct exynos_drm_dmabuf_attachment *exynos_attach = attach->priv;
  52. struct exynos_drm_gem_obj *gem_obj = attach->dmabuf->priv;
  53. struct drm_device *dev = gem_obj->base.dev;
  54. struct exynos_drm_gem_buf *buf;
  55. struct scatterlist *rd, *wr;
  56. struct sg_table *sgt = NULL;
  57. unsigned int i;
  58. int nents, ret;
  59. DRM_DEBUG_PRIME("%s\n", __FILE__);
  60. if (WARN_ON(dir == DMA_NONE))
  61. return ERR_PTR(-EINVAL);
  62. /* just return current sgt if already requested. */
  63. if (exynos_attach->dir == dir)
  64. return &exynos_attach->sgt;
  65. /* reattaching is not allowed. */
  66. if (WARN_ON(exynos_attach->dir != DMA_NONE))
  67. return ERR_PTR(-EBUSY);
  68. buf = gem_obj->buffer;
  69. if (!buf) {
  70. DRM_ERROR("buffer is null.\n");
  71. return ERR_PTR(-ENOMEM);
  72. }
  73. sgt = &exynos_attach->sgt;
  74. ret = sg_alloc_table(sgt, buf->sgt->orig_nents, GFP_KERNEL);
  75. if (ret) {
  76. DRM_ERROR("failed to alloc sgt.\n");
  77. return ERR_PTR(-ENOMEM);
  78. }
  79. mutex_lock(&dev->struct_mutex);
  80. rd = buf->sgt->sgl;
  81. wr = sgt->sgl;
  82. for (i = 0; i < sgt->orig_nents; ++i) {
  83. sg_set_page(wr, sg_page(rd), rd->length, rd->offset);
  84. rd = sg_next(rd);
  85. wr = sg_next(wr);
  86. }
  87. nents = dma_map_sg(attach->dev, sgt->sgl, sgt->orig_nents, dir);
  88. if (!nents) {
  89. DRM_ERROR("failed to map sgl with iommu.\n");
  90. sg_free_table(sgt);
  91. sgt = ERR_PTR(-EIO);
  92. goto err_unlock;
  93. }
  94. exynos_attach->dir = dir;
  95. attach->priv = exynos_attach;
  96. DRM_DEBUG_PRIME("buffer size = 0x%lx\n", buf->size);
  97. err_unlock:
  98. mutex_unlock(&dev->struct_mutex);
  99. return sgt;
  100. }
  101. static void exynos_gem_unmap_dma_buf(struct dma_buf_attachment *attach,
  102. struct sg_table *sgt,
  103. enum dma_data_direction dir)
  104. {
  105. /* Nothing to do. */
  106. }
  107. static void exynos_dmabuf_release(struct dma_buf *dmabuf)
  108. {
  109. struct exynos_drm_gem_obj *exynos_gem_obj = dmabuf->priv;
  110. DRM_DEBUG_PRIME("%s\n", __FILE__);
  111. /*
  112. * exynos_dmabuf_release() call means that file object's
  113. * f_count is 0 and it calls drm_gem_object_handle_unreference()
  114. * to drop the references that these values had been increased
  115. * at drm_prime_handle_to_fd()
  116. */
  117. if (exynos_gem_obj->base.export_dma_buf == dmabuf) {
  118. exynos_gem_obj->base.export_dma_buf = NULL;
  119. /*
  120. * drop this gem object refcount to release allocated buffer
  121. * and resources.
  122. */
  123. drm_gem_object_unreference_unlocked(&exynos_gem_obj->base);
  124. }
  125. }
  126. static void *exynos_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf,
  127. unsigned long page_num)
  128. {
  129. /* TODO */
  130. return NULL;
  131. }
  132. static void exynos_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf,
  133. unsigned long page_num,
  134. void *addr)
  135. {
  136. /* TODO */
  137. }
  138. static void *exynos_gem_dmabuf_kmap(struct dma_buf *dma_buf,
  139. unsigned long page_num)
  140. {
  141. /* TODO */
  142. return NULL;
  143. }
  144. static void exynos_gem_dmabuf_kunmap(struct dma_buf *dma_buf,
  145. unsigned long page_num, void *addr)
  146. {
  147. /* TODO */
  148. }
  149. static int exynos_gem_dmabuf_mmap(struct dma_buf *dma_buf,
  150. struct vm_area_struct *vma)
  151. {
  152. return -ENOTTY;
  153. }
  154. static struct dma_buf_ops exynos_dmabuf_ops = {
  155. .attach = exynos_gem_attach_dma_buf,
  156. .detach = exynos_gem_detach_dma_buf,
  157. .map_dma_buf = exynos_gem_map_dma_buf,
  158. .unmap_dma_buf = exynos_gem_unmap_dma_buf,
  159. .kmap = exynos_gem_dmabuf_kmap,
  160. .kmap_atomic = exynos_gem_dmabuf_kmap_atomic,
  161. .kunmap = exynos_gem_dmabuf_kunmap,
  162. .kunmap_atomic = exynos_gem_dmabuf_kunmap_atomic,
  163. .mmap = exynos_gem_dmabuf_mmap,
  164. .release = exynos_dmabuf_release,
  165. };
  166. struct dma_buf *exynos_dmabuf_prime_export(struct drm_device *drm_dev,
  167. struct drm_gem_object *obj, int flags)
  168. {
  169. struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj);
  170. return dma_buf_export(exynos_gem_obj, &exynos_dmabuf_ops,
  171. exynos_gem_obj->base.size, flags);
  172. }
  173. struct drm_gem_object *exynos_dmabuf_prime_import(struct drm_device *drm_dev,
  174. struct dma_buf *dma_buf)
  175. {
  176. struct dma_buf_attachment *attach;
  177. struct sg_table *sgt;
  178. struct scatterlist *sgl;
  179. struct exynos_drm_gem_obj *exynos_gem_obj;
  180. struct exynos_drm_gem_buf *buffer;
  181. int ret;
  182. DRM_DEBUG_PRIME("%s\n", __FILE__);
  183. /* is this one of own objects? */
  184. if (dma_buf->ops == &exynos_dmabuf_ops) {
  185. struct drm_gem_object *obj;
  186. exynos_gem_obj = dma_buf->priv;
  187. obj = &exynos_gem_obj->base;
  188. /* is it from our device? */
  189. if (obj->dev == drm_dev) {
  190. /*
  191. * Importing dmabuf exported from out own gem increases
  192. * refcount on gem itself instead of f_count of dmabuf.
  193. */
  194. drm_gem_object_reference(obj);
  195. dma_buf_put(dma_buf);
  196. return obj;
  197. }
  198. }
  199. attach = dma_buf_attach(dma_buf, drm_dev->dev);
  200. if (IS_ERR(attach))
  201. return ERR_PTR(-EINVAL);
  202. sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
  203. if (IS_ERR_OR_NULL(sgt)) {
  204. ret = PTR_ERR(sgt);
  205. goto err_buf_detach;
  206. }
  207. buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
  208. if (!buffer) {
  209. DRM_ERROR("failed to allocate exynos_drm_gem_buf.\n");
  210. ret = -ENOMEM;
  211. goto err_unmap_attach;
  212. }
  213. exynos_gem_obj = exynos_drm_gem_init(drm_dev, dma_buf->size);
  214. if (!exynos_gem_obj) {
  215. ret = -ENOMEM;
  216. goto err_free_buffer;
  217. }
  218. sgl = sgt->sgl;
  219. buffer->size = dma_buf->size;
  220. buffer->dma_addr = sg_dma_address(sgl);
  221. if (sgt->nents == 1) {
  222. /* always physically continuous memory if sgt->nents is 1. */
  223. exynos_gem_obj->flags |= EXYNOS_BO_CONTIG;
  224. } else {
  225. /*
  226. * this case could be CONTIG or NONCONTIG type but for now
  227. * sets NONCONTIG.
  228. * TODO. we have to find a way that exporter can notify
  229. * the type of its own buffer to importer.
  230. */
  231. exynos_gem_obj->flags |= EXYNOS_BO_NONCONTIG;
  232. }
  233. exynos_gem_obj->buffer = buffer;
  234. buffer->sgt = sgt;
  235. exynos_gem_obj->base.import_attach = attach;
  236. DRM_DEBUG_PRIME("dma_addr = 0x%x, size = 0x%lx\n", buffer->dma_addr,
  237. buffer->size);
  238. return &exynos_gem_obj->base;
  239. err_free_buffer:
  240. kfree(buffer);
  241. buffer = NULL;
  242. err_unmap_attach:
  243. dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
  244. err_buf_detach:
  245. dma_buf_detach(dma_buf, attach);
  246. return ERR_PTR(ret);
  247. }
  248. MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
  249. MODULE_DESCRIPTION("Samsung SoC DRM DMABUF Module");
  250. MODULE_LICENSE("GPL");