dma-buf.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  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. #include <linux/debugfs.h>
  30. #include <linux/seq_file.h>
  31. static inline int is_dma_buf_file(struct file *);
  32. struct dma_buf_list {
  33. struct list_head head;
  34. struct mutex lock;
  35. };
  36. static struct dma_buf_list db_list;
  37. static int dma_buf_release(struct inode *inode, struct file *file)
  38. {
  39. struct dma_buf *dmabuf;
  40. if (!is_dma_buf_file(file))
  41. return -EINVAL;
  42. dmabuf = file->private_data;
  43. BUG_ON(dmabuf->vmapping_counter);
  44. dmabuf->ops->release(dmabuf);
  45. mutex_lock(&db_list.lock);
  46. list_del(&dmabuf->list_node);
  47. mutex_unlock(&db_list.lock);
  48. kfree(dmabuf);
  49. return 0;
  50. }
  51. static int dma_buf_mmap_internal(struct file *file, struct vm_area_struct *vma)
  52. {
  53. struct dma_buf *dmabuf;
  54. if (!is_dma_buf_file(file))
  55. return -EINVAL;
  56. dmabuf = file->private_data;
  57. /* check for overflowing the buffer's size */
  58. if (vma->vm_pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) >
  59. dmabuf->size >> PAGE_SHIFT)
  60. return -EINVAL;
  61. return dmabuf->ops->mmap(dmabuf, vma);
  62. }
  63. static loff_t dma_buf_llseek(struct file *file, loff_t offset, int whence)
  64. {
  65. struct dma_buf *dmabuf;
  66. loff_t base;
  67. if (!is_dma_buf_file(file))
  68. return -EBADF;
  69. dmabuf = file->private_data;
  70. /* only support discovering the end of the buffer,
  71. but also allow SEEK_SET to maintain the idiomatic
  72. SEEK_END(0), SEEK_CUR(0) pattern */
  73. if (whence == SEEK_END)
  74. base = dmabuf->size;
  75. else if (whence == SEEK_SET)
  76. base = 0;
  77. else
  78. return -EINVAL;
  79. if (offset != 0)
  80. return -EINVAL;
  81. return base + offset;
  82. }
  83. static const struct file_operations dma_buf_fops = {
  84. .release = dma_buf_release,
  85. .mmap = dma_buf_mmap_internal,
  86. .llseek = dma_buf_llseek,
  87. };
  88. /*
  89. * is_dma_buf_file - Check if struct file* is associated with dma_buf
  90. */
  91. static inline int is_dma_buf_file(struct file *file)
  92. {
  93. return file->f_op == &dma_buf_fops;
  94. }
  95. /**
  96. * dma_buf_export_named - Creates a new dma_buf, and associates an anon file
  97. * with this buffer, so it can be exported.
  98. * Also connect the allocator specific data and ops to the buffer.
  99. * Additionally, provide a name string for exporter; useful in debugging.
  100. *
  101. * @priv: [in] Attach private data of allocator to this buffer
  102. * @ops: [in] Attach allocator-defined dma buf ops to the new buffer.
  103. * @size: [in] Size of the buffer
  104. * @flags: [in] mode flags for the file.
  105. * @exp_name: [in] name of the exporting module - useful for debugging.
  106. *
  107. * Returns, on success, a newly created dma_buf object, which wraps the
  108. * supplied private data and operations for dma_buf_ops. On either missing
  109. * ops, or error in allocating struct dma_buf, will return negative error.
  110. *
  111. */
  112. struct dma_buf *dma_buf_export_named(void *priv, const struct dma_buf_ops *ops,
  113. size_t size, int flags, const char *exp_name)
  114. {
  115. struct dma_buf *dmabuf;
  116. struct file *file;
  117. if (WARN_ON(!priv || !ops
  118. || !ops->map_dma_buf
  119. || !ops->unmap_dma_buf
  120. || !ops->release
  121. || !ops->kmap_atomic
  122. || !ops->kmap
  123. || !ops->mmap)) {
  124. return ERR_PTR(-EINVAL);
  125. }
  126. dmabuf = kzalloc(sizeof(struct dma_buf), GFP_KERNEL);
  127. if (dmabuf == NULL)
  128. return ERR_PTR(-ENOMEM);
  129. dmabuf->priv = priv;
  130. dmabuf->ops = ops;
  131. dmabuf->size = size;
  132. dmabuf->exp_name = exp_name;
  133. file = anon_inode_getfile("dmabuf", &dma_buf_fops, dmabuf, flags);
  134. if (IS_ERR(file)) {
  135. kfree(dmabuf);
  136. return ERR_CAST(file);
  137. }
  138. file->f_mode |= FMODE_LSEEK;
  139. dmabuf->file = file;
  140. mutex_init(&dmabuf->lock);
  141. INIT_LIST_HEAD(&dmabuf->attachments);
  142. mutex_lock(&db_list.lock);
  143. list_add(&dmabuf->list_node, &db_list.head);
  144. mutex_unlock(&db_list.lock);
  145. return dmabuf;
  146. }
  147. EXPORT_SYMBOL_GPL(dma_buf_export_named);
  148. /**
  149. * dma_buf_fd - returns a file descriptor for the given dma_buf
  150. * @dmabuf: [in] pointer to dma_buf for which fd is required.
  151. * @flags: [in] flags to give to fd
  152. *
  153. * On success, returns an associated 'fd'. Else, returns error.
  154. */
  155. int dma_buf_fd(struct dma_buf *dmabuf, int flags)
  156. {
  157. int fd;
  158. if (!dmabuf || !dmabuf->file)
  159. return -EINVAL;
  160. fd = get_unused_fd_flags(flags);
  161. if (fd < 0)
  162. return fd;
  163. fd_install(fd, dmabuf->file);
  164. return fd;
  165. }
  166. EXPORT_SYMBOL_GPL(dma_buf_fd);
  167. /**
  168. * dma_buf_get - returns the dma_buf structure related to an fd
  169. * @fd: [in] fd associated with the dma_buf to be returned
  170. *
  171. * On success, returns the dma_buf structure associated with an fd; uses
  172. * file's refcounting done by fget to increase refcount. returns ERR_PTR
  173. * otherwise.
  174. */
  175. struct dma_buf *dma_buf_get(int fd)
  176. {
  177. struct file *file;
  178. file = fget(fd);
  179. if (!file)
  180. return ERR_PTR(-EBADF);
  181. if (!is_dma_buf_file(file)) {
  182. fput(file);
  183. return ERR_PTR(-EINVAL);
  184. }
  185. return file->private_data;
  186. }
  187. EXPORT_SYMBOL_GPL(dma_buf_get);
  188. /**
  189. * dma_buf_put - decreases refcount of the buffer
  190. * @dmabuf: [in] buffer to reduce refcount of
  191. *
  192. * Uses file's refcounting done implicitly by fput()
  193. */
  194. void dma_buf_put(struct dma_buf *dmabuf)
  195. {
  196. if (WARN_ON(!dmabuf || !dmabuf->file))
  197. return;
  198. fput(dmabuf->file);
  199. }
  200. EXPORT_SYMBOL_GPL(dma_buf_put);
  201. /**
  202. * dma_buf_attach - Add the device to dma_buf's attachments list; optionally,
  203. * calls attach() of dma_buf_ops to allow device-specific attach functionality
  204. * @dmabuf: [in] buffer to attach device to.
  205. * @dev: [in] device to be attached.
  206. *
  207. * Returns struct dma_buf_attachment * for this attachment; returns ERR_PTR on
  208. * error.
  209. */
  210. struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
  211. struct device *dev)
  212. {
  213. struct dma_buf_attachment *attach;
  214. int ret;
  215. if (WARN_ON(!dmabuf || !dev))
  216. return ERR_PTR(-EINVAL);
  217. attach = kzalloc(sizeof(struct dma_buf_attachment), GFP_KERNEL);
  218. if (attach == NULL)
  219. return ERR_PTR(-ENOMEM);
  220. attach->dev = dev;
  221. attach->dmabuf = dmabuf;
  222. mutex_lock(&dmabuf->lock);
  223. if (dmabuf->ops->attach) {
  224. ret = dmabuf->ops->attach(dmabuf, dev, attach);
  225. if (ret)
  226. goto err_attach;
  227. }
  228. list_add(&attach->node, &dmabuf->attachments);
  229. mutex_unlock(&dmabuf->lock);
  230. return attach;
  231. err_attach:
  232. kfree(attach);
  233. mutex_unlock(&dmabuf->lock);
  234. return ERR_PTR(ret);
  235. }
  236. EXPORT_SYMBOL_GPL(dma_buf_attach);
  237. /**
  238. * dma_buf_detach - Remove the given attachment from dmabuf's attachments list;
  239. * optionally calls detach() of dma_buf_ops for device-specific detach
  240. * @dmabuf: [in] buffer to detach from.
  241. * @attach: [in] attachment to be detached; is free'd after this call.
  242. *
  243. */
  244. void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
  245. {
  246. if (WARN_ON(!dmabuf || !attach))
  247. return;
  248. mutex_lock(&dmabuf->lock);
  249. list_del(&attach->node);
  250. if (dmabuf->ops->detach)
  251. dmabuf->ops->detach(dmabuf, attach);
  252. mutex_unlock(&dmabuf->lock);
  253. kfree(attach);
  254. }
  255. EXPORT_SYMBOL_GPL(dma_buf_detach);
  256. /**
  257. * dma_buf_map_attachment - Returns the scatterlist table of the attachment;
  258. * mapped into _device_ address space. Is a wrapper for map_dma_buf() of the
  259. * dma_buf_ops.
  260. * @attach: [in] attachment whose scatterlist is to be returned
  261. * @direction: [in] direction of DMA transfer
  262. *
  263. * Returns sg_table containing the scatterlist to be returned; returns ERR_PTR
  264. * on error.
  265. */
  266. struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,
  267. enum dma_data_direction direction)
  268. {
  269. struct sg_table *sg_table = ERR_PTR(-EINVAL);
  270. might_sleep();
  271. if (WARN_ON(!attach || !attach->dmabuf))
  272. return ERR_PTR(-EINVAL);
  273. sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction);
  274. if (!sg_table)
  275. sg_table = ERR_PTR(-ENOMEM);
  276. return sg_table;
  277. }
  278. EXPORT_SYMBOL_GPL(dma_buf_map_attachment);
  279. /**
  280. * dma_buf_unmap_attachment - unmaps and decreases usecount of the buffer;might
  281. * deallocate the scatterlist associated. Is a wrapper for unmap_dma_buf() of
  282. * dma_buf_ops.
  283. * @attach: [in] attachment to unmap buffer from
  284. * @sg_table: [in] scatterlist info of the buffer to unmap
  285. * @direction: [in] direction of DMA transfer
  286. *
  287. */
  288. void dma_buf_unmap_attachment(struct dma_buf_attachment *attach,
  289. struct sg_table *sg_table,
  290. enum dma_data_direction direction)
  291. {
  292. might_sleep();
  293. if (WARN_ON(!attach || !attach->dmabuf || !sg_table))
  294. return;
  295. attach->dmabuf->ops->unmap_dma_buf(attach, sg_table,
  296. direction);
  297. }
  298. EXPORT_SYMBOL_GPL(dma_buf_unmap_attachment);
  299. /**
  300. * dma_buf_begin_cpu_access - Must be called before accessing a dma_buf from the
  301. * cpu in the kernel context. Calls begin_cpu_access to allow exporter-specific
  302. * preparations. Coherency is only guaranteed in the specified range for the
  303. * specified access direction.
  304. * @dmabuf: [in] buffer to prepare cpu access for.
  305. * @start: [in] start of range for cpu access.
  306. * @len: [in] length of range for cpu access.
  307. * @direction: [in] length of range for cpu access.
  308. *
  309. * Can return negative error values, returns 0 on success.
  310. */
  311. int dma_buf_begin_cpu_access(struct dma_buf *dmabuf, size_t start, size_t len,
  312. enum dma_data_direction direction)
  313. {
  314. int ret = 0;
  315. if (WARN_ON(!dmabuf))
  316. return -EINVAL;
  317. if (dmabuf->ops->begin_cpu_access)
  318. ret = dmabuf->ops->begin_cpu_access(dmabuf, start, len, direction);
  319. return ret;
  320. }
  321. EXPORT_SYMBOL_GPL(dma_buf_begin_cpu_access);
  322. /**
  323. * dma_buf_end_cpu_access - Must be called after accessing a dma_buf from the
  324. * cpu in the kernel context. Calls end_cpu_access to allow exporter-specific
  325. * actions. Coherency is only guaranteed in the specified range for the
  326. * specified access direction.
  327. * @dmabuf: [in] buffer to complete cpu access for.
  328. * @start: [in] start of range for cpu access.
  329. * @len: [in] length of range for cpu access.
  330. * @direction: [in] length of range for cpu access.
  331. *
  332. * This call must always succeed.
  333. */
  334. void dma_buf_end_cpu_access(struct dma_buf *dmabuf, size_t start, size_t len,
  335. enum dma_data_direction direction)
  336. {
  337. WARN_ON(!dmabuf);
  338. if (dmabuf->ops->end_cpu_access)
  339. dmabuf->ops->end_cpu_access(dmabuf, start, len, direction);
  340. }
  341. EXPORT_SYMBOL_GPL(dma_buf_end_cpu_access);
  342. /**
  343. * dma_buf_kmap_atomic - Map a page of the buffer object into kernel address
  344. * space. The same restrictions as for kmap_atomic and friends apply.
  345. * @dmabuf: [in] buffer to map page from.
  346. * @page_num: [in] page in PAGE_SIZE units to map.
  347. *
  348. * This call must always succeed, any necessary preparations that might fail
  349. * need to be done in begin_cpu_access.
  350. */
  351. void *dma_buf_kmap_atomic(struct dma_buf *dmabuf, unsigned long page_num)
  352. {
  353. WARN_ON(!dmabuf);
  354. return dmabuf->ops->kmap_atomic(dmabuf, page_num);
  355. }
  356. EXPORT_SYMBOL_GPL(dma_buf_kmap_atomic);
  357. /**
  358. * dma_buf_kunmap_atomic - Unmap a page obtained by dma_buf_kmap_atomic.
  359. * @dmabuf: [in] buffer to unmap page from.
  360. * @page_num: [in] page in PAGE_SIZE units to unmap.
  361. * @vaddr: [in] kernel space pointer obtained from dma_buf_kmap_atomic.
  362. *
  363. * This call must always succeed.
  364. */
  365. void dma_buf_kunmap_atomic(struct dma_buf *dmabuf, unsigned long page_num,
  366. void *vaddr)
  367. {
  368. WARN_ON(!dmabuf);
  369. if (dmabuf->ops->kunmap_atomic)
  370. dmabuf->ops->kunmap_atomic(dmabuf, page_num, vaddr);
  371. }
  372. EXPORT_SYMBOL_GPL(dma_buf_kunmap_atomic);
  373. /**
  374. * dma_buf_kmap - Map a page of the buffer object into kernel address space. The
  375. * same restrictions as for kmap and friends apply.
  376. * @dmabuf: [in] buffer to map page from.
  377. * @page_num: [in] page in PAGE_SIZE units to map.
  378. *
  379. * This call must always succeed, any necessary preparations that might fail
  380. * need to be done in begin_cpu_access.
  381. */
  382. void *dma_buf_kmap(struct dma_buf *dmabuf, unsigned long page_num)
  383. {
  384. WARN_ON(!dmabuf);
  385. return dmabuf->ops->kmap(dmabuf, page_num);
  386. }
  387. EXPORT_SYMBOL_GPL(dma_buf_kmap);
  388. /**
  389. * dma_buf_kunmap - Unmap a page obtained by dma_buf_kmap.
  390. * @dmabuf: [in] buffer to unmap page from.
  391. * @page_num: [in] page in PAGE_SIZE units to unmap.
  392. * @vaddr: [in] kernel space pointer obtained from dma_buf_kmap.
  393. *
  394. * This call must always succeed.
  395. */
  396. void dma_buf_kunmap(struct dma_buf *dmabuf, unsigned long page_num,
  397. void *vaddr)
  398. {
  399. WARN_ON(!dmabuf);
  400. if (dmabuf->ops->kunmap)
  401. dmabuf->ops->kunmap(dmabuf, page_num, vaddr);
  402. }
  403. EXPORT_SYMBOL_GPL(dma_buf_kunmap);
  404. /**
  405. * dma_buf_mmap - Setup up a userspace mmap with the given vma
  406. * @dmabuf: [in] buffer that should back the vma
  407. * @vma: [in] vma for the mmap
  408. * @pgoff: [in] offset in pages where this mmap should start within the
  409. * dma-buf buffer.
  410. *
  411. * This function adjusts the passed in vma so that it points at the file of the
  412. * dma_buf operation. It also adjusts the starting pgoff and does bounds
  413. * checking on the size of the vma. Then it calls the exporters mmap function to
  414. * set up the mapping.
  415. *
  416. * Can return negative error values, returns 0 on success.
  417. */
  418. int dma_buf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma,
  419. unsigned long pgoff)
  420. {
  421. struct file *oldfile;
  422. int ret;
  423. if (WARN_ON(!dmabuf || !vma))
  424. return -EINVAL;
  425. /* check for offset overflow */
  426. if (pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) < pgoff)
  427. return -EOVERFLOW;
  428. /* check for overflowing the buffer's size */
  429. if (pgoff + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) >
  430. dmabuf->size >> PAGE_SHIFT)
  431. return -EINVAL;
  432. /* readjust the vma */
  433. get_file(dmabuf->file);
  434. oldfile = vma->vm_file;
  435. vma->vm_file = dmabuf->file;
  436. vma->vm_pgoff = pgoff;
  437. ret = dmabuf->ops->mmap(dmabuf, vma);
  438. if (ret) {
  439. /* restore old parameters on failure */
  440. vma->vm_file = oldfile;
  441. fput(dmabuf->file);
  442. } else {
  443. if (oldfile)
  444. fput(oldfile);
  445. }
  446. return ret;
  447. }
  448. EXPORT_SYMBOL_GPL(dma_buf_mmap);
  449. /**
  450. * dma_buf_vmap - Create virtual mapping for the buffer object into kernel
  451. * address space. Same restrictions as for vmap and friends apply.
  452. * @dmabuf: [in] buffer to vmap
  453. *
  454. * This call may fail due to lack of virtual mapping address space.
  455. * These calls are optional in drivers. The intended use for them
  456. * is for mapping objects linear in kernel space for high use objects.
  457. * Please attempt to use kmap/kunmap before thinking about these interfaces.
  458. *
  459. * Returns NULL on error.
  460. */
  461. void *dma_buf_vmap(struct dma_buf *dmabuf)
  462. {
  463. void *ptr;
  464. if (WARN_ON(!dmabuf))
  465. return NULL;
  466. if (!dmabuf->ops->vmap)
  467. return NULL;
  468. mutex_lock(&dmabuf->lock);
  469. if (dmabuf->vmapping_counter) {
  470. dmabuf->vmapping_counter++;
  471. BUG_ON(!dmabuf->vmap_ptr);
  472. ptr = dmabuf->vmap_ptr;
  473. goto out_unlock;
  474. }
  475. BUG_ON(dmabuf->vmap_ptr);
  476. ptr = dmabuf->ops->vmap(dmabuf);
  477. if (WARN_ON_ONCE(IS_ERR(ptr)))
  478. ptr = NULL;
  479. if (!ptr)
  480. goto out_unlock;
  481. dmabuf->vmap_ptr = ptr;
  482. dmabuf->vmapping_counter = 1;
  483. out_unlock:
  484. mutex_unlock(&dmabuf->lock);
  485. return ptr;
  486. }
  487. EXPORT_SYMBOL_GPL(dma_buf_vmap);
  488. /**
  489. * dma_buf_vunmap - Unmap a vmap obtained by dma_buf_vmap.
  490. * @dmabuf: [in] buffer to vunmap
  491. * @vaddr: [in] vmap to vunmap
  492. */
  493. void dma_buf_vunmap(struct dma_buf *dmabuf, void *vaddr)
  494. {
  495. if (WARN_ON(!dmabuf))
  496. return;
  497. BUG_ON(!dmabuf->vmap_ptr);
  498. BUG_ON(dmabuf->vmapping_counter == 0);
  499. BUG_ON(dmabuf->vmap_ptr != vaddr);
  500. mutex_lock(&dmabuf->lock);
  501. if (--dmabuf->vmapping_counter == 0) {
  502. if (dmabuf->ops->vunmap)
  503. dmabuf->ops->vunmap(dmabuf, vaddr);
  504. dmabuf->vmap_ptr = NULL;
  505. }
  506. mutex_unlock(&dmabuf->lock);
  507. }
  508. EXPORT_SYMBOL_GPL(dma_buf_vunmap);
  509. #ifdef CONFIG_DEBUG_FS
  510. static int dma_buf_describe(struct seq_file *s)
  511. {
  512. int ret;
  513. struct dma_buf *buf_obj;
  514. struct dma_buf_attachment *attach_obj;
  515. int count = 0, attach_count;
  516. size_t size = 0;
  517. ret = mutex_lock_interruptible(&db_list.lock);
  518. if (ret)
  519. return ret;
  520. seq_puts(s, "\nDma-buf Objects:\n");
  521. seq_puts(s, "size\tflags\tmode\tcount\texp_name\n");
  522. list_for_each_entry(buf_obj, &db_list.head, list_node) {
  523. ret = mutex_lock_interruptible(&buf_obj->lock);
  524. if (ret) {
  525. seq_puts(s,
  526. "\tERROR locking buffer object: skipping\n");
  527. continue;
  528. }
  529. seq_printf(s, "%08zu\t%08x\t%08x\t%08ld\t%s\n",
  530. buf_obj->size,
  531. buf_obj->file->f_flags, buf_obj->file->f_mode,
  532. (long)(buf_obj->file->f_count.counter),
  533. buf_obj->exp_name);
  534. seq_puts(s, "\tAttached Devices:\n");
  535. attach_count = 0;
  536. list_for_each_entry(attach_obj, &buf_obj->attachments, node) {
  537. seq_puts(s, "\t");
  538. seq_printf(s, "%s\n", dev_name(attach_obj->dev));
  539. attach_count++;
  540. }
  541. seq_printf(s, "Total %d devices attached\n\n",
  542. attach_count);
  543. count++;
  544. size += buf_obj->size;
  545. mutex_unlock(&buf_obj->lock);
  546. }
  547. seq_printf(s, "\nTotal %d objects, %zu bytes\n", count, size);
  548. mutex_unlock(&db_list.lock);
  549. return 0;
  550. }
  551. static int dma_buf_show(struct seq_file *s, void *unused)
  552. {
  553. void (*func)(struct seq_file *) = s->private;
  554. func(s);
  555. return 0;
  556. }
  557. static int dma_buf_debug_open(struct inode *inode, struct file *file)
  558. {
  559. return single_open(file, dma_buf_show, inode->i_private);
  560. }
  561. static const struct file_operations dma_buf_debug_fops = {
  562. .open = dma_buf_debug_open,
  563. .read = seq_read,
  564. .llseek = seq_lseek,
  565. .release = single_release,
  566. };
  567. static struct dentry *dma_buf_debugfs_dir;
  568. static int dma_buf_init_debugfs(void)
  569. {
  570. int err = 0;
  571. dma_buf_debugfs_dir = debugfs_create_dir("dma_buf", NULL);
  572. if (IS_ERR(dma_buf_debugfs_dir)) {
  573. err = PTR_ERR(dma_buf_debugfs_dir);
  574. dma_buf_debugfs_dir = NULL;
  575. return err;
  576. }
  577. err = dma_buf_debugfs_create_file("bufinfo", dma_buf_describe);
  578. if (err)
  579. pr_debug("dma_buf: debugfs: failed to create node bufinfo\n");
  580. return err;
  581. }
  582. static void dma_buf_uninit_debugfs(void)
  583. {
  584. if (dma_buf_debugfs_dir)
  585. debugfs_remove_recursive(dma_buf_debugfs_dir);
  586. }
  587. int dma_buf_debugfs_create_file(const char *name,
  588. int (*write)(struct seq_file *))
  589. {
  590. struct dentry *d;
  591. d = debugfs_create_file(name, S_IRUGO, dma_buf_debugfs_dir,
  592. write, &dma_buf_debug_fops);
  593. return PTR_ERR_OR_ZERO(d);
  594. }
  595. #else
  596. static inline int dma_buf_init_debugfs(void)
  597. {
  598. return 0;
  599. }
  600. static inline void dma_buf_uninit_debugfs(void)
  601. {
  602. }
  603. #endif
  604. static int __init dma_buf_init(void)
  605. {
  606. mutex_init(&db_list.lock);
  607. INIT_LIST_HEAD(&db_list.head);
  608. dma_buf_init_debugfs();
  609. return 0;
  610. }
  611. subsys_initcall(dma_buf_init);
  612. static void __exit dma_buf_deinit(void)
  613. {
  614. dma_buf_uninit_debugfs();
  615. }
  616. __exitcall(dma_buf_deinit);