exynos_drm_dmabuf.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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_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. if (!sgt) {
  74. DRM_DEBUG_PRIME("exynos_pages_to_sg returned NULL!\n");
  75. goto err_unlock;
  76. }
  77. nents = dma_map_sg(attach->dev, sgt->sgl, sgt->nents, dir);
  78. DRM_DEBUG_PRIME("npages = %d buffer size = 0x%lx page_size = 0x%lx\n",
  79. npages, buf->size, buf->page_size);
  80. err_unlock:
  81. mutex_unlock(&dev->struct_mutex);
  82. return sgt;
  83. }
  84. static void exynos_gem_unmap_dma_buf(struct dma_buf_attachment *attach,
  85. struct sg_table *sgt,
  86. enum dma_data_direction dir)
  87. {
  88. dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents, dir);
  89. sg_free_table(sgt);
  90. kfree(sgt);
  91. sgt = NULL;
  92. }
  93. static void exynos_dmabuf_release(struct dma_buf *dmabuf)
  94. {
  95. struct exynos_drm_gem_obj *exynos_gem_obj = dmabuf->priv;
  96. DRM_DEBUG_PRIME("%s\n", __FILE__);
  97. /*
  98. * exynos_dmabuf_release() call means that file object's
  99. * f_count is 0 and it calls drm_gem_object_handle_unreference()
  100. * to drop the references that these values had been increased
  101. * at drm_prime_handle_to_fd()
  102. */
  103. if (exynos_gem_obj->base.export_dma_buf == dmabuf) {
  104. exynos_gem_obj->base.export_dma_buf = NULL;
  105. /*
  106. * drop this gem object refcount to release allocated buffer
  107. * and resources.
  108. */
  109. drm_gem_object_unreference_unlocked(&exynos_gem_obj->base);
  110. }
  111. }
  112. static void *exynos_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf,
  113. unsigned long page_num)
  114. {
  115. /* TODO */
  116. return NULL;
  117. }
  118. static void exynos_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf,
  119. unsigned long page_num,
  120. void *addr)
  121. {
  122. /* TODO */
  123. }
  124. static void *exynos_gem_dmabuf_kmap(struct dma_buf *dma_buf,
  125. unsigned long page_num)
  126. {
  127. /* TODO */
  128. return NULL;
  129. }
  130. static void exynos_gem_dmabuf_kunmap(struct dma_buf *dma_buf,
  131. unsigned long page_num, void *addr)
  132. {
  133. /* TODO */
  134. }
  135. static int exynos_gem_dmabuf_mmap(struct dma_buf *dma_buf,
  136. struct vm_area_struct *vma)
  137. {
  138. return -ENOTTY;
  139. }
  140. static struct dma_buf_ops exynos_dmabuf_ops = {
  141. .map_dma_buf = exynos_gem_map_dma_buf,
  142. .unmap_dma_buf = exynos_gem_unmap_dma_buf,
  143. .kmap = exynos_gem_dmabuf_kmap,
  144. .kmap_atomic = exynos_gem_dmabuf_kmap_atomic,
  145. .kunmap = exynos_gem_dmabuf_kunmap,
  146. .kunmap_atomic = exynos_gem_dmabuf_kunmap_atomic,
  147. .mmap = exynos_gem_dmabuf_mmap,
  148. .release = exynos_dmabuf_release,
  149. };
  150. struct dma_buf *exynos_dmabuf_prime_export(struct drm_device *drm_dev,
  151. struct drm_gem_object *obj, int flags)
  152. {
  153. struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj);
  154. return dma_buf_export(exynos_gem_obj, &exynos_dmabuf_ops,
  155. exynos_gem_obj->base.size, 0600);
  156. }
  157. struct drm_gem_object *exynos_dmabuf_prime_import(struct drm_device *drm_dev,
  158. struct dma_buf *dma_buf)
  159. {
  160. struct dma_buf_attachment *attach;
  161. struct sg_table *sgt;
  162. struct scatterlist *sgl;
  163. struct exynos_drm_gem_obj *exynos_gem_obj;
  164. struct exynos_drm_gem_buf *buffer;
  165. struct page *page;
  166. int ret;
  167. DRM_DEBUG_PRIME("%s\n", __FILE__);
  168. /* is this one of own objects? */
  169. if (dma_buf->ops == &exynos_dmabuf_ops) {
  170. struct drm_gem_object *obj;
  171. exynos_gem_obj = dma_buf->priv;
  172. obj = &exynos_gem_obj->base;
  173. /* is it from our device? */
  174. if (obj->dev == drm_dev) {
  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. sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
  183. if (IS_ERR_OR_NULL(sgt)) {
  184. ret = PTR_ERR(sgt);
  185. goto err_buf_detach;
  186. }
  187. buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
  188. if (!buffer) {
  189. DRM_ERROR("failed to allocate exynos_drm_gem_buf.\n");
  190. ret = -ENOMEM;
  191. goto err_unmap_attach;
  192. }
  193. buffer->pages = kzalloc(sizeof(*page) * sgt->nents, GFP_KERNEL);
  194. if (!buffer->pages) {
  195. DRM_ERROR("failed to allocate pages.\n");
  196. ret = -ENOMEM;
  197. goto err_free_buffer;
  198. }
  199. exynos_gem_obj = exynos_drm_gem_init(drm_dev, dma_buf->size);
  200. if (!exynos_gem_obj) {
  201. ret = -ENOMEM;
  202. goto err_free_pages;
  203. }
  204. sgl = sgt->sgl;
  205. if (sgt->nents == 1) {
  206. buffer->dma_addr = sg_dma_address(sgt->sgl);
  207. buffer->size = sg_dma_len(sgt->sgl);
  208. /* always physically continuous memory if sgt->nents is 1. */
  209. exynos_gem_obj->flags |= EXYNOS_BO_CONTIG;
  210. } else {
  211. unsigned int i = 0;
  212. buffer->dma_addr = sg_dma_address(sgl);
  213. while (i < sgt->nents) {
  214. buffer->pages[i] = sg_page(sgl);
  215. buffer->size += sg_dma_len(sgl);
  216. sgl = sg_next(sgl);
  217. i++;
  218. }
  219. exynos_gem_obj->flags |= EXYNOS_BO_NONCONTIG;
  220. }
  221. exynos_gem_obj->buffer = buffer;
  222. buffer->sgt = sgt;
  223. exynos_gem_obj->base.import_attach = attach;
  224. DRM_DEBUG_PRIME("dma_addr = 0x%x, size = 0x%lx\n", buffer->dma_addr,
  225. buffer->size);
  226. return &exynos_gem_obj->base;
  227. err_free_pages:
  228. kfree(buffer->pages);
  229. buffer->pages = NULL;
  230. err_free_buffer:
  231. kfree(buffer);
  232. buffer = NULL;
  233. err_unmap_attach:
  234. dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
  235. err_buf_detach:
  236. dma_buf_detach(dma_buf, attach);
  237. return ERR_PTR(ret);
  238. }
  239. MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
  240. MODULE_DESCRIPTION("Samsung SoC DRM DMABUF Module");
  241. MODULE_LICENSE("GPL");