exynos_drm_dmabuf.c 7.4 KB

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