udl_dmabuf.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * udl_dmabuf.c
  3. *
  4. * Copyright (c) 2014 The Chromium OS Authors
  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. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <drm/drmP.h>
  20. #include "udl_drv.h"
  21. #include <linux/shmem_fs.h>
  22. #include <linux/dma-buf.h>
  23. struct udl_drm_dmabuf_attachment {
  24. struct sg_table sgt;
  25. enum dma_data_direction dir;
  26. bool is_mapped;
  27. };
  28. static int udl_attach_dma_buf(struct dma_buf *dmabuf,
  29. struct device *dev,
  30. struct dma_buf_attachment *attach)
  31. {
  32. struct udl_drm_dmabuf_attachment *udl_attach;
  33. DRM_DEBUG_PRIME("[DEV:%s] size:%zd\n", dev_name(attach->dev),
  34. attach->dmabuf->size);
  35. udl_attach = kzalloc(sizeof(*udl_attach), GFP_KERNEL);
  36. if (!udl_attach)
  37. return -ENOMEM;
  38. udl_attach->dir = DMA_NONE;
  39. attach->priv = udl_attach;
  40. return 0;
  41. }
  42. static void udl_detach_dma_buf(struct dma_buf *dmabuf,
  43. struct dma_buf_attachment *attach)
  44. {
  45. struct udl_drm_dmabuf_attachment *udl_attach = attach->priv;
  46. struct sg_table *sgt;
  47. if (!udl_attach)
  48. return;
  49. DRM_DEBUG_PRIME("[DEV:%s] size:%zd\n", dev_name(attach->dev),
  50. attach->dmabuf->size);
  51. sgt = &udl_attach->sgt;
  52. if (udl_attach->dir != DMA_NONE)
  53. dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents,
  54. udl_attach->dir);
  55. sg_free_table(sgt);
  56. kfree(udl_attach);
  57. attach->priv = NULL;
  58. }
  59. static struct sg_table *udl_map_dma_buf(struct dma_buf_attachment *attach,
  60. enum dma_data_direction dir)
  61. {
  62. struct udl_drm_dmabuf_attachment *udl_attach = attach->priv;
  63. struct udl_gem_object *obj = to_udl_bo(attach->dmabuf->priv);
  64. struct drm_device *dev = obj->base.dev;
  65. struct scatterlist *rd, *wr;
  66. struct sg_table *sgt = NULL;
  67. unsigned int i;
  68. int page_count;
  69. int nents, ret;
  70. DRM_DEBUG_PRIME("[DEV:%s] size:%zd dir=%d\n", dev_name(attach->dev),
  71. attach->dmabuf->size, dir);
  72. /* just return current sgt if already requested. */
  73. if (udl_attach->dir == dir && udl_attach->is_mapped)
  74. return &udl_attach->sgt;
  75. if (!obj->pages) {
  76. ret = udl_gem_get_pages(obj);
  77. if (ret) {
  78. DRM_ERROR("failed to map pages.\n");
  79. return ERR_PTR(ret);
  80. }
  81. }
  82. page_count = obj->base.size / PAGE_SIZE;
  83. obj->sg = drm_prime_pages_to_sg(obj->pages, page_count);
  84. if (IS_ERR(obj->sg)) {
  85. DRM_ERROR("failed to allocate sgt.\n");
  86. return ERR_CAST(obj->sg);
  87. }
  88. sgt = &udl_attach->sgt;
  89. ret = sg_alloc_table(sgt, obj->sg->orig_nents, GFP_KERNEL);
  90. if (ret) {
  91. DRM_ERROR("failed to alloc sgt.\n");
  92. return ERR_PTR(-ENOMEM);
  93. }
  94. mutex_lock(&dev->struct_mutex);
  95. rd = obj->sg->sgl;
  96. wr = sgt->sgl;
  97. for (i = 0; i < sgt->orig_nents; ++i) {
  98. sg_set_page(wr, sg_page(rd), rd->length, rd->offset);
  99. rd = sg_next(rd);
  100. wr = sg_next(wr);
  101. }
  102. if (dir != DMA_NONE) {
  103. nents = dma_map_sg(attach->dev, sgt->sgl, sgt->orig_nents, dir);
  104. if (!nents) {
  105. DRM_ERROR("failed to map sgl with iommu.\n");
  106. sg_free_table(sgt);
  107. sgt = ERR_PTR(-EIO);
  108. goto err_unlock;
  109. }
  110. }
  111. udl_attach->is_mapped = true;
  112. udl_attach->dir = dir;
  113. attach->priv = udl_attach;
  114. err_unlock:
  115. mutex_unlock(&dev->struct_mutex);
  116. return sgt;
  117. }
  118. static void udl_unmap_dma_buf(struct dma_buf_attachment *attach,
  119. struct sg_table *sgt,
  120. enum dma_data_direction dir)
  121. {
  122. /* Nothing to do. */
  123. DRM_DEBUG_PRIME("[DEV:%s] size:%zd dir:%d\n", dev_name(attach->dev),
  124. attach->dmabuf->size, dir);
  125. }
  126. static void *udl_dmabuf_kmap(struct dma_buf *dma_buf, unsigned long page_num)
  127. {
  128. /* TODO */
  129. return NULL;
  130. }
  131. static void *udl_dmabuf_kmap_atomic(struct dma_buf *dma_buf,
  132. unsigned long page_num)
  133. {
  134. /* TODO */
  135. return NULL;
  136. }
  137. static void udl_dmabuf_kunmap(struct dma_buf *dma_buf,
  138. unsigned long page_num, void *addr)
  139. {
  140. /* TODO */
  141. }
  142. static void udl_dmabuf_kunmap_atomic(struct dma_buf *dma_buf,
  143. unsigned long page_num,
  144. void *addr)
  145. {
  146. /* TODO */
  147. }
  148. static int udl_dmabuf_mmap(struct dma_buf *dma_buf,
  149. struct vm_area_struct *vma)
  150. {
  151. /* TODO */
  152. return -EINVAL;
  153. }
  154. static struct dma_buf_ops udl_dmabuf_ops = {
  155. .attach = udl_attach_dma_buf,
  156. .detach = udl_detach_dma_buf,
  157. .map_dma_buf = udl_map_dma_buf,
  158. .unmap_dma_buf = udl_unmap_dma_buf,
  159. .kmap = udl_dmabuf_kmap,
  160. .kmap_atomic = udl_dmabuf_kmap_atomic,
  161. .kunmap = udl_dmabuf_kunmap,
  162. .kunmap_atomic = udl_dmabuf_kunmap_atomic,
  163. .mmap = udl_dmabuf_mmap,
  164. .release = drm_gem_dmabuf_release,
  165. };
  166. struct dma_buf *udl_gem_prime_export(struct drm_device *dev,
  167. struct drm_gem_object *obj, int flags)
  168. {
  169. DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
  170. exp_info.ops = &udl_dmabuf_ops;
  171. exp_info.size = obj->size;
  172. exp_info.flags = flags;
  173. exp_info.priv = obj;
  174. return dma_buf_export(&exp_info);
  175. }
  176. static int udl_prime_create(struct drm_device *dev,
  177. size_t size,
  178. struct sg_table *sg,
  179. struct udl_gem_object **obj_p)
  180. {
  181. struct udl_gem_object *obj;
  182. int npages;
  183. npages = size / PAGE_SIZE;
  184. *obj_p = NULL;
  185. obj = udl_gem_alloc_object(dev, npages * PAGE_SIZE);
  186. if (!obj)
  187. return -ENOMEM;
  188. obj->sg = sg;
  189. obj->pages = drm_malloc_ab(npages, sizeof(struct page *));
  190. if (obj->pages == NULL) {
  191. DRM_ERROR("obj pages is NULL %d\n", npages);
  192. return -ENOMEM;
  193. }
  194. drm_prime_sg_to_page_addr_arrays(sg, obj->pages, NULL, npages);
  195. *obj_p = obj;
  196. return 0;
  197. }
  198. struct drm_gem_object *udl_gem_prime_import(struct drm_device *dev,
  199. struct dma_buf *dma_buf)
  200. {
  201. struct dma_buf_attachment *attach;
  202. struct sg_table *sg;
  203. struct udl_gem_object *uobj;
  204. int ret;
  205. /* need to attach */
  206. get_device(dev->dev);
  207. attach = dma_buf_attach(dma_buf, dev->dev);
  208. if (IS_ERR(attach)) {
  209. put_device(dev->dev);
  210. return ERR_CAST(attach);
  211. }
  212. get_dma_buf(dma_buf);
  213. sg = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
  214. if (IS_ERR(sg)) {
  215. ret = PTR_ERR(sg);
  216. goto fail_detach;
  217. }
  218. ret = udl_prime_create(dev, dma_buf->size, sg, &uobj);
  219. if (ret)
  220. goto fail_unmap;
  221. uobj->base.import_attach = attach;
  222. uobj->flags = UDL_BO_WC;
  223. return &uobj->base;
  224. fail_unmap:
  225. dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
  226. fail_detach:
  227. dma_buf_detach(dma_buf, attach);
  228. dma_buf_put(dma_buf);
  229. put_device(dev->dev);
  230. return ERR_PTR(ret);
  231. }