dma-buf.c 23 KB

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