i915_gem_dmabuf.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 = NULL;
  36. int ret;
  37. int nents;
  38. ret = i915_mutex_lock_interruptible(dev);
  39. if (ret)
  40. return ERR_PTR(ret);
  41. if (!obj->pages) {
  42. ret = i915_gem_object_get_pages_gtt(obj, __GFP_NORETRY | __GFP_NOWARN);
  43. if (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. if (!obj->pages) {
  82. ret = i915_gem_object_get_pages_gtt(obj, __GFP_NORETRY | __GFP_NOWARN);
  83. if (ret) {
  84. mutex_unlock(&dev->struct_mutex);
  85. return ERR_PTR(ret);
  86. }
  87. }
  88. obj->dma_buf_vmapping = vmap(obj->pages, obj->base.size / PAGE_SIZE, 0, PAGE_KERNEL);
  89. if (!obj->dma_buf_vmapping) {
  90. DRM_ERROR("failed to vmap object\n");
  91. goto out_unlock;
  92. }
  93. obj->vmapping_count = 1;
  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. --obj->vmapping_count;
  107. if (obj->vmapping_count == 0) {
  108. vunmap(obj->dma_buf_vmapping);
  109. obj->dma_buf_vmapping = NULL;
  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 const struct dma_buf_ops i915_dmabuf_ops = {
  132. .map_dma_buf = i915_gem_map_dma_buf,
  133. .unmap_dma_buf = i915_gem_unmap_dma_buf,
  134. .release = i915_gem_dmabuf_release,
  135. .kmap = i915_gem_dmabuf_kmap,
  136. .kmap_atomic = i915_gem_dmabuf_kmap_atomic,
  137. .kunmap = i915_gem_dmabuf_kunmap,
  138. .kunmap_atomic = i915_gem_dmabuf_kunmap_atomic,
  139. .mmap = i915_gem_dmabuf_mmap,
  140. .vmap = i915_gem_dmabuf_vmap,
  141. .vunmap = i915_gem_dmabuf_vunmap,
  142. };
  143. struct dma_buf *i915_gem_prime_export(struct drm_device *dev,
  144. struct drm_gem_object *gem_obj, int flags)
  145. {
  146. struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
  147. return dma_buf_export(obj, &i915_dmabuf_ops,
  148. obj->base.size, 0600);
  149. }
  150. struct drm_gem_object *i915_gem_prime_import(struct drm_device *dev,
  151. struct dma_buf *dma_buf)
  152. {
  153. struct dma_buf_attachment *attach;
  154. struct sg_table *sg;
  155. struct drm_i915_gem_object *obj;
  156. int npages;
  157. int size;
  158. int ret;
  159. /* is this one of own objects? */
  160. if (dma_buf->ops == &i915_dmabuf_ops) {
  161. obj = dma_buf->priv;
  162. /* is it from our device? */
  163. if (obj->base.dev == dev) {
  164. drm_gem_object_reference(&obj->base);
  165. return &obj->base;
  166. }
  167. }
  168. /* need to attach */
  169. attach = dma_buf_attach(dma_buf, dev->dev);
  170. if (IS_ERR(attach))
  171. return ERR_CAST(attach);
  172. sg = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
  173. if (IS_ERR(sg)) {
  174. ret = PTR_ERR(sg);
  175. goto fail_detach;
  176. }
  177. size = dma_buf->size;
  178. npages = size / PAGE_SIZE;
  179. obj = kzalloc(sizeof(*obj), GFP_KERNEL);
  180. if (obj == NULL) {
  181. ret = -ENOMEM;
  182. goto fail_unmap;
  183. }
  184. ret = drm_gem_private_object_init(dev, &obj->base, size);
  185. if (ret) {
  186. kfree(obj);
  187. goto fail_unmap;
  188. }
  189. obj->sg_table = sg;
  190. obj->base.import_attach = attach;
  191. return &obj->base;
  192. fail_unmap:
  193. dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
  194. fail_detach:
  195. dma_buf_detach(dma_buf, attach);
  196. return ERR_PTR(ret);
  197. }