dma-buf.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  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; may return negative
  208. * error codes.
  209. *
  210. */
  211. struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
  212. struct device *dev)
  213. {
  214. struct dma_buf_attachment *attach;
  215. int ret;
  216. if (WARN_ON(!dmabuf || !dev))
  217. return ERR_PTR(-EINVAL);
  218. attach = kzalloc(sizeof(struct dma_buf_attachment), GFP_KERNEL);
  219. if (attach == NULL)
  220. return ERR_PTR(-ENOMEM);
  221. attach->dev = dev;
  222. attach->dmabuf = dmabuf;
  223. mutex_lock(&dmabuf->lock);
  224. if (dmabuf->ops->attach) {
  225. ret = dmabuf->ops->attach(dmabuf, dev, attach);
  226. if (ret)
  227. goto err_attach;
  228. }
  229. list_add(&attach->node, &dmabuf->attachments);
  230. mutex_unlock(&dmabuf->lock);
  231. return attach;
  232. err_attach:
  233. kfree(attach);
  234. mutex_unlock(&dmabuf->lock);
  235. return ERR_PTR(ret);
  236. }
  237. EXPORT_SYMBOL_GPL(dma_buf_attach);
  238. /**
  239. * dma_buf_detach - Remove the given attachment from dmabuf's attachments list;
  240. * optionally calls detach() of dma_buf_ops for device-specific detach
  241. * @dmabuf: [in] buffer to detach from.
  242. * @attach: [in] attachment to be detached; is free'd after this call.
  243. *
  244. */
  245. void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
  246. {
  247. if (WARN_ON(!dmabuf || !attach))
  248. return;
  249. mutex_lock(&dmabuf->lock);
  250. list_del(&attach->node);
  251. if (dmabuf->ops->detach)
  252. dmabuf->ops->detach(dmabuf, attach);
  253. mutex_unlock(&dmabuf->lock);
  254. kfree(attach);
  255. }
  256. EXPORT_SYMBOL_GPL(dma_buf_detach);
  257. /**
  258. * dma_buf_map_attachment - Returns the scatterlist table of the attachment;
  259. * mapped into _device_ address space. Is a wrapper for map_dma_buf() of the
  260. * dma_buf_ops.
  261. * @attach: [in] attachment whose scatterlist is to be returned
  262. * @direction: [in] direction of DMA transfer
  263. *
  264. * Returns sg_table containing the scatterlist to be returned; may return NULL
  265. * or ERR_PTR.
  266. *
  267. */
  268. struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,
  269. enum dma_data_direction direction)
  270. {
  271. struct sg_table *sg_table = ERR_PTR(-EINVAL);
  272. might_sleep();
  273. if (WARN_ON(!attach || !attach->dmabuf))
  274. return ERR_PTR(-EINVAL);
  275. sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction);
  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 alsog 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. void *dma_buf_vmap(struct dma_buf *dmabuf)
  460. {
  461. void *ptr;
  462. if (WARN_ON(!dmabuf))
  463. return NULL;
  464. if (!dmabuf->ops->vmap)
  465. return NULL;
  466. mutex_lock(&dmabuf->lock);
  467. if (dmabuf->vmapping_counter) {
  468. dmabuf->vmapping_counter++;
  469. BUG_ON(!dmabuf->vmap_ptr);
  470. ptr = dmabuf->vmap_ptr;
  471. goto out_unlock;
  472. }
  473. BUG_ON(dmabuf->vmap_ptr);
  474. ptr = dmabuf->ops->vmap(dmabuf);
  475. if (IS_ERR_OR_NULL(ptr))
  476. goto out_unlock;
  477. dmabuf->vmap_ptr = ptr;
  478. dmabuf->vmapping_counter = 1;
  479. out_unlock:
  480. mutex_unlock(&dmabuf->lock);
  481. return ptr;
  482. }
  483. EXPORT_SYMBOL_GPL(dma_buf_vmap);
  484. /**
  485. * dma_buf_vunmap - Unmap a vmap obtained by dma_buf_vmap.
  486. * @dmabuf: [in] buffer to vunmap
  487. * @vaddr: [in] vmap to vunmap
  488. */
  489. void dma_buf_vunmap(struct dma_buf *dmabuf, void *vaddr)
  490. {
  491. if (WARN_ON(!dmabuf))
  492. return;
  493. BUG_ON(!dmabuf->vmap_ptr);
  494. BUG_ON(dmabuf->vmapping_counter == 0);
  495. BUG_ON(dmabuf->vmap_ptr != vaddr);
  496. mutex_lock(&dmabuf->lock);
  497. if (--dmabuf->vmapping_counter == 0) {
  498. if (dmabuf->ops->vunmap)
  499. dmabuf->ops->vunmap(dmabuf, vaddr);
  500. dmabuf->vmap_ptr = NULL;
  501. }
  502. mutex_unlock(&dmabuf->lock);
  503. }
  504. EXPORT_SYMBOL_GPL(dma_buf_vunmap);
  505. #ifdef CONFIG_DEBUG_FS
  506. static int dma_buf_describe(struct seq_file *s)
  507. {
  508. int ret;
  509. struct dma_buf *buf_obj;
  510. struct dma_buf_attachment *attach_obj;
  511. int count = 0, attach_count;
  512. size_t size = 0;
  513. ret = mutex_lock_interruptible(&db_list.lock);
  514. if (ret)
  515. return ret;
  516. seq_printf(s, "\nDma-buf Objects:\n");
  517. seq_printf(s, "\texp_name\tsize\tflags\tmode\tcount\n");
  518. list_for_each_entry(buf_obj, &db_list.head, list_node) {
  519. ret = mutex_lock_interruptible(&buf_obj->lock);
  520. if (ret) {
  521. seq_printf(s,
  522. "\tERROR locking buffer object: skipping\n");
  523. continue;
  524. }
  525. seq_printf(s, "\t");
  526. seq_printf(s, "\t%s\t%08zu\t%08x\t%08x\t%08ld\n",
  527. buf_obj->exp_name, buf_obj->size,
  528. buf_obj->file->f_flags, buf_obj->file->f_mode,
  529. (long)(buf_obj->file->f_count.counter));
  530. seq_printf(s, "\t\tAttached Devices:\n");
  531. attach_count = 0;
  532. list_for_each_entry(attach_obj, &buf_obj->attachments, node) {
  533. seq_printf(s, "\t\t");
  534. seq_printf(s, "%s\n", attach_obj->dev->init_name);
  535. attach_count++;
  536. }
  537. seq_printf(s, "\n\t\tTotal %d devices attached\n",
  538. attach_count);
  539. count++;
  540. size += buf_obj->size;
  541. mutex_unlock(&buf_obj->lock);
  542. }
  543. seq_printf(s, "\nTotal %d objects, %zu bytes\n", count, size);
  544. mutex_unlock(&db_list.lock);
  545. return 0;
  546. }
  547. static int dma_buf_show(struct seq_file *s, void *unused)
  548. {
  549. void (*func)(struct seq_file *) = s->private;
  550. func(s);
  551. return 0;
  552. }
  553. static int dma_buf_debug_open(struct inode *inode, struct file *file)
  554. {
  555. return single_open(file, dma_buf_show, inode->i_private);
  556. }
  557. static const struct file_operations dma_buf_debug_fops = {
  558. .open = dma_buf_debug_open,
  559. .read = seq_read,
  560. .llseek = seq_lseek,
  561. .release = single_release,
  562. };
  563. static struct dentry *dma_buf_debugfs_dir;
  564. static int dma_buf_init_debugfs(void)
  565. {
  566. int err = 0;
  567. dma_buf_debugfs_dir = debugfs_create_dir("dma_buf", NULL);
  568. if (IS_ERR(dma_buf_debugfs_dir)) {
  569. err = PTR_ERR(dma_buf_debugfs_dir);
  570. dma_buf_debugfs_dir = NULL;
  571. return err;
  572. }
  573. err = dma_buf_debugfs_create_file("bufinfo", dma_buf_describe);
  574. if (err)
  575. pr_debug("dma_buf: debugfs: failed to create node bufinfo\n");
  576. return err;
  577. }
  578. static void dma_buf_uninit_debugfs(void)
  579. {
  580. if (dma_buf_debugfs_dir)
  581. debugfs_remove_recursive(dma_buf_debugfs_dir);
  582. }
  583. int dma_buf_debugfs_create_file(const char *name,
  584. int (*write)(struct seq_file *))
  585. {
  586. struct dentry *d;
  587. d = debugfs_create_file(name, S_IRUGO, dma_buf_debugfs_dir,
  588. write, &dma_buf_debug_fops);
  589. return PTR_ERR_OR_ZERO(d);
  590. }
  591. #else
  592. static inline int dma_buf_init_debugfs(void)
  593. {
  594. return 0;
  595. }
  596. static inline void dma_buf_uninit_debugfs(void)
  597. {
  598. }
  599. #endif
  600. static int __init dma_buf_init(void)
  601. {
  602. mutex_init(&db_list.lock);
  603. INIT_LIST_HEAD(&db_list.head);
  604. dma_buf_init_debugfs();
  605. return 0;
  606. }
  607. subsys_initcall(dma_buf_init);
  608. static void __exit dma_buf_deinit(void)
  609. {
  610. dma_buf_uninit_debugfs();
  611. }
  612. __exitcall(dma_buf_deinit);