exynos_drm_dmabuf.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 "drmP.h"
  26. #include "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_pages_to_sg(struct page **pages, int nr_pages,
  31. unsigned int page_size)
  32. {
  33. struct sg_table *sgt = NULL;
  34. struct scatterlist *sgl;
  35. int i, ret;
  36. sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
  37. if (!sgt)
  38. goto out;
  39. ret = sg_alloc_table(sgt, nr_pages, GFP_KERNEL);
  40. if (ret)
  41. goto err_free_sgt;
  42. if (page_size < PAGE_SIZE)
  43. page_size = PAGE_SIZE;
  44. for_each_sg(sgt->sgl, sgl, nr_pages, i)
  45. sg_set_page(sgl, pages[i], page_size, 0);
  46. return sgt;
  47. err_free_sgt:
  48. kfree(sgt);
  49. sgt = NULL;
  50. out:
  51. return NULL;
  52. }
  53. static struct sg_table *
  54. exynos_gem_map_dma_buf(struct dma_buf_attachment *attach,
  55. enum dma_data_direction dir)
  56. {
  57. struct exynos_drm_gem_obj *gem_obj = attach->dmabuf->priv;
  58. struct drm_device *dev = gem_obj->base.dev;
  59. struct exynos_drm_gem_buf *buf;
  60. struct sg_table *sgt = NULL;
  61. unsigned int npages;
  62. int nents;
  63. DRM_DEBUG_PRIME("%s\n", __FILE__);
  64. mutex_lock(&dev->struct_mutex);
  65. buf = gem_obj->buffer;
  66. /* there should always be pages allocated. */
  67. if (!buf->pages) {
  68. DRM_ERROR("pages is null.\n");
  69. goto err_unlock;
  70. }
  71. npages = buf->size / buf->page_size;
  72. sgt = exynos_pages_to_sg(buf->pages, npages, buf->page_size);
  73. nents = dma_map_sg(attach->dev, sgt->sgl, sgt->nents, dir);
  74. DRM_DEBUG_PRIME("npages = %d buffer size = 0x%lx page_size = 0x%lx\n",
  75. npages, buf->size, buf->page_size);
  76. err_unlock:
  77. mutex_unlock(&dev->struct_mutex);
  78. return sgt;
  79. }
  80. static void exynos_gem_unmap_dma_buf(struct dma_buf_attachment *attach,
  81. struct sg_table *sgt,
  82. enum dma_data_direction dir)
  83. {
  84. dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents, dir);
  85. sg_free_table(sgt);
  86. kfree(sgt);
  87. sgt = NULL;
  88. }
  89. static void exynos_dmabuf_release(struct dma_buf *dmabuf)
  90. {
  91. struct exynos_drm_gem_obj *exynos_gem_obj = dmabuf->priv;
  92. DRM_DEBUG_PRIME("%s\n", __FILE__);
  93. /*
  94. * exynos_dmabuf_release() call means that file object's
  95. * f_count is 0 and it calls drm_gem_object_handle_unreference()
  96. * to drop the references that these values had been increased
  97. * at drm_prime_handle_to_fd()
  98. */
  99. if (exynos_gem_obj->base.export_dma_buf == dmabuf) {
  100. exynos_gem_obj->base.export_dma_buf = NULL;
  101. /*
  102. * drop this gem object refcount to release allocated buffer
  103. * and resources.
  104. */
  105. drm_gem_object_unreference_unlocked(&exynos_gem_obj->base);
  106. }
  107. }
  108. static void *exynos_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf,
  109. unsigned long page_num)
  110. {
  111. /* TODO */
  112. return NULL;
  113. }
  114. static void exynos_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf,
  115. unsigned long page_num,
  116. void *addr)
  117. {
  118. /* TODO */
  119. }
  120. static void *exynos_gem_dmabuf_kmap(struct dma_buf *dma_buf,
  121. unsigned long page_num)
  122. {
  123. /* TODO */
  124. return NULL;
  125. }
  126. static void exynos_gem_dmabuf_kunmap(struct dma_buf *dma_buf,
  127. unsigned long page_num, void *addr)
  128. {
  129. /* TODO */
  130. }
  131. static struct dma_buf_ops exynos_dmabuf_ops = {
  132. .map_dma_buf = exynos_gem_map_dma_buf,
  133. .unmap_dma_buf = exynos_gem_unmap_dma_buf,
  134. .kmap = exynos_gem_dmabuf_kmap,
  135. .kmap_atomic = exynos_gem_dmabuf_kmap_atomic,
  136. .kunmap = exynos_gem_dmabuf_kunmap,
  137. .kunmap_atomic = exynos_gem_dmabuf_kunmap_atomic,
  138. .release = exynos_dmabuf_release,
  139. };
  140. struct dma_buf *exynos_dmabuf_prime_export(struct drm_device *drm_dev,
  141. struct drm_gem_object *obj, int flags)
  142. {
  143. struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj);
  144. return dma_buf_export(exynos_gem_obj, &exynos_dmabuf_ops,
  145. exynos_gem_obj->base.size, 0600);
  146. }
  147. struct drm_gem_object *exynos_dmabuf_prime_import(struct drm_device *drm_dev,
  148. struct dma_buf *dma_buf)
  149. {
  150. struct dma_buf_attachment *attach;
  151. struct sg_table *sgt;
  152. struct scatterlist *sgl;
  153. struct exynos_drm_gem_obj *exynos_gem_obj;
  154. struct exynos_drm_gem_buf *buffer;
  155. struct page *page;
  156. int ret, i = 0;
  157. DRM_DEBUG_PRIME("%s\n", __FILE__);
  158. /* is this one of own objects? */
  159. if (dma_buf->ops == &exynos_dmabuf_ops) {
  160. struct drm_gem_object *obj;
  161. exynos_gem_obj = dma_buf->priv;
  162. obj = &exynos_gem_obj->base;
  163. /* is it from our device? */
  164. if (obj->dev == drm_dev) {
  165. drm_gem_object_reference(obj);
  166. return obj;
  167. }
  168. }
  169. attach = dma_buf_attach(dma_buf, drm_dev->dev);
  170. if (IS_ERR(attach))
  171. return ERR_PTR(-EINVAL);
  172. sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
  173. if (IS_ERR(sgt)) {
  174. ret = PTR_ERR(sgt);
  175. goto err_buf_detach;
  176. }
  177. buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
  178. if (!buffer) {
  179. DRM_ERROR("failed to allocate exynos_drm_gem_buf.\n");
  180. ret = -ENOMEM;
  181. goto err_unmap_attach;
  182. }
  183. buffer->pages = kzalloc(sizeof(*page) * sgt->nents, GFP_KERNEL);
  184. if (!buffer->pages) {
  185. DRM_ERROR("failed to allocate pages.\n");
  186. ret = -ENOMEM;
  187. goto err_free_buffer;
  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_pages;
  193. }
  194. sgl = sgt->sgl;
  195. buffer->dma_addr = sg_dma_address(sgl);
  196. while (i < sgt->nents) {
  197. buffer->pages[i] = sg_page(sgl);
  198. buffer->size += sg_dma_len(sgl);
  199. sgl = sg_next(sgl);
  200. i++;
  201. }
  202. exynos_gem_obj->buffer = buffer;
  203. buffer->sgt = sgt;
  204. exynos_gem_obj->base.import_attach = attach;
  205. DRM_DEBUG_PRIME("dma_addr = 0x%x, size = 0x%lx\n", buffer->dma_addr,
  206. buffer->size);
  207. return &exynos_gem_obj->base;
  208. err_free_pages:
  209. kfree(buffer->pages);
  210. buffer->pages = NULL;
  211. err_free_buffer:
  212. kfree(buffer);
  213. buffer = NULL;
  214. err_unmap_attach:
  215. dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
  216. err_buf_detach:
  217. dma_buf_detach(dma_buf, attach);
  218. return ERR_PTR(ret);
  219. }
  220. MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
  221. MODULE_DESCRIPTION("Samsung SoC DRM DMABUF Module");
  222. MODULE_LICENSE("GPL");