i915_gem_dmabuf.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 "drmP.h"
  27. #include "i915_drv.h"
  28. #include <linux/dma-buf.h>
  29. static struct sg_table *i915_gem_map_dma_buf(struct dma_buf_attachment *attachment,
  30. enum dma_data_direction dir)
  31. {
  32. struct drm_i915_gem_object *obj = attachment->dmabuf->priv;
  33. struct drm_device *dev = obj->base.dev;
  34. int npages = obj->base.size / PAGE_SIZE;
  35. struct sg_table *sg;
  36. int ret;
  37. int nents;
  38. ret = i915_mutex_lock_interruptible(dev);
  39. if (ret)
  40. return ERR_PTR(ret);
  41. ret = i915_gem_object_get_pages(obj);
  42. if (ret) {
  43. sg = ERR_PTR(ret);
  44. goto out;
  45. }
  46. /* link the pages into an SG then map the sg */
  47. sg = drm_prime_pages_to_sg(obj->pages, npages);
  48. nents = dma_map_sg(attachment->dev, sg->sgl, sg->nents, dir);
  49. i915_gem_object_pin_pages(obj);
  50. out:
  51. mutex_unlock(&dev->struct_mutex);
  52. return sg;
  53. }
  54. static void i915_gem_unmap_dma_buf(struct dma_buf_attachment *attachment,
  55. struct sg_table *sg, enum dma_data_direction dir)
  56. {
  57. dma_unmap_sg(attachment->dev, sg->sgl, sg->nents, dir);
  58. sg_free_table(sg);
  59. kfree(sg);
  60. }
  61. static void i915_gem_dmabuf_release(struct dma_buf *dma_buf)
  62. {
  63. struct drm_i915_gem_object *obj = dma_buf->priv;
  64. if (obj->base.export_dma_buf == dma_buf) {
  65. /* drop the reference on the export fd holds */
  66. obj->base.export_dma_buf = NULL;
  67. drm_gem_object_unreference_unlocked(&obj->base);
  68. }
  69. }
  70. static void *i915_gem_dmabuf_vmap(struct dma_buf *dma_buf)
  71. {
  72. struct drm_i915_gem_object *obj = dma_buf->priv;
  73. struct drm_device *dev = obj->base.dev;
  74. int ret;
  75. ret = i915_mutex_lock_interruptible(dev);
  76. if (ret)
  77. return ERR_PTR(ret);
  78. if (obj->dma_buf_vmapping) {
  79. obj->vmapping_count++;
  80. goto out_unlock;
  81. }
  82. ret = i915_gem_object_get_pages(obj);
  83. if (ret) {
  84. mutex_unlock(&dev->struct_mutex);
  85. return ERR_PTR(ret);
  86. }
  87. obj->dma_buf_vmapping = vmap(obj->pages, obj->base.size / PAGE_SIZE, 0, PAGE_KERNEL);
  88. if (!obj->dma_buf_vmapping) {
  89. DRM_ERROR("failed to vmap object\n");
  90. goto out_unlock;
  91. }
  92. obj->vmapping_count = 1;
  93. i915_gem_object_pin_pages(obj);
  94. out_unlock:
  95. mutex_unlock(&dev->struct_mutex);
  96. return obj->dma_buf_vmapping;
  97. }
  98. static void i915_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr)
  99. {
  100. struct drm_i915_gem_object *obj = dma_buf->priv;
  101. struct drm_device *dev = obj->base.dev;
  102. int ret;
  103. ret = i915_mutex_lock_interruptible(dev);
  104. if (ret)
  105. return;
  106. if (--obj->vmapping_count == 0) {
  107. vunmap(obj->dma_buf_vmapping);
  108. obj->dma_buf_vmapping = NULL;
  109. i915_gem_object_unpin_pages(obj);
  110. }
  111. mutex_unlock(&dev->struct_mutex);
  112. }
  113. static void *i915_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf, unsigned long page_num)
  114. {
  115. return NULL;
  116. }
  117. static void i915_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf, unsigned long page_num, void *addr)
  118. {
  119. }
  120. static void *i915_gem_dmabuf_kmap(struct dma_buf *dma_buf, unsigned long page_num)
  121. {
  122. return NULL;
  123. }
  124. static void i915_gem_dmabuf_kunmap(struct dma_buf *dma_buf, unsigned long page_num, void *addr)
  125. {
  126. }
  127. static int i915_gem_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struct *vma)
  128. {
  129. return -EINVAL;
  130. }
  131. static int i915_gem_begin_cpu_access(struct dma_buf *dma_buf, size_t start, size_t length, enum dma_data_direction direction)
  132. {
  133. struct drm_i915_gem_object *obj = dma_buf->priv;
  134. struct drm_device *dev = obj->base.dev;
  135. int ret;
  136. bool write = (direction == DMA_BIDIRECTIONAL || direction == DMA_TO_DEVICE);
  137. ret = i915_mutex_lock_interruptible(dev);
  138. if (ret)
  139. return ret;
  140. ret = i915_gem_object_set_to_cpu_domain(obj, write);
  141. mutex_unlock(&dev->struct_mutex);
  142. return ret;
  143. }
  144. static const struct dma_buf_ops i915_dmabuf_ops = {
  145. .map_dma_buf = i915_gem_map_dma_buf,
  146. .unmap_dma_buf = i915_gem_unmap_dma_buf,
  147. .release = i915_gem_dmabuf_release,
  148. .kmap = i915_gem_dmabuf_kmap,
  149. .kmap_atomic = i915_gem_dmabuf_kmap_atomic,
  150. .kunmap = i915_gem_dmabuf_kunmap,
  151. .kunmap_atomic = i915_gem_dmabuf_kunmap_atomic,
  152. .mmap = i915_gem_dmabuf_mmap,
  153. .vmap = i915_gem_dmabuf_vmap,
  154. .vunmap = i915_gem_dmabuf_vunmap,
  155. .begin_cpu_access = i915_gem_begin_cpu_access,
  156. };
  157. struct dma_buf *i915_gem_prime_export(struct drm_device *dev,
  158. struct drm_gem_object *gem_obj, int flags)
  159. {
  160. struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
  161. return dma_buf_export(obj, &i915_dmabuf_ops,
  162. obj->base.size, 0600);
  163. }
  164. struct drm_gem_object *i915_gem_prime_import(struct drm_device *dev,
  165. struct dma_buf *dma_buf)
  166. {
  167. struct dma_buf_attachment *attach;
  168. struct sg_table *sg;
  169. struct drm_i915_gem_object *obj;
  170. int npages;
  171. int size;
  172. int ret;
  173. /* is this one of own objects? */
  174. if (dma_buf->ops == &i915_dmabuf_ops) {
  175. obj = dma_buf->priv;
  176. /* is it from our device? */
  177. if (obj->base.dev == dev) {
  178. drm_gem_object_reference(&obj->base);
  179. return &obj->base;
  180. }
  181. }
  182. /* need to attach */
  183. attach = dma_buf_attach(dma_buf, dev->dev);
  184. if (IS_ERR(attach))
  185. return ERR_CAST(attach);
  186. sg = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
  187. if (IS_ERR(sg)) {
  188. ret = PTR_ERR(sg);
  189. goto fail_detach;
  190. }
  191. size = dma_buf->size;
  192. npages = size / PAGE_SIZE;
  193. obj = kzalloc(sizeof(*obj), GFP_KERNEL);
  194. if (obj == NULL) {
  195. ret = -ENOMEM;
  196. goto fail_unmap;
  197. }
  198. ret = drm_gem_private_object_init(dev, &obj->base, size);
  199. if (ret) {
  200. kfree(obj);
  201. goto fail_unmap;
  202. }
  203. obj->sg_table = sg;
  204. obj->base.import_attach = attach;
  205. return &obj->base;
  206. fail_unmap:
  207. dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
  208. fail_detach:
  209. dma_buf_detach(dma_buf, attach);
  210. return ERR_PTR(ret);
  211. }