dma-buf.c 23 KB

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