i915_gem_dmabuf.c 6.4 KB

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