dma-buf.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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. BUG_ON(dmabuf->vmapping_counter);
  37. dmabuf->ops->release(dmabuf);
  38. kfree(dmabuf);
  39. return 0;
  40. }
  41. static int dma_buf_mmap_internal(struct file *file, struct vm_area_struct *vma)
  42. {
  43. struct dma_buf *dmabuf;
  44. if (!is_dma_buf_file(file))
  45. return -EINVAL;
  46. dmabuf = file->private_data;
  47. /* check for overflowing the buffer's size */
  48. if (vma->vm_pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) >
  49. dmabuf->size >> PAGE_SHIFT)
  50. return -EINVAL;
  51. return dmabuf->ops->mmap(dmabuf, vma);
  52. }
  53. static const struct file_operations dma_buf_fops = {
  54. .release = dma_buf_release,
  55. .mmap = dma_buf_mmap_internal,
  56. };
  57. /*
  58. * is_dma_buf_file - Check if struct file* is associated with dma_buf
  59. */
  60. static inline int is_dma_buf_file(struct file *file)
  61. {
  62. return file->f_op == &dma_buf_fops;
  63. }
  64. /**
  65. * dma_buf_export - Creates a new dma_buf, and associates an anon file
  66. * with this buffer, so it can be exported.
  67. * Also connect the allocator specific data and ops to the buffer.
  68. *
  69. * @priv: [in] Attach private data of allocator to this buffer
  70. * @ops: [in] Attach allocator-defined dma buf ops to the new buffer.
  71. * @size: [in] Size of the buffer
  72. * @flags: [in] mode flags for the file.
  73. *
  74. * Returns, on success, a newly created dma_buf object, which wraps the
  75. * supplied private data and operations for dma_buf_ops. On either missing
  76. * ops, or error in allocating struct dma_buf, will return negative error.
  77. *
  78. */
  79. struct dma_buf *dma_buf_export(void *priv, const struct dma_buf_ops *ops,
  80. size_t size, int flags)
  81. {
  82. struct dma_buf *dmabuf;
  83. struct file *file;
  84. if (WARN_ON(!priv || !ops
  85. || !ops->map_dma_buf
  86. || !ops->unmap_dma_buf
  87. || !ops->release
  88. || !ops->kmap_atomic
  89. || !ops->kmap
  90. || !ops->mmap)) {
  91. return ERR_PTR(-EINVAL);
  92. }
  93. dmabuf = kzalloc(sizeof(struct dma_buf), GFP_KERNEL);
  94. if (dmabuf == NULL)
  95. return ERR_PTR(-ENOMEM);
  96. dmabuf->priv = priv;
  97. dmabuf->ops = ops;
  98. dmabuf->size = size;
  99. file = anon_inode_getfile("dmabuf", &dma_buf_fops, dmabuf, flags);
  100. dmabuf->file = file;
  101. mutex_init(&dmabuf->lock);
  102. INIT_LIST_HEAD(&dmabuf->attachments);
  103. return dmabuf;
  104. }
  105. EXPORT_SYMBOL_GPL(dma_buf_export);
  106. /**
  107. * dma_buf_fd - returns a file descriptor for the given dma_buf
  108. * @dmabuf: [in] pointer to dma_buf for which fd is required.
  109. * @flags: [in] flags to give to fd
  110. *
  111. * On success, returns an associated 'fd'. Else, returns error.
  112. */
  113. int dma_buf_fd(struct dma_buf *dmabuf, int flags)
  114. {
  115. int fd;
  116. if (!dmabuf || !dmabuf->file)
  117. return -EINVAL;
  118. fd = get_unused_fd_flags(flags);
  119. if (fd < 0)
  120. return fd;
  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. might_sleep();
  251. if (WARN_ON(!attach || !attach->dmabuf || !sg_table))
  252. return;
  253. attach->dmabuf->ops->unmap_dma_buf(attach, sg_table,
  254. direction);
  255. }
  256. EXPORT_SYMBOL_GPL(dma_buf_unmap_attachment);
  257. /**
  258. * dma_buf_begin_cpu_access - Must be called before accessing a dma_buf from the
  259. * cpu in the kernel context. Calls begin_cpu_access to allow exporter-specific
  260. * preparations. Coherency is only guaranteed in the specified range for the
  261. * specified access direction.
  262. * @dmabuf: [in] buffer to prepare cpu access for.
  263. * @start: [in] start of range for cpu access.
  264. * @len: [in] length of range for cpu access.
  265. * @direction: [in] length of range for cpu access.
  266. *
  267. * Can return negative error values, returns 0 on success.
  268. */
  269. int dma_buf_begin_cpu_access(struct dma_buf *dmabuf, size_t start, size_t len,
  270. enum dma_data_direction direction)
  271. {
  272. int ret = 0;
  273. if (WARN_ON(!dmabuf))
  274. return -EINVAL;
  275. if (dmabuf->ops->begin_cpu_access)
  276. ret = dmabuf->ops->begin_cpu_access(dmabuf, start, len, direction);
  277. return ret;
  278. }
  279. EXPORT_SYMBOL_GPL(dma_buf_begin_cpu_access);
  280. /**
  281. * dma_buf_end_cpu_access - Must be called after accessing a dma_buf from the
  282. * cpu in the kernel context. Calls end_cpu_access to allow exporter-specific
  283. * actions. Coherency is only guaranteed in the specified range for the
  284. * specified access direction.
  285. * @dmabuf: [in] buffer to complete cpu access for.
  286. * @start: [in] start of range for cpu access.
  287. * @len: [in] length of range for cpu access.
  288. * @direction: [in] length of range for cpu access.
  289. *
  290. * This call must always succeed.
  291. */
  292. void dma_buf_end_cpu_access(struct dma_buf *dmabuf, size_t start, size_t len,
  293. enum dma_data_direction direction)
  294. {
  295. WARN_ON(!dmabuf);
  296. if (dmabuf->ops->end_cpu_access)
  297. dmabuf->ops->end_cpu_access(dmabuf, start, len, direction);
  298. }
  299. EXPORT_SYMBOL_GPL(dma_buf_end_cpu_access);
  300. /**
  301. * dma_buf_kmap_atomic - Map a page of the buffer object into kernel address
  302. * space. The same restrictions as for kmap_atomic and friends apply.
  303. * @dmabuf: [in] buffer to map page from.
  304. * @page_num: [in] page in PAGE_SIZE units to map.
  305. *
  306. * This call must always succeed, any necessary preparations that might fail
  307. * need to be done in begin_cpu_access.
  308. */
  309. void *dma_buf_kmap_atomic(struct dma_buf *dmabuf, unsigned long page_num)
  310. {
  311. WARN_ON(!dmabuf);
  312. return dmabuf->ops->kmap_atomic(dmabuf, page_num);
  313. }
  314. EXPORT_SYMBOL_GPL(dma_buf_kmap_atomic);
  315. /**
  316. * dma_buf_kunmap_atomic - Unmap a page obtained by dma_buf_kmap_atomic.
  317. * @dmabuf: [in] buffer to unmap page from.
  318. * @page_num: [in] page in PAGE_SIZE units to unmap.
  319. * @vaddr: [in] kernel space pointer obtained from dma_buf_kmap_atomic.
  320. *
  321. * This call must always succeed.
  322. */
  323. void dma_buf_kunmap_atomic(struct dma_buf *dmabuf, unsigned long page_num,
  324. void *vaddr)
  325. {
  326. WARN_ON(!dmabuf);
  327. if (dmabuf->ops->kunmap_atomic)
  328. dmabuf->ops->kunmap_atomic(dmabuf, page_num, vaddr);
  329. }
  330. EXPORT_SYMBOL_GPL(dma_buf_kunmap_atomic);
  331. /**
  332. * dma_buf_kmap - Map a page of the buffer object into kernel address space. The
  333. * same restrictions as for kmap and friends apply.
  334. * @dmabuf: [in] buffer to map page from.
  335. * @page_num: [in] page in PAGE_SIZE units to map.
  336. *
  337. * This call must always succeed, any necessary preparations that might fail
  338. * need to be done in begin_cpu_access.
  339. */
  340. void *dma_buf_kmap(struct dma_buf *dmabuf, unsigned long page_num)
  341. {
  342. WARN_ON(!dmabuf);
  343. return dmabuf->ops->kmap(dmabuf, page_num);
  344. }
  345. EXPORT_SYMBOL_GPL(dma_buf_kmap);
  346. /**
  347. * dma_buf_kunmap - Unmap a page obtained by dma_buf_kmap.
  348. * @dmabuf: [in] buffer to unmap page from.
  349. * @page_num: [in] page in PAGE_SIZE units to unmap.
  350. * @vaddr: [in] kernel space pointer obtained from dma_buf_kmap.
  351. *
  352. * This call must always succeed.
  353. */
  354. void dma_buf_kunmap(struct dma_buf *dmabuf, unsigned long page_num,
  355. void *vaddr)
  356. {
  357. WARN_ON(!dmabuf);
  358. if (dmabuf->ops->kunmap)
  359. dmabuf->ops->kunmap(dmabuf, page_num, vaddr);
  360. }
  361. EXPORT_SYMBOL_GPL(dma_buf_kunmap);
  362. /**
  363. * dma_buf_mmap - Setup up a userspace mmap with the given vma
  364. * @dmabuf: [in] buffer that should back the vma
  365. * @vma: [in] vma for the mmap
  366. * @pgoff: [in] offset in pages where this mmap should start within the
  367. * dma-buf buffer.
  368. *
  369. * This function adjusts the passed in vma so that it points at the file of the
  370. * dma_buf operation. It alsog adjusts the starting pgoff and does bounds
  371. * checking on the size of the vma. Then it calls the exporters mmap function to
  372. * set up the mapping.
  373. *
  374. * Can return negative error values, returns 0 on success.
  375. */
  376. int dma_buf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma,
  377. unsigned long pgoff)
  378. {
  379. struct file *oldfile;
  380. int ret;
  381. if (WARN_ON(!dmabuf || !vma))
  382. return -EINVAL;
  383. /* check for offset overflow */
  384. if (pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) < pgoff)
  385. return -EOVERFLOW;
  386. /* check for overflowing the buffer's size */
  387. if (pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) >
  388. dmabuf->size >> PAGE_SHIFT)
  389. return -EINVAL;
  390. /* readjust the vma */
  391. get_file(dmabuf->file);
  392. oldfile = vma->vm_file;
  393. vma->vm_file = dmabuf->file;
  394. vma->vm_pgoff = pgoff;
  395. ret = dmabuf->ops->mmap(dmabuf, vma);
  396. if (ret) {
  397. /* restore old parameters on failure */
  398. vma->vm_file = oldfile;
  399. fput(dmabuf->file);
  400. } else {
  401. if (oldfile)
  402. fput(oldfile);
  403. }
  404. return ret;
  405. }
  406. EXPORT_SYMBOL_GPL(dma_buf_mmap);
  407. /**
  408. * dma_buf_vmap - Create virtual mapping for the buffer object into kernel
  409. * address space. Same restrictions as for vmap and friends apply.
  410. * @dmabuf: [in] buffer to vmap
  411. *
  412. * This call may fail due to lack of virtual mapping address space.
  413. * These calls are optional in drivers. The intended use for them
  414. * is for mapping objects linear in kernel space for high use objects.
  415. * Please attempt to use kmap/kunmap before thinking about these interfaces.
  416. */
  417. void *dma_buf_vmap(struct dma_buf *dmabuf)
  418. {
  419. void *ptr;
  420. if (WARN_ON(!dmabuf))
  421. return NULL;
  422. if (!dmabuf->ops->vmap)
  423. return NULL;
  424. mutex_lock(&dmabuf->lock);
  425. if (dmabuf->vmapping_counter) {
  426. dmabuf->vmapping_counter++;
  427. BUG_ON(!dmabuf->vmap_ptr);
  428. ptr = dmabuf->vmap_ptr;
  429. goto out_unlock;
  430. }
  431. BUG_ON(dmabuf->vmap_ptr);
  432. ptr = dmabuf->ops->vmap(dmabuf);
  433. if (IS_ERR_OR_NULL(ptr))
  434. goto out_unlock;
  435. dmabuf->vmap_ptr = ptr;
  436. dmabuf->vmapping_counter = 1;
  437. out_unlock:
  438. mutex_unlock(&dmabuf->lock);
  439. return ptr;
  440. }
  441. EXPORT_SYMBOL_GPL(dma_buf_vmap);
  442. /**
  443. * dma_buf_vunmap - Unmap a vmap obtained by dma_buf_vmap.
  444. * @dmabuf: [in] buffer to vunmap
  445. * @vaddr: [in] vmap to vunmap
  446. */
  447. void dma_buf_vunmap(struct dma_buf *dmabuf, void *vaddr)
  448. {
  449. if (WARN_ON(!dmabuf))
  450. return;
  451. BUG_ON(!dmabuf->vmap_ptr);
  452. BUG_ON(dmabuf->vmapping_counter == 0);
  453. BUG_ON(dmabuf->vmap_ptr != vaddr);
  454. mutex_lock(&dmabuf->lock);
  455. if (--dmabuf->vmapping_counter == 0) {
  456. if (dmabuf->ops->vunmap)
  457. dmabuf->ops->vunmap(dmabuf, vaddr);
  458. dmabuf->vmap_ptr = NULL;
  459. }
  460. mutex_unlock(&dmabuf->lock);
  461. }
  462. EXPORT_SYMBOL_GPL(dma_buf_vunmap);