udl_dmabuf.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 dma_buf_attachment *attach)
  30. {
  31. struct udl_drm_dmabuf_attachment *udl_attach;
  32. DRM_DEBUG_PRIME("[DEV:%s] size:%zd\n", dev_name(attach->dev),
  33. attach->dmabuf->size);
  34. udl_attach = kzalloc(sizeof(*udl_attach), GFP_KERNEL);
  35. if (!udl_attach)
  36. return -ENOMEM;
  37. udl_attach->dir = DMA_NONE;
  38. attach->priv = udl_attach;
  39. return 0;
  40. }
  41. static void udl_detach_dma_buf(struct dma_buf *dmabuf,
  42. struct dma_buf_attachment *attach)
  43. {
  44. struct udl_drm_dmabuf_attachment *udl_attach = attach->priv;
  45. struct sg_table *sgt;
  46. if (!udl_attach)
  47. return;
  48. DRM_DEBUG_PRIME("[DEV:%s] size:%zd\n", dev_name(attach->dev),
  49. attach->dmabuf->size);
  50. sgt = &udl_attach->sgt;
  51. if (udl_attach->dir != DMA_NONE)
  52. dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents,
  53. udl_attach->dir);
  54. sg_free_table(sgt);
  55. kfree(udl_attach);
  56. attach->priv = NULL;
  57. }
  58. static struct sg_table *udl_map_dma_buf(struct dma_buf_attachment *attach,
  59. enum dma_data_direction dir)
  60. {
  61. struct udl_drm_dmabuf_attachment *udl_attach = attach->priv;
  62. struct udl_gem_object *obj = to_udl_bo(attach->dmabuf->priv);
  63. struct drm_device *dev = obj->base.dev;
  64. struct udl_device *udl = dev->dev_private;
  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(&udl->gem_lock);
  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(&udl->gem_lock);
  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_kunmap(struct dma_buf *dma_buf,
  132. unsigned long page_num, void *addr)
  133. {
  134. /* TODO */
  135. }
  136. static int udl_dmabuf_mmap(struct dma_buf *dma_buf,
  137. struct vm_area_struct *vma)
  138. {
  139. /* TODO */
  140. return -EINVAL;
  141. }
  142. static const struct dma_buf_ops udl_dmabuf_ops = {
  143. .attach = udl_attach_dma_buf,
  144. .detach = udl_detach_dma_buf,
  145. .map_dma_buf = udl_map_dma_buf,
  146. .unmap_dma_buf = udl_unmap_dma_buf,
  147. .map = udl_dmabuf_kmap,
  148. .unmap = udl_dmabuf_kunmap,
  149. .mmap = udl_dmabuf_mmap,
  150. .release = drm_gem_dmabuf_release,
  151. };
  152. struct dma_buf *udl_gem_prime_export(struct drm_device *dev,
  153. struct drm_gem_object *obj, int flags)
  154. {
  155. DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
  156. exp_info.ops = &udl_dmabuf_ops;
  157. exp_info.size = obj->size;
  158. exp_info.flags = flags;
  159. exp_info.priv = obj;
  160. return drm_gem_dmabuf_export(dev, &exp_info);
  161. }
  162. static int udl_prime_create(struct drm_device *dev,
  163. size_t size,
  164. struct sg_table *sg,
  165. struct udl_gem_object **obj_p)
  166. {
  167. struct udl_gem_object *obj;
  168. int npages;
  169. npages = size / PAGE_SIZE;
  170. *obj_p = NULL;
  171. obj = udl_gem_alloc_object(dev, npages * PAGE_SIZE);
  172. if (!obj)
  173. return -ENOMEM;
  174. obj->sg = sg;
  175. obj->pages = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
  176. if (obj->pages == NULL) {
  177. DRM_ERROR("obj pages is NULL %d\n", npages);
  178. return -ENOMEM;
  179. }
  180. drm_prime_sg_to_page_addr_arrays(sg, obj->pages, NULL, npages);
  181. *obj_p = obj;
  182. return 0;
  183. }
  184. struct drm_gem_object *udl_gem_prime_import(struct drm_device *dev,
  185. struct dma_buf *dma_buf)
  186. {
  187. struct dma_buf_attachment *attach;
  188. struct sg_table *sg;
  189. struct udl_gem_object *uobj;
  190. int ret;
  191. /* need to attach */
  192. get_device(dev->dev);
  193. attach = dma_buf_attach(dma_buf, dev->dev);
  194. if (IS_ERR(attach)) {
  195. put_device(dev->dev);
  196. return ERR_CAST(attach);
  197. }
  198. get_dma_buf(dma_buf);
  199. sg = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
  200. if (IS_ERR(sg)) {
  201. ret = PTR_ERR(sg);
  202. goto fail_detach;
  203. }
  204. ret = udl_prime_create(dev, dma_buf->size, sg, &uobj);
  205. if (ret)
  206. goto fail_unmap;
  207. uobj->base.import_attach = attach;
  208. uobj->flags = UDL_BO_WC;
  209. return &uobj->base;
  210. fail_unmap:
  211. dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
  212. fail_detach:
  213. dma_buf_detach(dma_buf, attach);
  214. dma_buf_put(dma_buf);
  215. put_device(dev->dev);
  216. return ERR_PTR(ret);
  217. }