dma-buf.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Framework for buffer objects that can be shared across devices/subsystems.
  3. *
  4. * Copyright(C) 2011 Linaro Limited. All rights reserved.
  5. * Author: Sumit Semwal <sumit.semwal@ti.com>
  6. *
  7. * Many thanks to linaro-mm-sig list, and specially
  8. * Arnd Bergmann <arnd@arndb.de>, Rob Clark <rob@ti.com> and
  9. * Daniel Vetter <daniel@ffwll.ch> for their support in creation and
  10. * refining of this idea.
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License version 2 as published by
  14. * the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful, but WITHOUT
  17. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  18. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  19. * more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along with
  22. * this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #include <linux/fs.h>
  25. #include <linux/slab.h>
  26. #include <linux/dma-buf.h>
  27. #include <linux/anon_inodes.h>
  28. #include <linux/export.h>
  29. static inline int is_dma_buf_file(struct file *);
  30. static int dma_buf_release(struct inode *inode, struct file *file)
  31. {
  32. struct dma_buf *dmabuf;
  33. if (!is_dma_buf_file(file))
  34. return -EINVAL;
  35. dmabuf = file->private_data;
  36. dmabuf->ops->release(dmabuf);
  37. kfree(dmabuf);
  38. return 0;
  39. }
  40. static const struct file_operations dma_buf_fops = {
  41. .release = dma_buf_release,
  42. };
  43. /*
  44. * is_dma_buf_file - Check if struct file* is associated with dma_buf
  45. */
  46. static inline int is_dma_buf_file(struct file *file)
  47. {
  48. return file->f_op == &dma_buf_fops;
  49. }
  50. /**
  51. * dma_buf_export - Creates a new dma_buf, and associates an anon file
  52. * with this buffer, so it can be exported.
  53. * Also connect the allocator specific data and ops to the buffer.
  54. *
  55. * @priv: [in] Attach private data of allocator to this buffer
  56. * @ops: [in] Attach allocator-defined dma buf ops to the new buffer.
  57. * @size: [in] Size of the buffer
  58. * @flags: [in] mode flags for the file.
  59. *
  60. * Returns, on success, a newly created dma_buf object, which wraps the
  61. * supplied private data and operations for dma_buf_ops. On either missing
  62. * ops, or error in allocating struct dma_buf, will return negative error.
  63. *
  64. */
  65. struct dma_buf *dma_buf_export(void *priv, struct dma_buf_ops *ops,
  66. size_t size, int flags)
  67. {
  68. struct dma_buf *dmabuf;
  69. struct file *file;
  70. if (WARN_ON(!priv || !ops
  71. || !ops->map_dma_buf
  72. || !ops->unmap_dma_buf
  73. || !ops->release)) {
  74. return ERR_PTR(-EINVAL);
  75. }
  76. dmabuf = kzalloc(sizeof(struct dma_buf), GFP_KERNEL);
  77. if (dmabuf == NULL)
  78. return ERR_PTR(-ENOMEM);
  79. dmabuf->priv = priv;
  80. dmabuf->ops = ops;
  81. dmabuf->size = size;
  82. file = anon_inode_getfile("dmabuf", &dma_buf_fops, dmabuf, flags);
  83. dmabuf->file = file;
  84. mutex_init(&dmabuf->lock);
  85. INIT_LIST_HEAD(&dmabuf->attachments);
  86. return dmabuf;
  87. }
  88. EXPORT_SYMBOL_GPL(dma_buf_export);
  89. /**
  90. * dma_buf_fd - returns a file descriptor for the given dma_buf
  91. * @dmabuf: [in] pointer to dma_buf for which fd is required.
  92. *
  93. * On success, returns an associated 'fd'. Else, returns error.
  94. */
  95. int dma_buf_fd(struct dma_buf *dmabuf)
  96. {
  97. int error, fd;
  98. if (!dmabuf || !dmabuf->file)
  99. return -EINVAL;
  100. error = get_unused_fd();
  101. if (error < 0)
  102. return error;
  103. fd = error;
  104. fd_install(fd, dmabuf->file);
  105. return fd;
  106. }
  107. EXPORT_SYMBOL_GPL(dma_buf_fd);
  108. /**
  109. * dma_buf_get - returns the dma_buf structure related to an fd
  110. * @fd: [in] fd associated with the dma_buf to be returned
  111. *
  112. * On success, returns the dma_buf structure associated with an fd; uses
  113. * file's refcounting done by fget to increase refcount. returns ERR_PTR
  114. * otherwise.
  115. */
  116. struct dma_buf *dma_buf_get(int fd)
  117. {
  118. struct file *file;
  119. file = fget(fd);
  120. if (!file)
  121. return ERR_PTR(-EBADF);
  122. if (!is_dma_buf_file(file)) {
  123. fput(file);
  124. return ERR_PTR(-EINVAL);
  125. }
  126. return file->private_data;
  127. }
  128. EXPORT_SYMBOL_GPL(dma_buf_get);
  129. /**
  130. * dma_buf_put - decreases refcount of the buffer
  131. * @dmabuf: [in] buffer to reduce refcount of
  132. *
  133. * Uses file's refcounting done implicitly by fput()
  134. */
  135. void dma_buf_put(struct dma_buf *dmabuf)
  136. {
  137. if (WARN_ON(!dmabuf || !dmabuf->file))
  138. return;
  139. fput(dmabuf->file);
  140. }
  141. EXPORT_SYMBOL_GPL(dma_buf_put);
  142. /**
  143. * dma_buf_attach - Add the device to dma_buf's attachments list; optionally,
  144. * calls attach() of dma_buf_ops to allow device-specific attach functionality
  145. * @dmabuf: [in] buffer to attach device to.
  146. * @dev: [in] device to be attached.
  147. *
  148. * Returns struct dma_buf_attachment * for this attachment; may return negative
  149. * error codes.
  150. *
  151. */
  152. struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
  153. struct device *dev)
  154. {
  155. struct dma_buf_attachment *attach;
  156. int ret;
  157. if (WARN_ON(!dmabuf || !dev || !dmabuf->ops))
  158. return ERR_PTR(-EINVAL);
  159. attach = kzalloc(sizeof(struct dma_buf_attachment), GFP_KERNEL);
  160. if (attach == NULL)
  161. goto err_alloc;
  162. mutex_lock(&dmabuf->lock);
  163. attach->dev = dev;
  164. attach->dmabuf = dmabuf;
  165. if (dmabuf->ops->attach) {
  166. ret = dmabuf->ops->attach(dmabuf, dev, attach);
  167. if (ret)
  168. goto err_attach;
  169. }
  170. list_add(&attach->node, &dmabuf->attachments);
  171. mutex_unlock(&dmabuf->lock);
  172. return attach;
  173. err_alloc:
  174. return ERR_PTR(-ENOMEM);
  175. err_attach:
  176. kfree(attach);
  177. mutex_unlock(&dmabuf->lock);
  178. return ERR_PTR(ret);
  179. }
  180. EXPORT_SYMBOL_GPL(dma_buf_attach);
  181. /**
  182. * dma_buf_detach - Remove the given attachment from dmabuf's attachments list;
  183. * optionally calls detach() of dma_buf_ops for device-specific detach
  184. * @dmabuf: [in] buffer to detach from.
  185. * @attach: [in] attachment to be detached; is free'd after this call.
  186. *
  187. */
  188. void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
  189. {
  190. if (WARN_ON(!dmabuf || !attach || !dmabuf->ops))
  191. return;
  192. mutex_lock(&dmabuf->lock);
  193. list_del(&attach->node);
  194. if (dmabuf->ops->detach)
  195. dmabuf->ops->detach(dmabuf, attach);
  196. mutex_unlock(&dmabuf->lock);
  197. kfree(attach);
  198. }
  199. EXPORT_SYMBOL_GPL(dma_buf_detach);
  200. /**
  201. * dma_buf_map_attachment - Returns the scatterlist table of the attachment;
  202. * mapped into _device_ address space. Is a wrapper for map_dma_buf() of the
  203. * dma_buf_ops.
  204. * @attach: [in] attachment whose scatterlist is to be returned
  205. * @direction: [in] direction of DMA transfer
  206. *
  207. * Returns sg_table containing the scatterlist to be returned; may return NULL
  208. * or ERR_PTR.
  209. *
  210. */
  211. struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,
  212. enum dma_data_direction direction)
  213. {
  214. struct sg_table *sg_table = ERR_PTR(-EINVAL);
  215. might_sleep();
  216. if (WARN_ON(!attach || !attach->dmabuf || !attach->dmabuf->ops))
  217. return ERR_PTR(-EINVAL);
  218. mutex_lock(&attach->dmabuf->lock);
  219. if (attach->dmabuf->ops->map_dma_buf)
  220. sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction);
  221. mutex_unlock(&attach->dmabuf->lock);
  222. return sg_table;
  223. }
  224. EXPORT_SYMBOL_GPL(dma_buf_map_attachment);
  225. /**
  226. * dma_buf_unmap_attachment - unmaps and decreases usecount of the buffer;might
  227. * deallocate the scatterlist associated. Is a wrapper for unmap_dma_buf() of
  228. * dma_buf_ops.
  229. * @attach: [in] attachment to unmap buffer from
  230. * @sg_table: [in] scatterlist info of the buffer to unmap
  231. *
  232. */
  233. void dma_buf_unmap_attachment(struct dma_buf_attachment *attach,
  234. struct sg_table *sg_table)
  235. {
  236. if (WARN_ON(!attach || !attach->dmabuf || !sg_table
  237. || !attach->dmabuf->ops))
  238. return;
  239. mutex_lock(&attach->dmabuf->lock);
  240. if (attach->dmabuf->ops->unmap_dma_buf)
  241. attach->dmabuf->ops->unmap_dma_buf(attach, sg_table);
  242. mutex_unlock(&attach->dmabuf->lock);
  243. }
  244. EXPORT_SYMBOL_GPL(dma_buf_unmap_attachment);