dma-buf.c 23 KB

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