exynos_drm_dmabuf.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. bool is_mapped;
  20. };
  21. static struct exynos_drm_gem_obj *dma_buf_to_obj(struct dma_buf *buf)
  22. {
  23. return to_exynos_gem_obj(buf->priv);
  24. }
  25. static int exynos_gem_attach_dma_buf(struct dma_buf *dmabuf,
  26. struct device *dev,
  27. struct dma_buf_attachment *attach)
  28. {
  29. struct exynos_drm_dmabuf_attachment *exynos_attach;
  30. exynos_attach = kzalloc(sizeof(*exynos_attach), GFP_KERNEL);
  31. if (!exynos_attach)
  32. return -ENOMEM;
  33. exynos_attach->dir = DMA_NONE;
  34. attach->priv = exynos_attach;
  35. return 0;
  36. }
  37. static void exynos_gem_detach_dma_buf(struct dma_buf *dmabuf,
  38. struct dma_buf_attachment *attach)
  39. {
  40. struct exynos_drm_dmabuf_attachment *exynos_attach = attach->priv;
  41. struct sg_table *sgt;
  42. if (!exynos_attach)
  43. return;
  44. sgt = &exynos_attach->sgt;
  45. if (exynos_attach->dir != DMA_NONE)
  46. dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents,
  47. exynos_attach->dir);
  48. sg_free_table(sgt);
  49. kfree(exynos_attach);
  50. attach->priv = NULL;
  51. }
  52. static struct sg_table *
  53. exynos_gem_map_dma_buf(struct dma_buf_attachment *attach,
  54. enum dma_data_direction dir)
  55. {
  56. struct exynos_drm_dmabuf_attachment *exynos_attach = attach->priv;
  57. struct exynos_drm_gem_obj *gem_obj = dma_buf_to_obj(attach->dmabuf);
  58. struct drm_device *dev = gem_obj->base.dev;
  59. struct exynos_drm_gem_buf *buf;
  60. struct scatterlist *rd, *wr;
  61. struct sg_table *sgt = NULL;
  62. unsigned int i;
  63. int nents, ret;
  64. /* just return current sgt if already requested. */
  65. if (exynos_attach->dir == dir && exynos_attach->is_mapped)
  66. return &exynos_attach->sgt;
  67. buf = gem_obj->buffer;
  68. if (!buf) {
  69. DRM_ERROR("buffer is null.\n");
  70. return ERR_PTR(-ENOMEM);
  71. }
  72. sgt = &exynos_attach->sgt;
  73. ret = sg_alloc_table(sgt, buf->sgt->orig_nents, GFP_KERNEL);
  74. if (ret) {
  75. DRM_ERROR("failed to alloc sgt.\n");
  76. return ERR_PTR(-ENOMEM);
  77. }
  78. mutex_lock(&dev->struct_mutex);
  79. rd = buf->sgt->sgl;
  80. wr = sgt->sgl;
  81. for (i = 0; i < sgt->orig_nents; ++i) {
  82. sg_set_page(wr, sg_page(rd), rd->length, rd->offset);
  83. rd = sg_next(rd);
  84. wr = sg_next(wr);
  85. }
  86. if (dir != DMA_NONE) {
  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. }
  95. exynos_attach->is_mapped = true;
  96. exynos_attach->dir = dir;
  97. attach->priv = exynos_attach;
  98. DRM_DEBUG_PRIME("buffer size = 0x%lx\n", buf->size);
  99. err_unlock:
  100. mutex_unlock(&dev->struct_mutex);
  101. return sgt;
  102. }
  103. static void exynos_gem_unmap_dma_buf(struct dma_buf_attachment *attach,
  104. struct sg_table *sgt,
  105. enum dma_data_direction dir)
  106. {
  107. /* Nothing to do. */
  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. .attach = exynos_gem_attach_dma_buf,
  139. .detach = exynos_gem_detach_dma_buf,
  140. .map_dma_buf = exynos_gem_map_dma_buf,
  141. .unmap_dma_buf = exynos_gem_unmap_dma_buf,
  142. .kmap = exynos_gem_dmabuf_kmap,
  143. .kmap_atomic = exynos_gem_dmabuf_kmap_atomic,
  144. .kunmap = exynos_gem_dmabuf_kunmap,
  145. .kunmap_atomic = exynos_gem_dmabuf_kunmap_atomic,
  146. .mmap = exynos_gem_dmabuf_mmap,
  147. .release = drm_gem_dmabuf_release,
  148. };
  149. struct dma_buf *exynos_dmabuf_prime_export(struct drm_device *drm_dev,
  150. struct drm_gem_object *obj, int flags)
  151. {
  152. struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj);
  153. return dma_buf_export(obj, &exynos_dmabuf_ops,
  154. exynos_gem_obj->base.size, flags);
  155. }
  156. struct drm_gem_object *exynos_dmabuf_prime_import(struct drm_device *drm_dev,
  157. struct dma_buf *dma_buf)
  158. {
  159. struct dma_buf_attachment *attach;
  160. struct sg_table *sgt;
  161. struct scatterlist *sgl;
  162. struct exynos_drm_gem_obj *exynos_gem_obj;
  163. struct exynos_drm_gem_buf *buffer;
  164. int ret;
  165. /* is this one of own objects? */
  166. if (dma_buf->ops == &exynos_dmabuf_ops) {
  167. struct drm_gem_object *obj;
  168. obj = dma_buf->priv;
  169. /* is it from our device? */
  170. if (obj->dev == drm_dev) {
  171. /*
  172. * Importing dmabuf exported from out own gem increases
  173. * refcount on gem itself instead of f_count of dmabuf.
  174. */
  175. drm_gem_object_reference(obj);
  176. return obj;
  177. }
  178. }
  179. attach = dma_buf_attach(dma_buf, drm_dev->dev);
  180. if (IS_ERR(attach))
  181. return ERR_PTR(-EINVAL);
  182. get_dma_buf(dma_buf);
  183. sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
  184. if (IS_ERR_OR_NULL(sgt)) {
  185. ret = PTR_ERR(sgt);
  186. goto err_buf_detach;
  187. }
  188. buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
  189. if (!buffer) {
  190. DRM_ERROR("failed to allocate exynos_drm_gem_buf.\n");
  191. ret = -ENOMEM;
  192. goto err_unmap_attach;
  193. }
  194. exynos_gem_obj = exynos_drm_gem_init(drm_dev, dma_buf->size);
  195. if (!exynos_gem_obj) {
  196. ret = -ENOMEM;
  197. goto err_free_buffer;
  198. }
  199. sgl = sgt->sgl;
  200. buffer->size = dma_buf->size;
  201. buffer->dma_addr = sg_dma_address(sgl);
  202. if (sgt->nents == 1) {
  203. /* always physically continuous memory if sgt->nents is 1. */
  204. exynos_gem_obj->flags |= EXYNOS_BO_CONTIG;
  205. } else {
  206. /*
  207. * this case could be CONTIG or NONCONTIG type but for now
  208. * sets NONCONTIG.
  209. * TODO. we have to find a way that exporter can notify
  210. * the type of its own buffer to importer.
  211. */
  212. exynos_gem_obj->flags |= EXYNOS_BO_NONCONTIG;
  213. }
  214. exynos_gem_obj->buffer = buffer;
  215. buffer->sgt = sgt;
  216. exynos_gem_obj->base.import_attach = attach;
  217. DRM_DEBUG_PRIME("dma_addr = 0x%x, size = 0x%lx\n", buffer->dma_addr,
  218. buffer->size);
  219. return &exynos_gem_obj->base;
  220. err_free_buffer:
  221. kfree(buffer);
  222. buffer = NULL;
  223. err_unmap_attach:
  224. dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
  225. err_buf_detach:
  226. dma_buf_detach(dma_buf, attach);
  227. dma_buf_put(dma_buf);
  228. return ERR_PTR(ret);
  229. }
  230. MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
  231. MODULE_DESCRIPTION("Samsung SoC DRM DMABUF Module");
  232. MODULE_LICENSE("GPL");