dma-buf.c 22 KB

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