drm_prime.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * Copyright © 2012 Red Hat
  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 DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Dave Airlie <airlied@redhat.com>
  25. * Rob Clark <rob.clark@linaro.org>
  26. *
  27. */
  28. #include <linux/export.h>
  29. #include <linux/dma-buf.h>
  30. #include "drmP.h"
  31. /*
  32. * DMA-BUF/GEM Object references and lifetime overview:
  33. *
  34. * On the export the dma_buf holds a reference to the exporting GEM
  35. * object. It takes this reference in handle_to_fd_ioctl, when it
  36. * first calls .prime_export and stores the exporting GEM object in
  37. * the dma_buf priv. This reference is released when the dma_buf
  38. * object goes away in the driver .release function.
  39. *
  40. * On the import the importing GEM object holds a reference to the
  41. * dma_buf (which in turn holds a ref to the exporting GEM object).
  42. * It takes that reference in the fd_to_handle ioctl.
  43. * It calls dma_buf_get, creates an attachment to it and stores the
  44. * attachment in the GEM object. When this attachment is destroyed
  45. * when the imported object is destroyed, we remove the attachment
  46. * and drop the reference to the dma_buf.
  47. *
  48. * Thus the chain of references always flows in one direction
  49. * (avoiding loops): importing_gem -> dmabuf -> exporting_gem
  50. *
  51. * Self-importing: if userspace is using PRIME as a replacement for flink
  52. * then it will get a fd->handle request for a GEM object that it created.
  53. * Drivers should detect this situation and return back the gem object
  54. * from the dma-buf private.
  55. */
  56. struct drm_prime_member {
  57. struct list_head entry;
  58. struct dma_buf *dma_buf;
  59. uint32_t handle;
  60. };
  61. int drm_gem_prime_handle_to_fd(struct drm_device *dev,
  62. struct drm_file *file_priv, uint32_t handle, uint32_t flags,
  63. int *prime_fd)
  64. {
  65. struct drm_gem_object *obj;
  66. void *buf;
  67. obj = drm_gem_object_lookup(dev, file_priv, handle);
  68. if (!obj)
  69. return -ENOENT;
  70. mutex_lock(&file_priv->prime.lock);
  71. /* re-export the original imported object */
  72. if (obj->import_attach) {
  73. get_dma_buf(obj->import_attach->dmabuf);
  74. *prime_fd = dma_buf_fd(obj->import_attach->dmabuf, flags);
  75. drm_gem_object_unreference_unlocked(obj);
  76. mutex_unlock(&file_priv->prime.lock);
  77. return 0;
  78. }
  79. if (obj->export_dma_buf) {
  80. get_dma_buf(obj->export_dma_buf);
  81. *prime_fd = dma_buf_fd(obj->export_dma_buf, flags);
  82. drm_gem_object_unreference_unlocked(obj);
  83. } else {
  84. buf = dev->driver->gem_prime_export(dev, obj, flags);
  85. if (IS_ERR(buf)) {
  86. /* normally the created dma-buf takes ownership of the ref,
  87. * but if that fails then drop the ref
  88. */
  89. drm_gem_object_unreference_unlocked(obj);
  90. mutex_unlock(&file_priv->prime.lock);
  91. return PTR_ERR(buf);
  92. }
  93. obj->export_dma_buf = buf;
  94. *prime_fd = dma_buf_fd(buf, flags);
  95. }
  96. mutex_unlock(&file_priv->prime.lock);
  97. return 0;
  98. }
  99. EXPORT_SYMBOL(drm_gem_prime_handle_to_fd);
  100. int drm_gem_prime_fd_to_handle(struct drm_device *dev,
  101. struct drm_file *file_priv, int prime_fd, uint32_t *handle)
  102. {
  103. struct dma_buf *dma_buf;
  104. struct drm_gem_object *obj;
  105. int ret;
  106. dma_buf = dma_buf_get(prime_fd);
  107. if (IS_ERR(dma_buf))
  108. return PTR_ERR(dma_buf);
  109. mutex_lock(&file_priv->prime.lock);
  110. ret = drm_prime_lookup_imported_buf_handle(&file_priv->prime,
  111. dma_buf, handle);
  112. if (!ret) {
  113. ret = 0;
  114. goto out_put;
  115. }
  116. /* never seen this one, need to import */
  117. obj = dev->driver->gem_prime_import(dev, dma_buf);
  118. if (IS_ERR(obj)) {
  119. ret = PTR_ERR(obj);
  120. goto out_put;
  121. }
  122. ret = drm_gem_handle_create(file_priv, obj, handle);
  123. drm_gem_object_unreference_unlocked(obj);
  124. if (ret)
  125. goto out_put;
  126. ret = drm_prime_add_imported_buf_handle(&file_priv->prime,
  127. dma_buf, *handle);
  128. if (ret)
  129. goto fail;
  130. mutex_unlock(&file_priv->prime.lock);
  131. return 0;
  132. fail:
  133. /* hmm, if driver attached, we are relying on the free-object path
  134. * to detach.. which seems ok..
  135. */
  136. drm_gem_object_handle_unreference_unlocked(obj);
  137. out_put:
  138. dma_buf_put(dma_buf);
  139. mutex_unlock(&file_priv->prime.lock);
  140. return ret;
  141. }
  142. EXPORT_SYMBOL(drm_gem_prime_fd_to_handle);
  143. int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
  144. struct drm_file *file_priv)
  145. {
  146. struct drm_prime_handle *args = data;
  147. uint32_t flags;
  148. if (!drm_core_check_feature(dev, DRIVER_PRIME))
  149. return -EINVAL;
  150. if (!dev->driver->prime_handle_to_fd)
  151. return -ENOSYS;
  152. /* check flags are valid */
  153. if (args->flags & ~DRM_CLOEXEC)
  154. return -EINVAL;
  155. /* we only want to pass DRM_CLOEXEC which is == O_CLOEXEC */
  156. flags = args->flags & DRM_CLOEXEC;
  157. return dev->driver->prime_handle_to_fd(dev, file_priv,
  158. args->handle, flags, &args->fd);
  159. }
  160. int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
  161. struct drm_file *file_priv)
  162. {
  163. struct drm_prime_handle *args = data;
  164. if (!drm_core_check_feature(dev, DRIVER_PRIME))
  165. return -EINVAL;
  166. if (!dev->driver->prime_fd_to_handle)
  167. return -ENOSYS;
  168. return dev->driver->prime_fd_to_handle(dev, file_priv,
  169. args->fd, &args->handle);
  170. }
  171. /*
  172. * drm_prime_pages_to_sg
  173. *
  174. * this helper creates an sg table object from a set of pages
  175. * the driver is responsible for mapping the pages into the
  176. * importers address space
  177. */
  178. struct sg_table *drm_prime_pages_to_sg(struct page **pages, int nr_pages)
  179. {
  180. struct sg_table *sg = NULL;
  181. struct scatterlist *iter;
  182. int i;
  183. int ret;
  184. sg = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
  185. if (!sg)
  186. goto out;
  187. ret = sg_alloc_table(sg, nr_pages, GFP_KERNEL);
  188. if (ret)
  189. goto out;
  190. for_each_sg(sg->sgl, iter, nr_pages, i)
  191. sg_set_page(iter, pages[i], PAGE_SIZE, 0);
  192. return sg;
  193. out:
  194. kfree(sg);
  195. return NULL;
  196. }
  197. EXPORT_SYMBOL(drm_prime_pages_to_sg);
  198. /* helper function to cleanup a GEM/prime object */
  199. void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg)
  200. {
  201. struct dma_buf_attachment *attach;
  202. struct dma_buf *dma_buf;
  203. attach = obj->import_attach;
  204. if (sg)
  205. dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
  206. dma_buf = attach->dmabuf;
  207. dma_buf_detach(attach->dmabuf, attach);
  208. /* remove the reference */
  209. dma_buf_put(dma_buf);
  210. }
  211. EXPORT_SYMBOL(drm_prime_gem_destroy);
  212. void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv)
  213. {
  214. INIT_LIST_HEAD(&prime_fpriv->head);
  215. mutex_init(&prime_fpriv->lock);
  216. }
  217. EXPORT_SYMBOL(drm_prime_init_file_private);
  218. void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv)
  219. {
  220. struct drm_prime_member *member, *safe;
  221. list_for_each_entry_safe(member, safe, &prime_fpriv->head, entry) {
  222. list_del(&member->entry);
  223. kfree(member);
  224. }
  225. }
  226. EXPORT_SYMBOL(drm_prime_destroy_file_private);
  227. int drm_prime_add_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t handle)
  228. {
  229. struct drm_prime_member *member;
  230. member = kmalloc(sizeof(*member), GFP_KERNEL);
  231. if (!member)
  232. return -ENOMEM;
  233. member->dma_buf = dma_buf;
  234. member->handle = handle;
  235. list_add(&member->entry, &prime_fpriv->head);
  236. return 0;
  237. }
  238. EXPORT_SYMBOL(drm_prime_add_imported_buf_handle);
  239. int drm_prime_lookup_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t *handle)
  240. {
  241. struct drm_prime_member *member;
  242. list_for_each_entry(member, &prime_fpriv->head, entry) {
  243. if (member->dma_buf == dma_buf) {
  244. *handle = member->handle;
  245. return 0;
  246. }
  247. }
  248. return -ENOENT;
  249. }
  250. EXPORT_SYMBOL(drm_prime_lookup_imported_buf_handle);
  251. void drm_prime_remove_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf)
  252. {
  253. struct drm_prime_member *member, *safe;
  254. mutex_lock(&prime_fpriv->lock);
  255. list_for_each_entry_safe(member, safe, &prime_fpriv->head, entry) {
  256. if (member->dma_buf == dma_buf) {
  257. list_del(&member->entry);
  258. kfree(member);
  259. }
  260. }
  261. mutex_unlock(&prime_fpriv->lock);
  262. }
  263. EXPORT_SYMBOL(drm_prime_remove_imported_buf_handle);