udl_dmabuf.c 6.5 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 udl_device *udl = dev->dev_private;
  66. struct scatterlist *rd, *wr;
  67. struct sg_table *sgt = NULL;
  68. unsigned int i;
  69. int page_count;
  70. int nents, ret;
  71. DRM_DEBUG_PRIME("[DEV:%s] size:%zd dir=%d\n", dev_name(attach->dev),
  72. attach->dmabuf->size, dir);
  73. /* just return current sgt if already requested. */
  74. if (udl_attach->dir == dir && udl_attach->is_mapped)
  75. return &udl_attach->sgt;
  76. if (!obj->pages) {
  77. ret = udl_gem_get_pages(obj);
  78. if (ret) {
  79. DRM_ERROR("failed to map pages.\n");
  80. return ERR_PTR(ret);
  81. }
  82. }
  83. page_count = obj->base.size / PAGE_SIZE;
  84. obj->sg = drm_prime_pages_to_sg(obj->pages, page_count);
  85. if (IS_ERR(obj->sg)) {
  86. DRM_ERROR("failed to allocate sgt.\n");
  87. return ERR_CAST(obj->sg);
  88. }
  89. sgt = &udl_attach->sgt;
  90. ret = sg_alloc_table(sgt, obj->sg->orig_nents, GFP_KERNEL);
  91. if (ret) {
  92. DRM_ERROR("failed to alloc sgt.\n");
  93. return ERR_PTR(-ENOMEM);
  94. }
  95. mutex_lock(&udl->gem_lock);
  96. rd = obj->sg->sgl;
  97. wr = sgt->sgl;
  98. for (i = 0; i < sgt->orig_nents; ++i) {
  99. sg_set_page(wr, sg_page(rd), rd->length, rd->offset);
  100. rd = sg_next(rd);
  101. wr = sg_next(wr);
  102. }
  103. if (dir != DMA_NONE) {
  104. nents = dma_map_sg(attach->dev, sgt->sgl, sgt->orig_nents, dir);
  105. if (!nents) {
  106. DRM_ERROR("failed to map sgl with iommu.\n");
  107. sg_free_table(sgt);
  108. sgt = ERR_PTR(-EIO);
  109. goto err_unlock;
  110. }
  111. }
  112. udl_attach->is_mapped = true;
  113. udl_attach->dir = dir;
  114. attach->priv = udl_attach;
  115. err_unlock:
  116. mutex_unlock(&udl->gem_lock);
  117. return sgt;
  118. }
  119. static void udl_unmap_dma_buf(struct dma_buf_attachment *attach,
  120. struct sg_table *sgt,
  121. enum dma_data_direction dir)
  122. {
  123. /* Nothing to do. */
  124. DRM_DEBUG_PRIME("[DEV:%s] size:%zd dir:%d\n", dev_name(attach->dev),
  125. attach->dmabuf->size, dir);
  126. }
  127. static void *udl_dmabuf_kmap(struct dma_buf *dma_buf, unsigned long page_num)
  128. {
  129. /* TODO */
  130. return NULL;
  131. }
  132. static void *udl_dmabuf_kmap_atomic(struct dma_buf *dma_buf,
  133. unsigned long page_num)
  134. {
  135. /* TODO */
  136. return NULL;
  137. }
  138. static void udl_dmabuf_kunmap(struct dma_buf *dma_buf,
  139. unsigned long page_num, void *addr)
  140. {
  141. /* TODO */
  142. }
  143. static void udl_dmabuf_kunmap_atomic(struct dma_buf *dma_buf,
  144. unsigned long page_num,
  145. void *addr)
  146. {
  147. /* TODO */
  148. }
  149. static int udl_dmabuf_mmap(struct dma_buf *dma_buf,
  150. struct vm_area_struct *vma)
  151. {
  152. /* TODO */
  153. return -EINVAL;
  154. }
  155. static const struct dma_buf_ops udl_dmabuf_ops = {
  156. .attach = udl_attach_dma_buf,
  157. .detach = udl_detach_dma_buf,
  158. .map_dma_buf = udl_map_dma_buf,
  159. .unmap_dma_buf = udl_unmap_dma_buf,
  160. .map = udl_dmabuf_kmap,
  161. .map_atomic = udl_dmabuf_kmap_atomic,
  162. .unmap = udl_dmabuf_kunmap,
  163. .unmap_atomic = udl_dmabuf_kunmap_atomic,
  164. .mmap = udl_dmabuf_mmap,
  165. .release = drm_gem_dmabuf_release,
  166. };
  167. struct dma_buf *udl_gem_prime_export(struct drm_device *dev,
  168. struct drm_gem_object *obj, int flags)
  169. {
  170. DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
  171. exp_info.ops = &udl_dmabuf_ops;
  172. exp_info.size = obj->size;
  173. exp_info.flags = flags;
  174. exp_info.priv = obj;
  175. return drm_gem_dmabuf_export(dev, &exp_info);
  176. }
  177. static int udl_prime_create(struct drm_device *dev,
  178. size_t size,
  179. struct sg_table *sg,
  180. struct udl_gem_object **obj_p)
  181. {
  182. struct udl_gem_object *obj;
  183. int npages;
  184. npages = size / PAGE_SIZE;
  185. *obj_p = NULL;
  186. obj = udl_gem_alloc_object(dev, npages * PAGE_SIZE);
  187. if (!obj)
  188. return -ENOMEM;
  189. obj->sg = sg;
  190. obj->pages = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
  191. if (obj->pages == NULL) {
  192. DRM_ERROR("obj pages is NULL %d\n", npages);
  193. return -ENOMEM;
  194. }
  195. drm_prime_sg_to_page_addr_arrays(sg, obj->pages, NULL, npages);
  196. *obj_p = obj;
  197. return 0;
  198. }
  199. struct drm_gem_object *udl_gem_prime_import(struct drm_device *dev,
  200. struct dma_buf *dma_buf)
  201. {
  202. struct dma_buf_attachment *attach;
  203. struct sg_table *sg;
  204. struct udl_gem_object *uobj;
  205. int ret;
  206. /* need to attach */
  207. get_device(dev->dev);
  208. attach = dma_buf_attach(dma_buf, dev->dev);
  209. if (IS_ERR(attach)) {
  210. put_device(dev->dev);
  211. return ERR_CAST(attach);
  212. }
  213. get_dma_buf(dma_buf);
  214. sg = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
  215. if (IS_ERR(sg)) {
  216. ret = PTR_ERR(sg);
  217. goto fail_detach;
  218. }
  219. ret = udl_prime_create(dev, dma_buf->size, sg, &uobj);
  220. if (ret)
  221. goto fail_unmap;
  222. uobj->base.import_attach = attach;
  223. uobj->flags = UDL_BO_WC;
  224. return &uobj->base;
  225. fail_unmap:
  226. dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
  227. fail_detach:
  228. dma_buf_detach(dma_buf, attach);
  229. dma_buf_put(dma_buf);
  230. put_device(dev->dev);
  231. return ERR_PTR(ret);
  232. }