dma-buf.c 23 KB

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