i915_gem_dmabuf.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * Copyright 2012 Red Hat Inc
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Dave Airlie <airlied@redhat.com>
  25. */
  26. #include <drm/drmP.h>
  27. #include "i915_drv.h"
  28. #include <linux/dma-buf.h>
  29. static struct drm_i915_gem_object *dma_buf_to_obj(struct dma_buf *buf)
  30. {
  31. return to_intel_bo(buf->priv);
  32. }
  33. static struct sg_table *i915_gem_map_dma_buf(struct dma_buf_attachment *attachment,
  34. enum dma_data_direction dir)
  35. {
  36. struct drm_i915_gem_object *obj = dma_buf_to_obj(attachment->dmabuf);
  37. struct sg_table *st;
  38. struct scatterlist *src, *dst;
  39. int ret, i;
  40. ret = i915_mutex_lock_interruptible(obj->base.dev);
  41. if (ret)
  42. goto err;
  43. ret = i915_gem_object_get_pages(obj);
  44. if (ret)
  45. goto err_unlock;
  46. i915_gem_object_pin_pages(obj);
  47. /* Copy sg so that we make an independent mapping */
  48. st = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
  49. if (st == NULL) {
  50. ret = -ENOMEM;
  51. goto err_unpin;
  52. }
  53. ret = sg_alloc_table(st, obj->pages->nents, GFP_KERNEL);
  54. if (ret)
  55. goto err_free;
  56. src = obj->pages->sgl;
  57. dst = st->sgl;
  58. for (i = 0; i < obj->pages->nents; i++) {
  59. sg_set_page(dst, sg_page(src), src->length, 0);
  60. dst = sg_next(dst);
  61. src = sg_next(src);
  62. }
  63. if (!dma_map_sg(attachment->dev, st->sgl, st->nents, dir)) {
  64. ret =-ENOMEM;
  65. goto err_free_sg;
  66. }
  67. mutex_unlock(&obj->base.dev->struct_mutex);
  68. return st;
  69. err_free_sg:
  70. sg_free_table(st);
  71. err_free:
  72. kfree(st);
  73. err_unpin:
  74. i915_gem_object_unpin_pages(obj);
  75. err_unlock:
  76. mutex_unlock(&obj->base.dev->struct_mutex);
  77. err:
  78. return ERR_PTR(ret);
  79. }
  80. static void i915_gem_unmap_dma_buf(struct dma_buf_attachment *attachment,
  81. struct sg_table *sg,
  82. enum dma_data_direction dir)
  83. {
  84. struct drm_i915_gem_object *obj = dma_buf_to_obj(attachment->dmabuf);
  85. mutex_lock(&obj->base.dev->struct_mutex);
  86. dma_unmap_sg(attachment->dev, sg->sgl, sg->nents, dir);
  87. sg_free_table(sg);
  88. kfree(sg);
  89. i915_gem_object_unpin_pages(obj);
  90. mutex_unlock(&obj->base.dev->struct_mutex);
  91. }
  92. static void *i915_gem_dmabuf_vmap(struct dma_buf *dma_buf)
  93. {
  94. struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf);
  95. struct drm_device *dev = obj->base.dev;
  96. struct sg_page_iter sg_iter;
  97. struct page **pages;
  98. int ret, i;
  99. ret = i915_mutex_lock_interruptible(dev);
  100. if (ret)
  101. return ERR_PTR(ret);
  102. if (obj->dma_buf_vmapping) {
  103. obj->vmapping_count++;
  104. goto out_unlock;
  105. }
  106. ret = i915_gem_object_get_pages(obj);
  107. if (ret)
  108. goto err;
  109. i915_gem_object_pin_pages(obj);
  110. ret = -ENOMEM;
  111. pages = drm_malloc_ab(obj->base.size >> PAGE_SHIFT, sizeof(*pages));
  112. if (pages == NULL)
  113. goto err_unpin;
  114. i = 0;
  115. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0)
  116. pages[i++] = sg_page_iter_page(&sg_iter);
  117. obj->dma_buf_vmapping = vmap(pages, i, 0, PAGE_KERNEL);
  118. drm_free_large(pages);
  119. if (!obj->dma_buf_vmapping)
  120. goto err_unpin;
  121. obj->vmapping_count = 1;
  122. out_unlock:
  123. mutex_unlock(&dev->struct_mutex);
  124. return obj->dma_buf_vmapping;
  125. err_unpin:
  126. i915_gem_object_unpin_pages(obj);
  127. err:
  128. mutex_unlock(&dev->struct_mutex);
  129. return ERR_PTR(ret);
  130. }
  131. static void i915_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr)
  132. {
  133. struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf);
  134. struct drm_device *dev = obj->base.dev;
  135. mutex_lock(&dev->struct_mutex);
  136. if (--obj->vmapping_count == 0) {
  137. vunmap(obj->dma_buf_vmapping);
  138. obj->dma_buf_vmapping = NULL;
  139. i915_gem_object_unpin_pages(obj);
  140. }
  141. mutex_unlock(&dev->struct_mutex);
  142. }
  143. static void *i915_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf, unsigned long page_num)
  144. {
  145. return NULL;
  146. }
  147. static void i915_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf, unsigned long page_num, void *addr)
  148. {
  149. }
  150. static void *i915_gem_dmabuf_kmap(struct dma_buf *dma_buf, unsigned long page_num)
  151. {
  152. return NULL;
  153. }
  154. static void i915_gem_dmabuf_kunmap(struct dma_buf *dma_buf, unsigned long page_num, void *addr)
  155. {
  156. }
  157. static int i915_gem_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struct *vma)
  158. {
  159. return -EINVAL;
  160. }
  161. static int i915_gem_begin_cpu_access(struct dma_buf *dma_buf, size_t start, size_t length, enum dma_data_direction direction)
  162. {
  163. struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf);
  164. struct drm_device *dev = obj->base.dev;
  165. int ret;
  166. bool write = (direction == DMA_BIDIRECTIONAL || direction == DMA_TO_DEVICE);
  167. ret = i915_mutex_lock_interruptible(dev);
  168. if (ret)
  169. return ret;
  170. ret = i915_gem_object_set_to_cpu_domain(obj, write);
  171. mutex_unlock(&dev->struct_mutex);
  172. return ret;
  173. }
  174. static const struct dma_buf_ops i915_dmabuf_ops = {
  175. .map_dma_buf = i915_gem_map_dma_buf,
  176. .unmap_dma_buf = i915_gem_unmap_dma_buf,
  177. .release = drm_gem_dmabuf_release,
  178. .kmap = i915_gem_dmabuf_kmap,
  179. .kmap_atomic = i915_gem_dmabuf_kmap_atomic,
  180. .kunmap = i915_gem_dmabuf_kunmap,
  181. .kunmap_atomic = i915_gem_dmabuf_kunmap_atomic,
  182. .mmap = i915_gem_dmabuf_mmap,
  183. .vmap = i915_gem_dmabuf_vmap,
  184. .vunmap = i915_gem_dmabuf_vunmap,
  185. .begin_cpu_access = i915_gem_begin_cpu_access,
  186. };
  187. struct dma_buf *i915_gem_prime_export(struct drm_device *dev,
  188. struct drm_gem_object *gem_obj, int flags)
  189. {
  190. struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
  191. DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
  192. exp_info.ops = &i915_dmabuf_ops;
  193. exp_info.size = gem_obj->size;
  194. exp_info.flags = flags;
  195. exp_info.priv = gem_obj;
  196. if (obj->ops->dmabuf_export) {
  197. int ret = obj->ops->dmabuf_export(obj);
  198. if (ret)
  199. return ERR_PTR(ret);
  200. }
  201. return dma_buf_export(&exp_info);
  202. }
  203. static int i915_gem_object_get_pages_dmabuf(struct drm_i915_gem_object *obj)
  204. {
  205. struct sg_table *sg;
  206. sg = dma_buf_map_attachment(obj->base.import_attach, DMA_BIDIRECTIONAL);
  207. if (IS_ERR(sg))
  208. return PTR_ERR(sg);
  209. obj->pages = sg;
  210. return 0;
  211. }
  212. static void i915_gem_object_put_pages_dmabuf(struct drm_i915_gem_object *obj)
  213. {
  214. dma_buf_unmap_attachment(obj->base.import_attach,
  215. obj->pages, DMA_BIDIRECTIONAL);
  216. }
  217. static const struct drm_i915_gem_object_ops i915_gem_object_dmabuf_ops = {
  218. .get_pages = i915_gem_object_get_pages_dmabuf,
  219. .put_pages = i915_gem_object_put_pages_dmabuf,
  220. };
  221. struct drm_gem_object *i915_gem_prime_import(struct drm_device *dev,
  222. struct dma_buf *dma_buf)
  223. {
  224. struct dma_buf_attachment *attach;
  225. struct drm_i915_gem_object *obj;
  226. int ret;
  227. /* is this one of own objects? */
  228. if (dma_buf->ops == &i915_dmabuf_ops) {
  229. obj = dma_buf_to_obj(dma_buf);
  230. /* is it from our device? */
  231. if (obj->base.dev == dev) {
  232. /*
  233. * Importing dmabuf exported from out own gem increases
  234. * refcount on gem itself instead of f_count of dmabuf.
  235. */
  236. drm_gem_object_reference(&obj->base);
  237. return &obj->base;
  238. }
  239. }
  240. /* need to attach */
  241. attach = dma_buf_attach(dma_buf, dev->dev);
  242. if (IS_ERR(attach))
  243. return ERR_CAST(attach);
  244. get_dma_buf(dma_buf);
  245. obj = i915_gem_object_alloc(dev);
  246. if (obj == NULL) {
  247. ret = -ENOMEM;
  248. goto fail_detach;
  249. }
  250. drm_gem_private_object_init(dev, &obj->base, dma_buf->size);
  251. i915_gem_object_init(obj, &i915_gem_object_dmabuf_ops);
  252. obj->base.import_attach = attach;
  253. return &obj->base;
  254. fail_detach:
  255. dma_buf_detach(dma_buf, attach);
  256. dma_buf_put(dma_buf);
  257. return ERR_PTR(ret);
  258. }