exynos_drm_dmabuf.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 struct dma_buf_ops exynos_dmabuf_ops = {
  137. .map_dma_buf = exynos_gem_map_dma_buf,
  138. .unmap_dma_buf = exynos_gem_unmap_dma_buf,
  139. .kmap = exynos_gem_dmabuf_kmap,
  140. .kmap_atomic = exynos_gem_dmabuf_kmap_atomic,
  141. .kunmap = exynos_gem_dmabuf_kunmap,
  142. .kunmap_atomic = exynos_gem_dmabuf_kunmap_atomic,
  143. .release = exynos_dmabuf_release,
  144. };
  145. struct dma_buf *exynos_dmabuf_prime_export(struct drm_device *drm_dev,
  146. struct drm_gem_object *obj, int flags)
  147. {
  148. struct exynos_drm_gem_obj *exynos_gem_obj = to_exynos_gem_obj(obj);
  149. return dma_buf_export(exynos_gem_obj, &exynos_dmabuf_ops,
  150. exynos_gem_obj->base.size, 0600);
  151. }
  152. struct drm_gem_object *exynos_dmabuf_prime_import(struct drm_device *drm_dev,
  153. struct dma_buf *dma_buf)
  154. {
  155. struct dma_buf_attachment *attach;
  156. struct sg_table *sgt;
  157. struct scatterlist *sgl;
  158. struct exynos_drm_gem_obj *exynos_gem_obj;
  159. struct exynos_drm_gem_buf *buffer;
  160. struct page *page;
  161. int ret;
  162. DRM_DEBUG_PRIME("%s\n", __FILE__);
  163. /* is this one of own objects? */
  164. if (dma_buf->ops == &exynos_dmabuf_ops) {
  165. struct drm_gem_object *obj;
  166. exynos_gem_obj = dma_buf->priv;
  167. obj = &exynos_gem_obj->base;
  168. /* is it from our device? */
  169. if (obj->dev == drm_dev) {
  170. drm_gem_object_reference(obj);
  171. return obj;
  172. }
  173. }
  174. attach = dma_buf_attach(dma_buf, drm_dev->dev);
  175. if (IS_ERR(attach))
  176. return ERR_PTR(-EINVAL);
  177. sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
  178. if (IS_ERR_OR_NULL(sgt)) {
  179. ret = PTR_ERR(sgt);
  180. goto err_buf_detach;
  181. }
  182. buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
  183. if (!buffer) {
  184. DRM_ERROR("failed to allocate exynos_drm_gem_buf.\n");
  185. ret = -ENOMEM;
  186. goto err_unmap_attach;
  187. }
  188. buffer->pages = kzalloc(sizeof(*page) * sgt->nents, GFP_KERNEL);
  189. if (!buffer->pages) {
  190. DRM_ERROR("failed to allocate pages.\n");
  191. ret = -ENOMEM;
  192. goto err_free_buffer;
  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_pages;
  198. }
  199. sgl = sgt->sgl;
  200. if (sgt->nents == 1) {
  201. buffer->dma_addr = sg_dma_address(sgt->sgl);
  202. buffer->size = sg_dma_len(sgt->sgl);
  203. /* always physically continuous memory if sgt->nents is 1. */
  204. exynos_gem_obj->flags |= EXYNOS_BO_CONTIG;
  205. } else {
  206. unsigned int i = 0;
  207. buffer->dma_addr = sg_dma_address(sgl);
  208. while (i < sgt->nents) {
  209. buffer->pages[i] = sg_page(sgl);
  210. buffer->size += sg_dma_len(sgl);
  211. sgl = sg_next(sgl);
  212. i++;
  213. }
  214. exynos_gem_obj->flags |= EXYNOS_BO_NONCONTIG;
  215. }
  216. exynos_gem_obj->buffer = buffer;
  217. buffer->sgt = sgt;
  218. exynos_gem_obj->base.import_attach = attach;
  219. DRM_DEBUG_PRIME("dma_addr = 0x%x, size = 0x%lx\n", buffer->dma_addr,
  220. buffer->size);
  221. return &exynos_gem_obj->base;
  222. err_free_pages:
  223. kfree(buffer->pages);
  224. buffer->pages = NULL;
  225. err_free_buffer:
  226. kfree(buffer);
  227. buffer = NULL;
  228. err_unmap_attach:
  229. dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
  230. err_buf_detach:
  231. dma_buf_detach(dma_buf, attach);
  232. return ERR_PTR(ret);
  233. }
  234. MODULE_AUTHOR("Inki Dae <inki.dae@samsung.com>");
  235. MODULE_DESCRIPTION("Samsung SoC DRM DMABUF Module");
  236. MODULE_LICENSE("GPL");