dma-buf.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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, const 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. * @flags: [in] flags to give to fd
  93. *
  94. * On success, returns an associated 'fd'. Else, returns error.
  95. */
  96. int dma_buf_fd(struct dma_buf *dmabuf, int flags)
  97. {
  98. int error, fd;
  99. if (!dmabuf || !dmabuf->file)
  100. return -EINVAL;
  101. error = get_unused_fd_flags(flags);
  102. if (error < 0)
  103. return error;
  104. fd = error;
  105. fd_install(fd, dmabuf->file);
  106. return fd;
  107. }
  108. EXPORT_SYMBOL_GPL(dma_buf_fd);
  109. /**
  110. * dma_buf_get - returns the dma_buf structure related to an fd
  111. * @fd: [in] fd associated with the dma_buf to be returned
  112. *
  113. * On success, returns the dma_buf structure associated with an fd; uses
  114. * file's refcounting done by fget to increase refcount. returns ERR_PTR
  115. * otherwise.
  116. */
  117. struct dma_buf *dma_buf_get(int fd)
  118. {
  119. struct file *file;
  120. file = fget(fd);
  121. if (!file)
  122. return ERR_PTR(-EBADF);
  123. if (!is_dma_buf_file(file)) {
  124. fput(file);
  125. return ERR_PTR(-EINVAL);
  126. }
  127. return file->private_data;
  128. }
  129. EXPORT_SYMBOL_GPL(dma_buf_get);
  130. /**
  131. * dma_buf_put - decreases refcount of the buffer
  132. * @dmabuf: [in] buffer to reduce refcount of
  133. *
  134. * Uses file's refcounting done implicitly by fput()
  135. */
  136. void dma_buf_put(struct dma_buf *dmabuf)
  137. {
  138. if (WARN_ON(!dmabuf || !dmabuf->file))
  139. return;
  140. fput(dmabuf->file);
  141. }
  142. EXPORT_SYMBOL_GPL(dma_buf_put);
  143. /**
  144. * dma_buf_attach - Add the device to dma_buf's attachments list; optionally,
  145. * calls attach() of dma_buf_ops to allow device-specific attach functionality
  146. * @dmabuf: [in] buffer to attach device to.
  147. * @dev: [in] device to be attached.
  148. *
  149. * Returns struct dma_buf_attachment * for this attachment; may return negative
  150. * error codes.
  151. *
  152. */
  153. struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
  154. struct device *dev)
  155. {
  156. struct dma_buf_attachment *attach;
  157. int ret;
  158. if (WARN_ON(!dmabuf || !dev))
  159. return ERR_PTR(-EINVAL);
  160. attach = kzalloc(sizeof(struct dma_buf_attachment), GFP_KERNEL);
  161. if (attach == NULL)
  162. return ERR_PTR(-ENOMEM);
  163. attach->dev = dev;
  164. attach->dmabuf = dmabuf;
  165. mutex_lock(&dmabuf->lock);
  166. if (dmabuf->ops->attach) {
  167. ret = dmabuf->ops->attach(dmabuf, dev, attach);
  168. if (ret)
  169. goto err_attach;
  170. }
  171. list_add(&attach->node, &dmabuf->attachments);
  172. mutex_unlock(&dmabuf->lock);
  173. return attach;
  174. err_attach:
  175. kfree(attach);
  176. mutex_unlock(&dmabuf->lock);
  177. return ERR_PTR(ret);
  178. }
  179. EXPORT_SYMBOL_GPL(dma_buf_attach);
  180. /**
  181. * dma_buf_detach - Remove the given attachment from dmabuf's attachments list;
  182. * optionally calls detach() of dma_buf_ops for device-specific detach
  183. * @dmabuf: [in] buffer to detach from.
  184. * @attach: [in] attachment to be detached; is free'd after this call.
  185. *
  186. */
  187. void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
  188. {
  189. if (WARN_ON(!dmabuf || !attach))
  190. return;
  191. mutex_lock(&dmabuf->lock);
  192. list_del(&attach->node);
  193. if (dmabuf->ops->detach)
  194. dmabuf->ops->detach(dmabuf, attach);
  195. mutex_unlock(&dmabuf->lock);
  196. kfree(attach);
  197. }
  198. EXPORT_SYMBOL_GPL(dma_buf_detach);
  199. /**
  200. * dma_buf_map_attachment - Returns the scatterlist table of the attachment;
  201. * mapped into _device_ address space. Is a wrapper for map_dma_buf() of the
  202. * dma_buf_ops.
  203. * @attach: [in] attachment whose scatterlist is to be returned
  204. * @direction: [in] direction of DMA transfer
  205. *
  206. * Returns sg_table containing the scatterlist to be returned; may return NULL
  207. * or ERR_PTR.
  208. *
  209. */
  210. struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,
  211. enum dma_data_direction direction)
  212. {
  213. struct sg_table *sg_table = ERR_PTR(-EINVAL);
  214. might_sleep();
  215. if (WARN_ON(!attach || !attach->dmabuf))
  216. return ERR_PTR(-EINVAL);
  217. sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction);
  218. return sg_table;
  219. }
  220. EXPORT_SYMBOL_GPL(dma_buf_map_attachment);
  221. /**
  222. * dma_buf_unmap_attachment - unmaps and decreases usecount of the buffer;might
  223. * deallocate the scatterlist associated. Is a wrapper for unmap_dma_buf() of
  224. * dma_buf_ops.
  225. * @attach: [in] attachment to unmap buffer from
  226. * @sg_table: [in] scatterlist info of the buffer to unmap
  227. * @direction: [in] direction of DMA transfer
  228. *
  229. */
  230. void dma_buf_unmap_attachment(struct dma_buf_attachment *attach,
  231. struct sg_table *sg_table,
  232. enum dma_data_direction direction)
  233. {
  234. if (WARN_ON(!attach || !attach->dmabuf || !sg_table))
  235. return;
  236. attach->dmabuf->ops->unmap_dma_buf(attach, sg_table,
  237. direction);
  238. }
  239. EXPORT_SYMBOL_GPL(dma_buf_unmap_attachment);