i915_gem_dmabuf.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 <linux/dma-buf.h>
  27. #include <linux/reservation.h>
  28. #include <drm/drmP.h>
  29. #include "i915_drv.h"
  30. static struct drm_i915_gem_object *dma_buf_to_obj(struct dma_buf *buf)
  31. {
  32. return to_intel_bo(buf->priv);
  33. }
  34. static struct sg_table *i915_gem_map_dma_buf(struct dma_buf_attachment *attachment,
  35. enum dma_data_direction dir)
  36. {
  37. struct drm_i915_gem_object *obj = dma_buf_to_obj(attachment->dmabuf);
  38. struct sg_table *st;
  39. struct scatterlist *src, *dst;
  40. int ret, i;
  41. ret = i915_mutex_lock_interruptible(obj->base.dev);
  42. if (ret)
  43. goto err;
  44. ret = i915_gem_object_get_pages(obj);
  45. if (ret)
  46. goto err_unlock;
  47. i915_gem_object_pin_pages(obj);
  48. /* Copy sg so that we make an independent mapping */
  49. st = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
  50. if (st == NULL) {
  51. ret = -ENOMEM;
  52. goto err_unpin;
  53. }
  54. ret = sg_alloc_table(st, obj->pages->nents, GFP_KERNEL);
  55. if (ret)
  56. goto err_free;
  57. src = obj->pages->sgl;
  58. dst = st->sgl;
  59. for (i = 0; i < obj->pages->nents; i++) {
  60. sg_set_page(dst, sg_page(src), src->length, 0);
  61. dst = sg_next(dst);
  62. src = sg_next(src);
  63. }
  64. if (!dma_map_sg(attachment->dev, st->sgl, st->nents, dir)) {
  65. ret =-ENOMEM;
  66. goto err_free_sg;
  67. }
  68. mutex_unlock(&obj->base.dev->struct_mutex);
  69. return st;
  70. err_free_sg:
  71. sg_free_table(st);
  72. err_free:
  73. kfree(st);
  74. err_unpin:
  75. i915_gem_object_unpin_pages(obj);
  76. err_unlock:
  77. mutex_unlock(&obj->base.dev->struct_mutex);
  78. err:
  79. return ERR_PTR(ret);
  80. }
  81. static void i915_gem_unmap_dma_buf(struct dma_buf_attachment *attachment,
  82. struct sg_table *sg,
  83. enum dma_data_direction dir)
  84. {
  85. struct drm_i915_gem_object *obj = dma_buf_to_obj(attachment->dmabuf);
  86. dma_unmap_sg(attachment->dev, sg->sgl, sg->nents, dir);
  87. sg_free_table(sg);
  88. kfree(sg);
  89. mutex_lock(&obj->base.dev->struct_mutex);
  90. i915_gem_object_unpin_pages(obj);
  91. mutex_unlock(&obj->base.dev->struct_mutex);
  92. }
  93. static void *i915_gem_dmabuf_vmap(struct dma_buf *dma_buf)
  94. {
  95. struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf);
  96. struct drm_device *dev = obj->base.dev;
  97. void *addr;
  98. int ret;
  99. ret = i915_mutex_lock_interruptible(dev);
  100. if (ret)
  101. return ERR_PTR(ret);
  102. addr = i915_gem_object_pin_map(obj, I915_MAP_WB);
  103. mutex_unlock(&dev->struct_mutex);
  104. return addr;
  105. }
  106. static void i915_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr)
  107. {
  108. struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf);
  109. struct drm_device *dev = obj->base.dev;
  110. mutex_lock(&dev->struct_mutex);
  111. i915_gem_object_unpin_map(obj);
  112. mutex_unlock(&dev->struct_mutex);
  113. }
  114. static void *i915_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf, unsigned long page_num)
  115. {
  116. return NULL;
  117. }
  118. static void i915_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf, unsigned long page_num, void *addr)
  119. {
  120. }
  121. static void *i915_gem_dmabuf_kmap(struct dma_buf *dma_buf, unsigned long page_num)
  122. {
  123. return NULL;
  124. }
  125. static void i915_gem_dmabuf_kunmap(struct dma_buf *dma_buf, unsigned long page_num, void *addr)
  126. {
  127. }
  128. static int i915_gem_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struct *vma)
  129. {
  130. struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf);
  131. int ret;
  132. if (obj->base.size < vma->vm_end - vma->vm_start)
  133. return -EINVAL;
  134. if (!obj->base.filp)
  135. return -ENODEV;
  136. ret = obj->base.filp->f_op->mmap(obj->base.filp, vma);
  137. if (ret)
  138. return ret;
  139. fput(vma->vm_file);
  140. vma->vm_file = get_file(obj->base.filp);
  141. return 0;
  142. }
  143. static int i915_gem_begin_cpu_access(struct dma_buf *dma_buf, enum dma_data_direction direction)
  144. {
  145. struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf);
  146. struct drm_device *dev = obj->base.dev;
  147. int ret;
  148. bool write = (direction == DMA_BIDIRECTIONAL || direction == DMA_TO_DEVICE);
  149. ret = i915_mutex_lock_interruptible(dev);
  150. if (ret)
  151. return ret;
  152. ret = i915_gem_object_set_to_cpu_domain(obj, write);
  153. mutex_unlock(&dev->struct_mutex);
  154. return ret;
  155. }
  156. static int i915_gem_end_cpu_access(struct dma_buf *dma_buf, enum dma_data_direction direction)
  157. {
  158. struct drm_i915_gem_object *obj = dma_buf_to_obj(dma_buf);
  159. struct drm_device *dev = obj->base.dev;
  160. int ret;
  161. ret = i915_mutex_lock_interruptible(dev);
  162. if (ret)
  163. return ret;
  164. ret = i915_gem_object_set_to_gtt_domain(obj, false);
  165. mutex_unlock(&dev->struct_mutex);
  166. return ret;
  167. }
  168. static const struct dma_buf_ops i915_dmabuf_ops = {
  169. .map_dma_buf = i915_gem_map_dma_buf,
  170. .unmap_dma_buf = i915_gem_unmap_dma_buf,
  171. .release = drm_gem_dmabuf_release,
  172. .kmap = i915_gem_dmabuf_kmap,
  173. .kmap_atomic = i915_gem_dmabuf_kmap_atomic,
  174. .kunmap = i915_gem_dmabuf_kunmap,
  175. .kunmap_atomic = i915_gem_dmabuf_kunmap_atomic,
  176. .mmap = i915_gem_dmabuf_mmap,
  177. .vmap = i915_gem_dmabuf_vmap,
  178. .vunmap = i915_gem_dmabuf_vunmap,
  179. .begin_cpu_access = i915_gem_begin_cpu_access,
  180. .end_cpu_access = i915_gem_end_cpu_access,
  181. };
  182. static void export_fences(struct drm_i915_gem_object *obj,
  183. struct dma_buf *dma_buf)
  184. {
  185. struct reservation_object *resv = dma_buf->resv;
  186. struct drm_i915_gem_request *req;
  187. unsigned long active;
  188. int idx;
  189. active = __I915_BO_ACTIVE(obj);
  190. if (!active)
  191. return;
  192. /* Serialise with execbuf to prevent concurrent fence-loops */
  193. mutex_lock(&obj->base.dev->struct_mutex);
  194. /* Mark the object for future fences before racily adding old fences */
  195. obj->base.dma_buf = dma_buf;
  196. ww_mutex_lock(&resv->lock, NULL);
  197. for_each_active(active, idx) {
  198. req = i915_gem_active_get(&obj->last_read[idx],
  199. &obj->base.dev->struct_mutex);
  200. if (!req)
  201. continue;
  202. if (reservation_object_reserve_shared(resv) == 0)
  203. reservation_object_add_shared_fence(resv, &req->fence);
  204. i915_gem_request_put(req);
  205. }
  206. req = i915_gem_active_get(&obj->last_write,
  207. &obj->base.dev->struct_mutex);
  208. if (req) {
  209. reservation_object_add_excl_fence(resv, &req->fence);
  210. i915_gem_request_put(req);
  211. }
  212. ww_mutex_unlock(&resv->lock);
  213. mutex_unlock(&obj->base.dev->struct_mutex);
  214. }
  215. struct dma_buf *i915_gem_prime_export(struct drm_device *dev,
  216. struct drm_gem_object *gem_obj, int flags)
  217. {
  218. struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
  219. DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
  220. struct dma_buf *dma_buf;
  221. exp_info.ops = &i915_dmabuf_ops;
  222. exp_info.size = gem_obj->size;
  223. exp_info.flags = flags;
  224. exp_info.priv = gem_obj;
  225. if (obj->ops->dmabuf_export) {
  226. int ret = obj->ops->dmabuf_export(obj);
  227. if (ret)
  228. return ERR_PTR(ret);
  229. }
  230. dma_buf = drm_gem_dmabuf_export(dev, &exp_info);
  231. if (IS_ERR(dma_buf))
  232. return dma_buf;
  233. export_fences(obj, dma_buf);
  234. return dma_buf;
  235. }
  236. static int i915_gem_object_get_pages_dmabuf(struct drm_i915_gem_object *obj)
  237. {
  238. struct sg_table *sg;
  239. sg = dma_buf_map_attachment(obj->base.import_attach, DMA_BIDIRECTIONAL);
  240. if (IS_ERR(sg))
  241. return PTR_ERR(sg);
  242. obj->pages = sg;
  243. return 0;
  244. }
  245. static void i915_gem_object_put_pages_dmabuf(struct drm_i915_gem_object *obj)
  246. {
  247. dma_buf_unmap_attachment(obj->base.import_attach,
  248. obj->pages, DMA_BIDIRECTIONAL);
  249. }
  250. static const struct drm_i915_gem_object_ops i915_gem_object_dmabuf_ops = {
  251. .get_pages = i915_gem_object_get_pages_dmabuf,
  252. .put_pages = i915_gem_object_put_pages_dmabuf,
  253. };
  254. struct drm_gem_object *i915_gem_prime_import(struct drm_device *dev,
  255. struct dma_buf *dma_buf)
  256. {
  257. struct dma_buf_attachment *attach;
  258. struct drm_i915_gem_object *obj;
  259. int ret;
  260. /* is this one of own objects? */
  261. if (dma_buf->ops == &i915_dmabuf_ops) {
  262. obj = dma_buf_to_obj(dma_buf);
  263. /* is it from our device? */
  264. if (obj->base.dev == dev) {
  265. /*
  266. * Importing dmabuf exported from out own gem increases
  267. * refcount on gem itself instead of f_count of dmabuf.
  268. */
  269. return &i915_gem_object_get(obj)->base;
  270. }
  271. }
  272. /* need to attach */
  273. attach = dma_buf_attach(dma_buf, dev->dev);
  274. if (IS_ERR(attach))
  275. return ERR_CAST(attach);
  276. get_dma_buf(dma_buf);
  277. obj = i915_gem_object_alloc(dev);
  278. if (obj == NULL) {
  279. ret = -ENOMEM;
  280. goto fail_detach;
  281. }
  282. drm_gem_private_object_init(dev, &obj->base, dma_buf->size);
  283. i915_gem_object_init(obj, &i915_gem_object_dmabuf_ops);
  284. obj->base.import_attach = attach;
  285. /* We use GTT as shorthand for a coherent domain, one that is
  286. * neither in the GPU cache nor in the CPU cache, where all
  287. * writes are immediately visible in memory. (That's not strictly
  288. * true, but it's close! There are internal buffers such as the
  289. * write-combined buffer or a delay through the chipset for GTT
  290. * writes that do require us to treat GTT as a separate cache domain.)
  291. */
  292. obj->base.read_domains = I915_GEM_DOMAIN_GTT;
  293. obj->base.write_domain = 0;
  294. return &obj->base;
  295. fail_detach:
  296. dma_buf_detach(dma_buf, attach);
  297. dma_buf_put(dma_buf);
  298. return ERR_PTR(ret);
  299. }