dma-buf.c 27 KB

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