dma-buf.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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 int dma_buf_mmap_internal(struct file *file, struct vm_area_struct *vma)
  41. {
  42. struct dma_buf *dmabuf;
  43. if (!is_dma_buf_file(file))
  44. return -EINVAL;
  45. dmabuf = file->private_data;
  46. /* check for overflowing the buffer's size */
  47. if (vma->vm_pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) >
  48. dmabuf->size >> PAGE_SHIFT)
  49. return -EINVAL;
  50. return dmabuf->ops->mmap(dmabuf, vma);
  51. }
  52. static const struct file_operations dma_buf_fops = {
  53. .release = dma_buf_release,
  54. .mmap = dma_buf_mmap_internal,
  55. };
  56. /*
  57. * is_dma_buf_file - Check if struct file* is associated with dma_buf
  58. */
  59. static inline int is_dma_buf_file(struct file *file)
  60. {
  61. return file->f_op == &dma_buf_fops;
  62. }
  63. /**
  64. * dma_buf_export - Creates a new dma_buf, and associates an anon file
  65. * with this buffer, so it can be exported.
  66. * Also connect the allocator specific data and ops to the buffer.
  67. *
  68. * @priv: [in] Attach private data of allocator to this buffer
  69. * @ops: [in] Attach allocator-defined dma buf ops to the new buffer.
  70. * @size: [in] Size of the buffer
  71. * @flags: [in] mode flags for the file.
  72. *
  73. * Returns, on success, a newly created dma_buf object, which wraps the
  74. * supplied private data and operations for dma_buf_ops. On either missing
  75. * ops, or error in allocating struct dma_buf, will return negative error.
  76. *
  77. */
  78. struct dma_buf *dma_buf_export(void *priv, const struct dma_buf_ops *ops,
  79. size_t size, int flags)
  80. {
  81. struct dma_buf *dmabuf;
  82. struct file *file;
  83. if (WARN_ON(!priv || !ops
  84. || !ops->map_dma_buf
  85. || !ops->unmap_dma_buf
  86. || !ops->release
  87. || !ops->kmap_atomic
  88. || !ops->kmap
  89. || !ops->mmap)) {
  90. return ERR_PTR(-EINVAL);
  91. }
  92. dmabuf = kzalloc(sizeof(struct dma_buf), GFP_KERNEL);
  93. if (dmabuf == NULL)
  94. return ERR_PTR(-ENOMEM);
  95. dmabuf->priv = priv;
  96. dmabuf->ops = ops;
  97. dmabuf->size = size;
  98. file = anon_inode_getfile("dmabuf", &dma_buf_fops, dmabuf, flags);
  99. dmabuf->file = file;
  100. mutex_init(&dmabuf->lock);
  101. INIT_LIST_HEAD(&dmabuf->attachments);
  102. return dmabuf;
  103. }
  104. EXPORT_SYMBOL_GPL(dma_buf_export);
  105. /**
  106. * dma_buf_fd - returns a file descriptor for the given dma_buf
  107. * @dmabuf: [in] pointer to dma_buf for which fd is required.
  108. * @flags: [in] flags to give to fd
  109. *
  110. * On success, returns an associated 'fd'. Else, returns error.
  111. */
  112. int dma_buf_fd(struct dma_buf *dmabuf, int flags)
  113. {
  114. int error, fd;
  115. if (!dmabuf || !dmabuf->file)
  116. return -EINVAL;
  117. error = get_unused_fd_flags(flags);
  118. if (error < 0)
  119. return error;
  120. fd = error;
  121. fd_install(fd, dmabuf->file);
  122. return fd;
  123. }
  124. EXPORT_SYMBOL_GPL(dma_buf_fd);
  125. /**
  126. * dma_buf_get - returns the dma_buf structure related to an fd
  127. * @fd: [in] fd associated with the dma_buf to be returned
  128. *
  129. * On success, returns the dma_buf structure associated with an fd; uses
  130. * file's refcounting done by fget to increase refcount. returns ERR_PTR
  131. * otherwise.
  132. */
  133. struct dma_buf *dma_buf_get(int fd)
  134. {
  135. struct file *file;
  136. file = fget(fd);
  137. if (!file)
  138. return ERR_PTR(-EBADF);
  139. if (!is_dma_buf_file(file)) {
  140. fput(file);
  141. return ERR_PTR(-EINVAL);
  142. }
  143. return file->private_data;
  144. }
  145. EXPORT_SYMBOL_GPL(dma_buf_get);
  146. /**
  147. * dma_buf_put - decreases refcount of the buffer
  148. * @dmabuf: [in] buffer to reduce refcount of
  149. *
  150. * Uses file's refcounting done implicitly by fput()
  151. */
  152. void dma_buf_put(struct dma_buf *dmabuf)
  153. {
  154. if (WARN_ON(!dmabuf || !dmabuf->file))
  155. return;
  156. fput(dmabuf->file);
  157. }
  158. EXPORT_SYMBOL_GPL(dma_buf_put);
  159. /**
  160. * dma_buf_attach - Add the device to dma_buf's attachments list; optionally,
  161. * calls attach() of dma_buf_ops to allow device-specific attach functionality
  162. * @dmabuf: [in] buffer to attach device to.
  163. * @dev: [in] device to be attached.
  164. *
  165. * Returns struct dma_buf_attachment * for this attachment; may return negative
  166. * error codes.
  167. *
  168. */
  169. struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
  170. struct device *dev)
  171. {
  172. struct dma_buf_attachment *attach;
  173. int ret;
  174. if (WARN_ON(!dmabuf || !dev))
  175. return ERR_PTR(-EINVAL);
  176. attach = kzalloc(sizeof(struct dma_buf_attachment), GFP_KERNEL);
  177. if (attach == NULL)
  178. return ERR_PTR(-ENOMEM);
  179. attach->dev = dev;
  180. attach->dmabuf = dmabuf;
  181. mutex_lock(&dmabuf->lock);
  182. if (dmabuf->ops->attach) {
  183. ret = dmabuf->ops->attach(dmabuf, dev, attach);
  184. if (ret)
  185. goto err_attach;
  186. }
  187. list_add(&attach->node, &dmabuf->attachments);
  188. mutex_unlock(&dmabuf->lock);
  189. return attach;
  190. err_attach:
  191. kfree(attach);
  192. mutex_unlock(&dmabuf->lock);
  193. return ERR_PTR(ret);
  194. }
  195. EXPORT_SYMBOL_GPL(dma_buf_attach);
  196. /**
  197. * dma_buf_detach - Remove the given attachment from dmabuf's attachments list;
  198. * optionally calls detach() of dma_buf_ops for device-specific detach
  199. * @dmabuf: [in] buffer to detach from.
  200. * @attach: [in] attachment to be detached; is free'd after this call.
  201. *
  202. */
  203. void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
  204. {
  205. if (WARN_ON(!dmabuf || !attach))
  206. return;
  207. mutex_lock(&dmabuf->lock);
  208. list_del(&attach->node);
  209. if (dmabuf->ops->detach)
  210. dmabuf->ops->detach(dmabuf, attach);
  211. mutex_unlock(&dmabuf->lock);
  212. kfree(attach);
  213. }
  214. EXPORT_SYMBOL_GPL(dma_buf_detach);
  215. /**
  216. * dma_buf_map_attachment - Returns the scatterlist table of the attachment;
  217. * mapped into _device_ address space. Is a wrapper for map_dma_buf() of the
  218. * dma_buf_ops.
  219. * @attach: [in] attachment whose scatterlist is to be returned
  220. * @direction: [in] direction of DMA transfer
  221. *
  222. * Returns sg_table containing the scatterlist to be returned; may return NULL
  223. * or ERR_PTR.
  224. *
  225. */
  226. struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,
  227. enum dma_data_direction direction)
  228. {
  229. struct sg_table *sg_table = ERR_PTR(-EINVAL);
  230. might_sleep();
  231. if (WARN_ON(!attach || !attach->dmabuf))
  232. return ERR_PTR(-EINVAL);
  233. sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction);
  234. return sg_table;
  235. }
  236. EXPORT_SYMBOL_GPL(dma_buf_map_attachment);
  237. /**
  238. * dma_buf_unmap_attachment - unmaps and decreases usecount of the buffer;might
  239. * deallocate the scatterlist associated. Is a wrapper for unmap_dma_buf() of
  240. * dma_buf_ops.
  241. * @attach: [in] attachment to unmap buffer from
  242. * @sg_table: [in] scatterlist info of the buffer to unmap
  243. * @direction: [in] direction of DMA transfer
  244. *
  245. */
  246. void dma_buf_unmap_attachment(struct dma_buf_attachment *attach,
  247. struct sg_table *sg_table,
  248. enum dma_data_direction direction)
  249. {
  250. if (WARN_ON(!attach || !attach->dmabuf || !sg_table))
  251. return;
  252. attach->dmabuf->ops->unmap_dma_buf(attach, sg_table,
  253. direction);
  254. }
  255. EXPORT_SYMBOL_GPL(dma_buf_unmap_attachment);
  256. /**
  257. * dma_buf_begin_cpu_access - Must be called before accessing a dma_buf from the
  258. * cpu in the kernel context. Calls begin_cpu_access to allow exporter-specific
  259. * preparations. Coherency is only guaranteed in the specified range for the
  260. * specified access direction.
  261. * @dma_buf: [in] buffer to prepare cpu access for.
  262. * @start: [in] start of range for cpu access.
  263. * @len: [in] length of range for cpu access.
  264. * @direction: [in] length of range for cpu access.
  265. *
  266. * Can return negative error values, returns 0 on success.
  267. */
  268. int dma_buf_begin_cpu_access(struct dma_buf *dmabuf, size_t start, size_t len,
  269. enum dma_data_direction direction)
  270. {
  271. int ret = 0;
  272. if (WARN_ON(!dmabuf))
  273. return -EINVAL;
  274. if (dmabuf->ops->begin_cpu_access)
  275. ret = dmabuf->ops->begin_cpu_access(dmabuf, start, len, direction);
  276. return ret;
  277. }
  278. EXPORT_SYMBOL_GPL(dma_buf_begin_cpu_access);
  279. /**
  280. * dma_buf_end_cpu_access - Must be called after accessing a dma_buf from the
  281. * cpu in the kernel context. Calls end_cpu_access to allow exporter-specific
  282. * actions. Coherency is only guaranteed in the specified range for the
  283. * specified access direction.
  284. * @dma_buf: [in] buffer to complete cpu access for.
  285. * @start: [in] start of range for cpu access.
  286. * @len: [in] length of range for cpu access.
  287. * @direction: [in] length of range for cpu access.
  288. *
  289. * This call must always succeed.
  290. */
  291. void dma_buf_end_cpu_access(struct dma_buf *dmabuf, size_t start, size_t len,
  292. enum dma_data_direction direction)
  293. {
  294. WARN_ON(!dmabuf);
  295. if (dmabuf->ops->end_cpu_access)
  296. dmabuf->ops->end_cpu_access(dmabuf, start, len, direction);
  297. }
  298. EXPORT_SYMBOL_GPL(dma_buf_end_cpu_access);
  299. /**
  300. * dma_buf_kmap_atomic - Map a page of the buffer object into kernel address
  301. * space. The same restrictions as for kmap_atomic and friends apply.
  302. * @dma_buf: [in] buffer to map page from.
  303. * @page_num: [in] page in PAGE_SIZE units to map.
  304. *
  305. * This call must always succeed, any necessary preparations that might fail
  306. * need to be done in begin_cpu_access.
  307. */
  308. void *dma_buf_kmap_atomic(struct dma_buf *dmabuf, unsigned long page_num)
  309. {
  310. WARN_ON(!dmabuf);
  311. return dmabuf->ops->kmap_atomic(dmabuf, page_num);
  312. }
  313. EXPORT_SYMBOL_GPL(dma_buf_kmap_atomic);
  314. /**
  315. * dma_buf_kunmap_atomic - Unmap a page obtained by dma_buf_kmap_atomic.
  316. * @dma_buf: [in] buffer to unmap page from.
  317. * @page_num: [in] page in PAGE_SIZE units to unmap.
  318. * @vaddr: [in] kernel space pointer obtained from dma_buf_kmap_atomic.
  319. *
  320. * This call must always succeed.
  321. */
  322. void dma_buf_kunmap_atomic(struct dma_buf *dmabuf, unsigned long page_num,
  323. void *vaddr)
  324. {
  325. WARN_ON(!dmabuf);
  326. if (dmabuf->ops->kunmap_atomic)
  327. dmabuf->ops->kunmap_atomic(dmabuf, page_num, vaddr);
  328. }
  329. EXPORT_SYMBOL_GPL(dma_buf_kunmap_atomic);
  330. /**
  331. * dma_buf_kmap - Map a page of the buffer object into kernel address space. The
  332. * same restrictions as for kmap and friends apply.
  333. * @dma_buf: [in] buffer to map page from.
  334. * @page_num: [in] page in PAGE_SIZE units to map.
  335. *
  336. * This call must always succeed, any necessary preparations that might fail
  337. * need to be done in begin_cpu_access.
  338. */
  339. void *dma_buf_kmap(struct dma_buf *dmabuf, unsigned long page_num)
  340. {
  341. WARN_ON(!dmabuf);
  342. return dmabuf->ops->kmap(dmabuf, page_num);
  343. }
  344. EXPORT_SYMBOL_GPL(dma_buf_kmap);
  345. /**
  346. * dma_buf_kunmap - Unmap a page obtained by dma_buf_kmap.
  347. * @dma_buf: [in] buffer to unmap page from.
  348. * @page_num: [in] page in PAGE_SIZE units to unmap.
  349. * @vaddr: [in] kernel space pointer obtained from dma_buf_kmap.
  350. *
  351. * This call must always succeed.
  352. */
  353. void dma_buf_kunmap(struct dma_buf *dmabuf, unsigned long page_num,
  354. void *vaddr)
  355. {
  356. WARN_ON(!dmabuf);
  357. if (dmabuf->ops->kunmap)
  358. dmabuf->ops->kunmap(dmabuf, page_num, vaddr);
  359. }
  360. EXPORT_SYMBOL_GPL(dma_buf_kunmap);
  361. /**
  362. * dma_buf_mmap - Setup up a userspace mmap with the given vma
  363. * @dma_buf: [in] buffer that should back the vma
  364. * @vma: [in] vma for the mmap
  365. * @pgoff: [in] offset in pages where this mmap should start within the
  366. * dma-buf buffer.
  367. *
  368. * This function adjusts the passed in vma so that it points at the file of the
  369. * dma_buf operation. It alsog adjusts the starting pgoff and does bounds
  370. * checking on the size of the vma. Then it calls the exporters mmap function to
  371. * set up the mapping.
  372. *
  373. * Can return negative error values, returns 0 on success.
  374. */
  375. int dma_buf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma,
  376. unsigned long pgoff)
  377. {
  378. if (WARN_ON(!dmabuf || !vma))
  379. return -EINVAL;
  380. /* check for offset overflow */
  381. if (pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) < pgoff)
  382. return -EOVERFLOW;
  383. /* check for overflowing the buffer's size */
  384. if (pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) >
  385. dmabuf->size >> PAGE_SHIFT)
  386. return -EINVAL;
  387. /* readjust the vma */
  388. if (vma->vm_file)
  389. fput(vma->vm_file);
  390. vma->vm_file = get_file(dmabuf->file);
  391. vma->vm_pgoff = pgoff;
  392. return dmabuf->ops->mmap(dmabuf, vma);
  393. }
  394. EXPORT_SYMBOL_GPL(dma_buf_mmap);