dma-buf.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  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. #ifdef CONFIG_COMPAT
  254. .compat_ioctl = dma_buf_ioctl,
  255. #endif
  256. };
  257. /*
  258. * is_dma_buf_file - Check if struct file* is associated with dma_buf
  259. */
  260. static inline int is_dma_buf_file(struct file *file)
  261. {
  262. return file->f_op == &dma_buf_fops;
  263. }
  264. /**
  265. * dma_buf_export - Creates a new dma_buf, and associates an anon file
  266. * with this buffer, so it can be exported.
  267. * Also connect the allocator specific data and ops to the buffer.
  268. * Additionally, provide a name string for exporter; useful in debugging.
  269. *
  270. * @exp_info: [in] holds all the export related information provided
  271. * by the exporter. see struct dma_buf_export_info
  272. * for further details.
  273. *
  274. * Returns, on success, a newly created dma_buf object, which wraps the
  275. * supplied private data and operations for dma_buf_ops. On either missing
  276. * ops, or error in allocating struct dma_buf, will return negative error.
  277. *
  278. */
  279. struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info)
  280. {
  281. struct dma_buf *dmabuf;
  282. struct reservation_object *resv = exp_info->resv;
  283. struct file *file;
  284. size_t alloc_size = sizeof(struct dma_buf);
  285. int ret;
  286. if (!exp_info->resv)
  287. alloc_size += sizeof(struct reservation_object);
  288. else
  289. /* prevent &dma_buf[1] == dma_buf->resv */
  290. alloc_size += 1;
  291. if (WARN_ON(!exp_info->priv
  292. || !exp_info->ops
  293. || !exp_info->ops->map_dma_buf
  294. || !exp_info->ops->unmap_dma_buf
  295. || !exp_info->ops->release
  296. || !exp_info->ops->kmap_atomic
  297. || !exp_info->ops->kmap
  298. || !exp_info->ops->mmap)) {
  299. return ERR_PTR(-EINVAL);
  300. }
  301. if (!try_module_get(exp_info->owner))
  302. return ERR_PTR(-ENOENT);
  303. dmabuf = kzalloc(alloc_size, GFP_KERNEL);
  304. if (!dmabuf) {
  305. ret = -ENOMEM;
  306. goto err_module;
  307. }
  308. dmabuf->priv = exp_info->priv;
  309. dmabuf->ops = exp_info->ops;
  310. dmabuf->size = exp_info->size;
  311. dmabuf->exp_name = exp_info->exp_name;
  312. dmabuf->owner = exp_info->owner;
  313. init_waitqueue_head(&dmabuf->poll);
  314. dmabuf->cb_excl.poll = dmabuf->cb_shared.poll = &dmabuf->poll;
  315. dmabuf->cb_excl.active = dmabuf->cb_shared.active = 0;
  316. if (!resv) {
  317. resv = (struct reservation_object *)&dmabuf[1];
  318. reservation_object_init(resv);
  319. }
  320. dmabuf->resv = resv;
  321. file = anon_inode_getfile("dmabuf", &dma_buf_fops, dmabuf,
  322. exp_info->flags);
  323. if (IS_ERR(file)) {
  324. ret = PTR_ERR(file);
  325. goto err_dmabuf;
  326. }
  327. file->f_mode |= FMODE_LSEEK;
  328. dmabuf->file = file;
  329. mutex_init(&dmabuf->lock);
  330. INIT_LIST_HEAD(&dmabuf->attachments);
  331. mutex_lock(&db_list.lock);
  332. list_add(&dmabuf->list_node, &db_list.head);
  333. mutex_unlock(&db_list.lock);
  334. return dmabuf;
  335. err_dmabuf:
  336. kfree(dmabuf);
  337. err_module:
  338. module_put(exp_info->owner);
  339. return ERR_PTR(ret);
  340. }
  341. EXPORT_SYMBOL_GPL(dma_buf_export);
  342. /**
  343. * dma_buf_fd - returns a file descriptor for the given dma_buf
  344. * @dmabuf: [in] pointer to dma_buf for which fd is required.
  345. * @flags: [in] flags to give to fd
  346. *
  347. * On success, returns an associated 'fd'. Else, returns error.
  348. */
  349. int dma_buf_fd(struct dma_buf *dmabuf, int flags)
  350. {
  351. int fd;
  352. if (!dmabuf || !dmabuf->file)
  353. return -EINVAL;
  354. fd = get_unused_fd_flags(flags);
  355. if (fd < 0)
  356. return fd;
  357. fd_install(fd, dmabuf->file);
  358. return fd;
  359. }
  360. EXPORT_SYMBOL_GPL(dma_buf_fd);
  361. /**
  362. * dma_buf_get - returns the dma_buf structure related to an fd
  363. * @fd: [in] fd associated with the dma_buf to be returned
  364. *
  365. * On success, returns the dma_buf structure associated with an fd; uses
  366. * file's refcounting done by fget to increase refcount. returns ERR_PTR
  367. * otherwise.
  368. */
  369. struct dma_buf *dma_buf_get(int fd)
  370. {
  371. struct file *file;
  372. file = fget(fd);
  373. if (!file)
  374. return ERR_PTR(-EBADF);
  375. if (!is_dma_buf_file(file)) {
  376. fput(file);
  377. return ERR_PTR(-EINVAL);
  378. }
  379. return file->private_data;
  380. }
  381. EXPORT_SYMBOL_GPL(dma_buf_get);
  382. /**
  383. * dma_buf_put - decreases refcount of the buffer
  384. * @dmabuf: [in] buffer to reduce refcount of
  385. *
  386. * Uses file's refcounting done implicitly by fput()
  387. */
  388. void dma_buf_put(struct dma_buf *dmabuf)
  389. {
  390. if (WARN_ON(!dmabuf || !dmabuf->file))
  391. return;
  392. fput(dmabuf->file);
  393. }
  394. EXPORT_SYMBOL_GPL(dma_buf_put);
  395. /**
  396. * dma_buf_attach - Add the device to dma_buf's attachments list; optionally,
  397. * calls attach() of dma_buf_ops to allow device-specific attach functionality
  398. * @dmabuf: [in] buffer to attach device to.
  399. * @dev: [in] device to be attached.
  400. *
  401. * Returns struct dma_buf_attachment * for this attachment; returns ERR_PTR on
  402. * error.
  403. */
  404. struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
  405. struct device *dev)
  406. {
  407. struct dma_buf_attachment *attach;
  408. int ret;
  409. if (WARN_ON(!dmabuf || !dev))
  410. return ERR_PTR(-EINVAL);
  411. attach = kzalloc(sizeof(struct dma_buf_attachment), GFP_KERNEL);
  412. if (attach == NULL)
  413. return ERR_PTR(-ENOMEM);
  414. attach->dev = dev;
  415. attach->dmabuf = dmabuf;
  416. mutex_lock(&dmabuf->lock);
  417. if (dmabuf->ops->attach) {
  418. ret = dmabuf->ops->attach(dmabuf, dev, attach);
  419. if (ret)
  420. goto err_attach;
  421. }
  422. list_add(&attach->node, &dmabuf->attachments);
  423. mutex_unlock(&dmabuf->lock);
  424. return attach;
  425. err_attach:
  426. kfree(attach);
  427. mutex_unlock(&dmabuf->lock);
  428. return ERR_PTR(ret);
  429. }
  430. EXPORT_SYMBOL_GPL(dma_buf_attach);
  431. /**
  432. * dma_buf_detach - Remove the given attachment from dmabuf's attachments list;
  433. * optionally calls detach() of dma_buf_ops for device-specific detach
  434. * @dmabuf: [in] buffer to detach from.
  435. * @attach: [in] attachment to be detached; is free'd after this call.
  436. *
  437. */
  438. void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
  439. {
  440. if (WARN_ON(!dmabuf || !attach))
  441. return;
  442. mutex_lock(&dmabuf->lock);
  443. list_del(&attach->node);
  444. if (dmabuf->ops->detach)
  445. dmabuf->ops->detach(dmabuf, attach);
  446. mutex_unlock(&dmabuf->lock);
  447. kfree(attach);
  448. }
  449. EXPORT_SYMBOL_GPL(dma_buf_detach);
  450. /**
  451. * dma_buf_map_attachment - Returns the scatterlist table of the attachment;
  452. * mapped into _device_ address space. Is a wrapper for map_dma_buf() of the
  453. * dma_buf_ops.
  454. * @attach: [in] attachment whose scatterlist is to be returned
  455. * @direction: [in] direction of DMA transfer
  456. *
  457. * Returns sg_table containing the scatterlist to be returned; returns ERR_PTR
  458. * on error.
  459. */
  460. struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach,
  461. enum dma_data_direction direction)
  462. {
  463. struct sg_table *sg_table;
  464. might_sleep();
  465. if (WARN_ON(!attach || !attach->dmabuf))
  466. return ERR_PTR(-EINVAL);
  467. sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction);
  468. if (!sg_table)
  469. sg_table = ERR_PTR(-ENOMEM);
  470. return sg_table;
  471. }
  472. EXPORT_SYMBOL_GPL(dma_buf_map_attachment);
  473. /**
  474. * dma_buf_unmap_attachment - unmaps and decreases usecount of the buffer;might
  475. * deallocate the scatterlist associated. Is a wrapper for unmap_dma_buf() of
  476. * dma_buf_ops.
  477. * @attach: [in] attachment to unmap buffer from
  478. * @sg_table: [in] scatterlist info of the buffer to unmap
  479. * @direction: [in] direction of DMA transfer
  480. *
  481. */
  482. void dma_buf_unmap_attachment(struct dma_buf_attachment *attach,
  483. struct sg_table *sg_table,
  484. enum dma_data_direction direction)
  485. {
  486. might_sleep();
  487. if (WARN_ON(!attach || !attach->dmabuf || !sg_table))
  488. return;
  489. attach->dmabuf->ops->unmap_dma_buf(attach, sg_table,
  490. direction);
  491. }
  492. EXPORT_SYMBOL_GPL(dma_buf_unmap_attachment);
  493. static int __dma_buf_begin_cpu_access(struct dma_buf *dmabuf,
  494. enum dma_data_direction direction)
  495. {
  496. bool write = (direction == DMA_BIDIRECTIONAL ||
  497. direction == DMA_TO_DEVICE);
  498. struct reservation_object *resv = dmabuf->resv;
  499. long ret;
  500. /* Wait on any implicit rendering fences */
  501. ret = reservation_object_wait_timeout_rcu(resv, write, true,
  502. MAX_SCHEDULE_TIMEOUT);
  503. if (ret < 0)
  504. return ret;
  505. return 0;
  506. }
  507. /**
  508. * dma_buf_begin_cpu_access - Must be called before accessing a dma_buf from the
  509. * cpu in the kernel context. Calls begin_cpu_access to allow exporter-specific
  510. * preparations. Coherency is only guaranteed in the specified range for the
  511. * specified access direction.
  512. * @dmabuf: [in] buffer to prepare cpu access for.
  513. * @direction: [in] length of range for cpu access.
  514. *
  515. * Can return negative error values, returns 0 on success.
  516. */
  517. int dma_buf_begin_cpu_access(struct dma_buf *dmabuf,
  518. enum dma_data_direction direction)
  519. {
  520. int ret = 0;
  521. if (WARN_ON(!dmabuf))
  522. return -EINVAL;
  523. if (dmabuf->ops->begin_cpu_access)
  524. ret = dmabuf->ops->begin_cpu_access(dmabuf, direction);
  525. /* Ensure that all fences are waited upon - but we first allow
  526. * the native handler the chance to do so more efficiently if it
  527. * chooses. A double invocation here will be reasonably cheap no-op.
  528. */
  529. if (ret == 0)
  530. ret = __dma_buf_begin_cpu_access(dmabuf, direction);
  531. return ret;
  532. }
  533. EXPORT_SYMBOL_GPL(dma_buf_begin_cpu_access);
  534. /**
  535. * dma_buf_end_cpu_access - Must be called after accessing a dma_buf from the
  536. * cpu in the kernel context. Calls end_cpu_access to allow exporter-specific
  537. * actions. Coherency is only guaranteed in the specified range for the
  538. * specified access direction.
  539. * @dmabuf: [in] buffer to complete cpu access for.
  540. * @direction: [in] length of range for cpu access.
  541. *
  542. * Can return negative error values, returns 0 on success.
  543. */
  544. int dma_buf_end_cpu_access(struct dma_buf *dmabuf,
  545. enum dma_data_direction direction)
  546. {
  547. int ret = 0;
  548. WARN_ON(!dmabuf);
  549. if (dmabuf->ops->end_cpu_access)
  550. ret = dmabuf->ops->end_cpu_access(dmabuf, direction);
  551. return ret;
  552. }
  553. EXPORT_SYMBOL_GPL(dma_buf_end_cpu_access);
  554. /**
  555. * dma_buf_kmap_atomic - Map a page of the buffer object into kernel address
  556. * space. The same restrictions as for kmap_atomic and friends apply.
  557. * @dmabuf: [in] buffer to map page from.
  558. * @page_num: [in] page in PAGE_SIZE units to map.
  559. *
  560. * This call must always succeed, any necessary preparations that might fail
  561. * need to be done in begin_cpu_access.
  562. */
  563. void *dma_buf_kmap_atomic(struct dma_buf *dmabuf, unsigned long page_num)
  564. {
  565. WARN_ON(!dmabuf);
  566. return dmabuf->ops->kmap_atomic(dmabuf, page_num);
  567. }
  568. EXPORT_SYMBOL_GPL(dma_buf_kmap_atomic);
  569. /**
  570. * dma_buf_kunmap_atomic - Unmap a page obtained by dma_buf_kmap_atomic.
  571. * @dmabuf: [in] buffer to unmap page from.
  572. * @page_num: [in] page in PAGE_SIZE units to unmap.
  573. * @vaddr: [in] kernel space pointer obtained from dma_buf_kmap_atomic.
  574. *
  575. * This call must always succeed.
  576. */
  577. void dma_buf_kunmap_atomic(struct dma_buf *dmabuf, unsigned long page_num,
  578. void *vaddr)
  579. {
  580. WARN_ON(!dmabuf);
  581. if (dmabuf->ops->kunmap_atomic)
  582. dmabuf->ops->kunmap_atomic(dmabuf, page_num, vaddr);
  583. }
  584. EXPORT_SYMBOL_GPL(dma_buf_kunmap_atomic);
  585. /**
  586. * dma_buf_kmap - Map a page of the buffer object into kernel address space. The
  587. * same restrictions as for kmap and friends apply.
  588. * @dmabuf: [in] buffer to map page from.
  589. * @page_num: [in] page in PAGE_SIZE units to map.
  590. *
  591. * This call must always succeed, any necessary preparations that might fail
  592. * need to be done in begin_cpu_access.
  593. */
  594. void *dma_buf_kmap(struct dma_buf *dmabuf, unsigned long page_num)
  595. {
  596. WARN_ON(!dmabuf);
  597. return dmabuf->ops->kmap(dmabuf, page_num);
  598. }
  599. EXPORT_SYMBOL_GPL(dma_buf_kmap);
  600. /**
  601. * dma_buf_kunmap - Unmap a page obtained by dma_buf_kmap.
  602. * @dmabuf: [in] buffer to unmap page from.
  603. * @page_num: [in] page in PAGE_SIZE units to unmap.
  604. * @vaddr: [in] kernel space pointer obtained from dma_buf_kmap.
  605. *
  606. * This call must always succeed.
  607. */
  608. void dma_buf_kunmap(struct dma_buf *dmabuf, unsigned long page_num,
  609. void *vaddr)
  610. {
  611. WARN_ON(!dmabuf);
  612. if (dmabuf->ops->kunmap)
  613. dmabuf->ops->kunmap(dmabuf, page_num, vaddr);
  614. }
  615. EXPORT_SYMBOL_GPL(dma_buf_kunmap);
  616. /**
  617. * dma_buf_mmap - Setup up a userspace mmap with the given vma
  618. * @dmabuf: [in] buffer that should back the vma
  619. * @vma: [in] vma for the mmap
  620. * @pgoff: [in] offset in pages where this mmap should start within the
  621. * dma-buf buffer.
  622. *
  623. * This function adjusts the passed in vma so that it points at the file of the
  624. * dma_buf operation. It also adjusts the starting pgoff and does bounds
  625. * checking on the size of the vma. Then it calls the exporters mmap function to
  626. * set up the mapping.
  627. *
  628. * Can return negative error values, returns 0 on success.
  629. */
  630. int dma_buf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma,
  631. unsigned long pgoff)
  632. {
  633. struct file *oldfile;
  634. int ret;
  635. if (WARN_ON(!dmabuf || !vma))
  636. return -EINVAL;
  637. /* check for offset overflow */
  638. if (pgoff + vma_pages(vma) < pgoff)
  639. return -EOVERFLOW;
  640. /* check for overflowing the buffer's size */
  641. if (pgoff + vma_pages(vma) >
  642. dmabuf->size >> PAGE_SHIFT)
  643. return -EINVAL;
  644. /* readjust the vma */
  645. get_file(dmabuf->file);
  646. oldfile = vma->vm_file;
  647. vma->vm_file = dmabuf->file;
  648. vma->vm_pgoff = pgoff;
  649. ret = dmabuf->ops->mmap(dmabuf, vma);
  650. if (ret) {
  651. /* restore old parameters on failure */
  652. vma->vm_file = oldfile;
  653. fput(dmabuf->file);
  654. } else {
  655. if (oldfile)
  656. fput(oldfile);
  657. }
  658. return ret;
  659. }
  660. EXPORT_SYMBOL_GPL(dma_buf_mmap);
  661. /**
  662. * dma_buf_vmap - Create virtual mapping for the buffer object into kernel
  663. * address space. Same restrictions as for vmap and friends apply.
  664. * @dmabuf: [in] buffer to vmap
  665. *
  666. * This call may fail due to lack of virtual mapping address space.
  667. * These calls are optional in drivers. The intended use for them
  668. * is for mapping objects linear in kernel space for high use objects.
  669. * Please attempt to use kmap/kunmap before thinking about these interfaces.
  670. *
  671. * Returns NULL on error.
  672. */
  673. void *dma_buf_vmap(struct dma_buf *dmabuf)
  674. {
  675. void *ptr;
  676. if (WARN_ON(!dmabuf))
  677. return NULL;
  678. if (!dmabuf->ops->vmap)
  679. return NULL;
  680. mutex_lock(&dmabuf->lock);
  681. if (dmabuf->vmapping_counter) {
  682. dmabuf->vmapping_counter++;
  683. BUG_ON(!dmabuf->vmap_ptr);
  684. ptr = dmabuf->vmap_ptr;
  685. goto out_unlock;
  686. }
  687. BUG_ON(dmabuf->vmap_ptr);
  688. ptr = dmabuf->ops->vmap(dmabuf);
  689. if (WARN_ON_ONCE(IS_ERR(ptr)))
  690. ptr = NULL;
  691. if (!ptr)
  692. goto out_unlock;
  693. dmabuf->vmap_ptr = ptr;
  694. dmabuf->vmapping_counter = 1;
  695. out_unlock:
  696. mutex_unlock(&dmabuf->lock);
  697. return ptr;
  698. }
  699. EXPORT_SYMBOL_GPL(dma_buf_vmap);
  700. /**
  701. * dma_buf_vunmap - Unmap a vmap obtained by dma_buf_vmap.
  702. * @dmabuf: [in] buffer to vunmap
  703. * @vaddr: [in] vmap to vunmap
  704. */
  705. void dma_buf_vunmap(struct dma_buf *dmabuf, void *vaddr)
  706. {
  707. if (WARN_ON(!dmabuf))
  708. return;
  709. BUG_ON(!dmabuf->vmap_ptr);
  710. BUG_ON(dmabuf->vmapping_counter == 0);
  711. BUG_ON(dmabuf->vmap_ptr != vaddr);
  712. mutex_lock(&dmabuf->lock);
  713. if (--dmabuf->vmapping_counter == 0) {
  714. if (dmabuf->ops->vunmap)
  715. dmabuf->ops->vunmap(dmabuf, vaddr);
  716. dmabuf->vmap_ptr = NULL;
  717. }
  718. mutex_unlock(&dmabuf->lock);
  719. }
  720. EXPORT_SYMBOL_GPL(dma_buf_vunmap);
  721. #ifdef CONFIG_DEBUG_FS
  722. static int dma_buf_debug_show(struct seq_file *s, void *unused)
  723. {
  724. int ret;
  725. struct dma_buf *buf_obj;
  726. struct dma_buf_attachment *attach_obj;
  727. int count = 0, attach_count;
  728. size_t size = 0;
  729. ret = mutex_lock_interruptible(&db_list.lock);
  730. if (ret)
  731. return ret;
  732. seq_puts(s, "\nDma-buf Objects:\n");
  733. seq_puts(s, "size\tflags\tmode\tcount\texp_name\n");
  734. list_for_each_entry(buf_obj, &db_list.head, list_node) {
  735. ret = mutex_lock_interruptible(&buf_obj->lock);
  736. if (ret) {
  737. seq_puts(s,
  738. "\tERROR locking buffer object: skipping\n");
  739. continue;
  740. }
  741. seq_printf(s, "%08zu\t%08x\t%08x\t%08ld\t%s\n",
  742. buf_obj->size,
  743. buf_obj->file->f_flags, buf_obj->file->f_mode,
  744. file_count(buf_obj->file),
  745. buf_obj->exp_name);
  746. seq_puts(s, "\tAttached Devices:\n");
  747. attach_count = 0;
  748. list_for_each_entry(attach_obj, &buf_obj->attachments, node) {
  749. seq_puts(s, "\t");
  750. seq_printf(s, "%s\n", dev_name(attach_obj->dev));
  751. attach_count++;
  752. }
  753. seq_printf(s, "Total %d devices attached\n\n",
  754. attach_count);
  755. count++;
  756. size += buf_obj->size;
  757. mutex_unlock(&buf_obj->lock);
  758. }
  759. seq_printf(s, "\nTotal %d objects, %zu bytes\n", count, size);
  760. mutex_unlock(&db_list.lock);
  761. return 0;
  762. }
  763. static int dma_buf_debug_open(struct inode *inode, struct file *file)
  764. {
  765. return single_open(file, dma_buf_debug_show, NULL);
  766. }
  767. static const struct file_operations dma_buf_debug_fops = {
  768. .open = dma_buf_debug_open,
  769. .read = seq_read,
  770. .llseek = seq_lseek,
  771. .release = single_release,
  772. };
  773. static struct dentry *dma_buf_debugfs_dir;
  774. static int dma_buf_init_debugfs(void)
  775. {
  776. struct dentry *d;
  777. int err = 0;
  778. d = debugfs_create_dir("dma_buf", NULL);
  779. if (IS_ERR(d))
  780. return PTR_ERR(d);
  781. dma_buf_debugfs_dir = d;
  782. d = debugfs_create_file("bufinfo", S_IRUGO, dma_buf_debugfs_dir,
  783. NULL, &dma_buf_debug_fops);
  784. if (IS_ERR(d)) {
  785. pr_debug("dma_buf: debugfs: failed to create node bufinfo\n");
  786. debugfs_remove_recursive(dma_buf_debugfs_dir);
  787. dma_buf_debugfs_dir = NULL;
  788. err = PTR_ERR(d);
  789. }
  790. return err;
  791. }
  792. static void dma_buf_uninit_debugfs(void)
  793. {
  794. if (dma_buf_debugfs_dir)
  795. debugfs_remove_recursive(dma_buf_debugfs_dir);
  796. }
  797. #else
  798. static inline int dma_buf_init_debugfs(void)
  799. {
  800. return 0;
  801. }
  802. static inline void dma_buf_uninit_debugfs(void)
  803. {
  804. }
  805. #endif
  806. static int __init dma_buf_init(void)
  807. {
  808. mutex_init(&db_list.lock);
  809. INIT_LIST_HEAD(&db_list.head);
  810. dma_buf_init_debugfs();
  811. return 0;
  812. }
  813. subsys_initcall(dma_buf_init);
  814. static void __exit dma_buf_deinit(void)
  815. {
  816. dma_buf_uninit_debugfs();
  817. }
  818. __exitcall(dma_buf_deinit);