exynos_drm_dmabuf.c 8.0 KB

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