dma-buf.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. || !ops->kmap_atomic
  75. || !ops->kmap)) {
  76. return ERR_PTR(-EINVAL);
  77. }
  78. dmabuf = kzalloc(sizeof(struct dma_buf), GFP_KERNEL);
  79. if (dmabuf == NULL)
  80. return ERR_PTR(-ENOMEM);
  81. dmabuf->priv = priv;
  82. dmabuf->ops = ops;
  83. dmabuf->size = size;
  84. file = anon_inode_getfile("dmabuf", &dma_buf_fops, dmabuf, flags);
  85. dmabuf->file = file;
  86. mutex_init(&dmabuf->lock);
  87. INIT_LIST_HEAD(&dmabuf->attachments);
  88. return dmabuf;
  89. }
  90. EXPORT_SYMBOL_GPL(dma_buf_export);
  91. /**
  92. * dma_buf_fd - returns a file descriptor for the given dma_buf
  93. * @dmabuf: [in] pointer to dma_buf for which fd is required.
  94. * @flags: [in] flags to give to fd
  95. *
  96. * On success, returns an associated 'fd'. Else, returns error.
  97. */
  98. int dma_buf_fd(struct dma_buf *dmabuf, int flags)
  99. {
  100. int error, fd;
  101. if (!dmabuf || !dmabuf->file)
  102. return -EINVAL;
  103. error = get_unused_fd_flags(flags);
  104. if (error < 0)
  105. return error;
  106. fd = error;
  107. fd_install(fd, dmabuf->file);
  108. return fd;
  109. }
  110. EXPORT_SYMBOL_GPL(dma_buf_fd);
  111. /**
  112. * dma_buf_get - returns the dma_buf structure related to an fd
  113. * @fd: [in] fd associated with the dma_buf to be returned
  114. *
  115. * On success, returns the dma_buf structure associated with an fd; uses
  116. * file's refcounting done by fget to increase refcount. returns ERR_PTR
  117. * otherwise.
  118. */
  119. struct dma_buf *dma_buf_get(int fd)
  120. {
  121. struct file *file;
  122. file = fget(fd);
  123. if (!file)
  124. return ERR_PTR(-EBADF);
  125. if (!is_dma_buf_file(file)) {
  126. fput(file);
  127. return ERR_PTR(-EINVAL);
  128. }
  129. return file->private_data;
  130. }
  131. EXPORT_SYMBOL_GPL(dma_buf_get);
  132. /**
  133. * dma_buf_put - decreases refcount of the buffer
  134. * @dmabuf: [in] buffer to reduce refcount of
  135. *
  136. * Uses file's refcounting done implicitly by fput()
  137. */
  138. void dma_buf_put(struct dma_buf *dmabuf)
  139. {
  140. if (WARN_ON(!dmabuf || !dmabuf->file))
  141. return;
  142. fput(dmabuf->file);
  143. }
  144. EXPORT_SYMBOL_GPL(dma_buf_put);
  145. /**
  146. * dma_buf_attach - Add the device to dma_buf's attachments list; optionally,
  147. * calls attach() of dma_buf_ops to allow device-specific attach functionality
  148. * @dmabuf: [in] buffer to attach device to.
  149. * @dev: [in] device to be attached.
  150. *
  151. * Returns struct dma_buf_attachment * for this attachment; may return negative
  152. * error codes.
  153. *
  154. */
  155. struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
  156. struct device *dev)
  157. {
  158. struct dma_buf_attachment *attach;
  159. int ret;
  160. if (WARN_ON(!dmabuf || !dev))
  161. return ERR_PTR(-EINVAL);
  162. attach = kzalloc(sizeof(struct dma_buf_attachment), GFP_KERNEL);
  163. if (attach == NULL)
  164. return ERR_PTR(-ENOMEM);
  165. attach->dev = dev;
  166. attach->dmabuf = dmabuf;
  167. mutex_lock(&dmabuf->lock);
  168. if (dmabuf->ops->attach) {
  169. ret = dmabuf->ops->attach(dmabuf, dev, attach);
  170. if (ret)
  171. goto err_attach;
  172. }
  173. list_add(&attach->node, &dmabuf->attachments);
  174. mutex_unlock(&dmabuf->lock);
  175. return attach;
  176. err_attach:
  177. kfree(attach);
  178. mutex_unlock(&dmabuf->lock);
  179. return ERR_PTR(ret);
  180. }
  181. EXPORT_SYMBOL_GPL(dma_buf_attach);
  182. /**
  183. * dma_buf_detach - Remove the given attachment from dmabuf's attachments list;
  184. * optionally calls detach() of dma_buf_ops for device-specific detach
  185. * @dmabuf: [in] buffer to detach from.
  186. * @attach: [in] attachment to be detached; is free'd after this call.
  187. *
  188. */
  189. void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
  190. {
  191. if (WARN_ON(!dmabuf || !attach))
  192. return;
  193. mutex_lock(&dmabuf->lock);
  194. list_del(&attach->node);
  195. if (dmabuf->ops->detach)
  196. dmabuf->ops->detach(dmabuf, attach);
  197. mutex_unlock(&dmabuf->lock);
  198. kfree(attach);
  199. }
  200. EXPORT_SYMBOL_GPL(dma_buf_detach);
  201. /**
  202. * dma_buf_map_attachment - Returns the scatterlist table of the attachment;
  203. * mapped into _device_ address space. Is a wrapper for map_dma_buf() of the
  204. * dma_buf_ops.
  205. * @attach: [in] attachment whose scatterlist is to be returned
  206. * @direction: [in] direction of DMA transfer
  207. *
  208. * Returns sg_table containing the scatterlist to be returned; may return NULL
  209. * or ERR_PTR.
  210. *
  211. */
  212. struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,
  213. enum dma_data_direction direction)
  214. {
  215. struct sg_table *sg_table = ERR_PTR(-EINVAL);
  216. might_sleep();
  217. if (WARN_ON(!attach || !attach->dmabuf))
  218. return ERR_PTR(-EINVAL);
  219. sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction);
  220. return sg_table;
  221. }
  222. EXPORT_SYMBOL_GPL(dma_buf_map_attachment);
  223. /**
  224. * dma_buf_unmap_attachment - unmaps and decreases usecount of the buffer;might
  225. * deallocate the scatterlist associated. Is a wrapper for unmap_dma_buf() of
  226. * dma_buf_ops.
  227. * @attach: [in] attachment to unmap buffer from
  228. * @sg_table: [in] scatterlist info of the buffer to unmap
  229. * @direction: [in] direction of DMA transfer
  230. *
  231. */
  232. void dma_buf_unmap_attachment(struct dma_buf_attachment *attach,
  233. struct sg_table *sg_table,
  234. enum dma_data_direction direction)
  235. {
  236. if (WARN_ON(!attach || !attach->dmabuf || !sg_table))
  237. return;
  238. attach->dmabuf->ops->unmap_dma_buf(attach, sg_table,
  239. direction);
  240. }
  241. EXPORT_SYMBOL_GPL(dma_buf_unmap_attachment);
  242. /**
  243. * dma_buf_begin_cpu_access - Must be called before accessing a dma_buf from the
  244. * cpu in the kernel context. Calls begin_cpu_access to allow exporter-specific
  245. * preparations. Coherency is only guaranteed in the specified range for the
  246. * specified access direction.
  247. * @dma_buf: [in] buffer to prepare cpu access for.
  248. * @start: [in] start of range for cpu access.
  249. * @len: [in] length of range for cpu access.
  250. * @direction: [in] length of range for cpu access.
  251. *
  252. * Can return negative error values, returns 0 on success.
  253. */
  254. int dma_buf_begin_cpu_access(struct dma_buf *dmabuf, size_t start, size_t len,
  255. enum dma_data_direction direction)
  256. {
  257. int ret = 0;
  258. if (WARN_ON(!dmabuf))
  259. return -EINVAL;
  260. if (dmabuf->ops->begin_cpu_access)
  261. ret = dmabuf->ops->begin_cpu_access(dmabuf, start, len, direction);
  262. return ret;
  263. }
  264. EXPORT_SYMBOL_GPL(dma_buf_begin_cpu_access);
  265. /**
  266. * dma_buf_end_cpu_access - Must be called after accessing a dma_buf from the
  267. * cpu in the kernel context. Calls end_cpu_access to allow exporter-specific
  268. * actions. Coherency is only guaranteed in the specified range for the
  269. * specified access direction.
  270. * @dma_buf: [in] buffer to complete cpu access for.
  271. * @start: [in] start of range for cpu access.
  272. * @len: [in] length of range for cpu access.
  273. * @direction: [in] length of range for cpu access.
  274. *
  275. * This call must always succeed.
  276. */
  277. void dma_buf_end_cpu_access(struct dma_buf *dmabuf, size_t start, size_t len,
  278. enum dma_data_direction direction)
  279. {
  280. WARN_ON(!dmabuf);
  281. if (dmabuf->ops->end_cpu_access)
  282. dmabuf->ops->end_cpu_access(dmabuf, start, len, direction);
  283. }
  284. EXPORT_SYMBOL_GPL(dma_buf_end_cpu_access);
  285. /**
  286. * dma_buf_kmap_atomic - Map a page of the buffer object into kernel address
  287. * space. The same restrictions as for kmap_atomic and friends apply.
  288. * @dma_buf: [in] buffer to map page from.
  289. * @page_num: [in] page in PAGE_SIZE units to map.
  290. *
  291. * This call must always succeed, any necessary preparations that might fail
  292. * need to be done in begin_cpu_access.
  293. */
  294. void *dma_buf_kmap_atomic(struct dma_buf *dmabuf, unsigned long page_num)
  295. {
  296. WARN_ON(!dmabuf);
  297. return dmabuf->ops->kmap_atomic(dmabuf, page_num);
  298. }
  299. EXPORT_SYMBOL_GPL(dma_buf_kmap_atomic);
  300. /**
  301. * dma_buf_kunmap_atomic - Unmap a page obtained by dma_buf_kmap_atomic.
  302. * @dma_buf: [in] buffer to unmap page from.
  303. * @page_num: [in] page in PAGE_SIZE units to unmap.
  304. * @vaddr: [in] kernel space pointer obtained from dma_buf_kmap_atomic.
  305. *
  306. * This call must always succeed.
  307. */
  308. void dma_buf_kunmap_atomic(struct dma_buf *dmabuf, unsigned long page_num,
  309. void *vaddr)
  310. {
  311. WARN_ON(!dmabuf);
  312. if (dmabuf->ops->kunmap_atomic)
  313. dmabuf->ops->kunmap_atomic(dmabuf, page_num, vaddr);
  314. }
  315. EXPORT_SYMBOL_GPL(dma_buf_kunmap_atomic);
  316. /**
  317. * dma_buf_kmap - Map a page of the buffer object into kernel address space. The
  318. * same restrictions as for kmap and friends apply.
  319. * @dma_buf: [in] buffer to map page from.
  320. * @page_num: [in] page in PAGE_SIZE units to map.
  321. *
  322. * This call must always succeed, any necessary preparations that might fail
  323. * need to be done in begin_cpu_access.
  324. */
  325. void *dma_buf_kmap(struct dma_buf *dmabuf, unsigned long page_num)
  326. {
  327. WARN_ON(!dmabuf);
  328. return dmabuf->ops->kmap(dmabuf, page_num);
  329. }
  330. EXPORT_SYMBOL_GPL(dma_buf_kmap);
  331. /**
  332. * dma_buf_kunmap - Unmap a page obtained by dma_buf_kmap.
  333. * @dma_buf: [in] buffer to unmap page from.
  334. * @page_num: [in] page in PAGE_SIZE units to unmap.
  335. * @vaddr: [in] kernel space pointer obtained from dma_buf_kmap.
  336. *
  337. * This call must always succeed.
  338. */
  339. void dma_buf_kunmap(struct dma_buf *dmabuf, unsigned long page_num,
  340. void *vaddr)
  341. {
  342. WARN_ON(!dmabuf);
  343. if (dmabuf->ops->kunmap)
  344. dmabuf->ops->kunmap(dmabuf, page_num, vaddr);
  345. }
  346. EXPORT_SYMBOL_GPL(dma_buf_kunmap);