Thomas Gleixner | caab277 | 2019-06-03 07:44:50 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 2 | /* |
| 3 | * Framework for buffer objects that can be shared across devices/subsystems. |
| 4 | * |
| 5 | * Copyright(C) 2011 Linaro Limited. All rights reserved. |
| 6 | * Author: Sumit Semwal <sumit.semwal@ti.com> |
| 7 | * |
| 8 | * Many thanks to linaro-mm-sig list, and specially |
| 9 | * Arnd Bergmann <arnd@arndb.de>, Rob Clark <rob@ti.com> and |
| 10 | * Daniel Vetter <daniel@ffwll.ch> for their support in creation and |
| 11 | * refining of this idea. |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include <linux/fs.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/dma-buf.h> |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 17 | #include <linux/dma-fence.h> |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 18 | #include <linux/anon_inodes.h> |
| 19 | #include <linux/export.h> |
Sumit Semwal | b89e3563 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 20 | #include <linux/debugfs.h> |
Sumit Semwal | 9abdffe | 2015-05-05 14:56:15 +0530 | [diff] [blame] | 21 | #include <linux/module.h> |
Sumit Semwal | b89e3563 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 22 | #include <linux/seq_file.h> |
Maarten Lankhorst | 9b495a5 | 2014-07-01 12:57:43 +0200 | [diff] [blame] | 23 | #include <linux/poll.h> |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 24 | #include <linux/dma-resv.h> |
Muhammad Falak R Wani | b02da6f | 2016-05-23 17:08:42 +0530 | [diff] [blame] | 25 | #include <linux/mm.h> |
Greg Hackmann | ed63bb1 | 2019-06-13 15:34:06 -0700 | [diff] [blame] | 26 | #include <linux/mount.h> |
Linus Torvalds | 933a90b | 2019-07-19 10:42:02 -0700 | [diff] [blame] | 27 | #include <linux/pseudo_fs.h> |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 28 | |
Daniel Vetter | c11e391 | 2016-02-11 20:04:51 -0200 | [diff] [blame] | 29 | #include <uapi/linux/dma-buf.h> |
Greg Hackmann | ed63bb1 | 2019-06-13 15:34:06 -0700 | [diff] [blame] | 30 | #include <uapi/linux/magic.h> |
Daniel Vetter | c11e391 | 2016-02-11 20:04:51 -0200 | [diff] [blame] | 31 | |
Hridya Valsaraju | bdb8d06 | 2021-06-03 14:47:51 -0700 | [diff] [blame] | 32 | #include "dma-buf-sysfs-stats.h" |
| 33 | |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 34 | static inline int is_dma_buf_file(struct file *); |
| 35 | |
Sumit Semwal | b89e3563 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 36 | struct dma_buf_list { |
| 37 | struct list_head head; |
| 38 | struct mutex lock; |
| 39 | }; |
| 40 | |
| 41 | static struct dma_buf_list db_list; |
| 42 | |
Greg Hackmann | bb2bb90 | 2019-06-13 15:34:07 -0700 | [diff] [blame] | 43 | static char *dmabuffs_dname(struct dentry *dentry, char *buffer, int buflen) |
| 44 | { |
| 45 | struct dma_buf *dmabuf; |
| 46 | char name[DMA_BUF_NAME_LEN]; |
| 47 | size_t ret = 0; |
| 48 | |
| 49 | dmabuf = dentry->d_fsdata; |
Charan Teja Kalla | 6348dd2 | 2020-06-19 17:27:19 +0530 | [diff] [blame] | 50 | spin_lock(&dmabuf->name_lock); |
Greg Hackmann | bb2bb90 | 2019-06-13 15:34:07 -0700 | [diff] [blame] | 51 | if (dmabuf->name) |
| 52 | ret = strlcpy(name, dmabuf->name, DMA_BUF_NAME_LEN); |
Charan Teja Kalla | 6348dd2 | 2020-06-19 17:27:19 +0530 | [diff] [blame] | 53 | spin_unlock(&dmabuf->name_lock); |
Greg Hackmann | bb2bb90 | 2019-06-13 15:34:07 -0700 | [diff] [blame] | 54 | |
| 55 | return dynamic_dname(dentry, buffer, buflen, "/%s:%s", |
| 56 | dentry->d_name.name, ret > 0 ? name : ""); |
| 57 | } |
| 58 | |
Sumit Semwal | 4ab59c3 | 2020-06-11 17:14:18 +0530 | [diff] [blame] | 59 | static void dma_buf_release(struct dentry *dentry) |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 60 | { |
| 61 | struct dma_buf *dmabuf; |
| 62 | |
Sumit Semwal | 4ab59c3 | 2020-06-11 17:14:18 +0530 | [diff] [blame] | 63 | dmabuf = dentry->d_fsdata; |
Charan Teja Reddy | 19a508b | 2020-09-18 16:02:31 +0530 | [diff] [blame] | 64 | if (unlikely(!dmabuf)) |
| 65 | return; |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 66 | |
Daniel Vetter | f00b4da | 2012-12-20 14:14:23 +0100 | [diff] [blame] | 67 | BUG_ON(dmabuf->vmapping_counter); |
| 68 | |
Maarten Lankhorst | 9b495a5 | 2014-07-01 12:57:43 +0200 | [diff] [blame] | 69 | /* |
| 70 | * Any fences that a dma-buf poll can wait on should be signaled |
| 71 | * before releasing dma-buf. This is the responsibility of each |
| 72 | * driver that uses the reservation objects. |
| 73 | * |
| 74 | * If you hit this BUG() it means someone dropped their ref to the |
| 75 | * dma-buf while still having pending operation to the buffer. |
| 76 | */ |
| 77 | BUG_ON(dmabuf->cb_shared.active || dmabuf->cb_excl.active); |
| 78 | |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 79 | dmabuf->ops->release(dmabuf); |
Sumit Semwal | b89e3563 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 80 | |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 81 | if (dmabuf->resv == (struct dma_resv *)&dmabuf[1]) |
| 82 | dma_resv_fini(dmabuf->resv); |
Maarten Lankhorst | 3aac450 | 2014-07-01 12:57:26 +0200 | [diff] [blame] | 83 | |
Hridya Valsaraju | bdb8d06 | 2021-06-03 14:47:51 -0700 | [diff] [blame] | 84 | dma_buf_stats_teardown(dmabuf); |
Sumit Semwal | 9abdffe | 2015-05-05 14:56:15 +0530 | [diff] [blame] | 85 | module_put(dmabuf->owner); |
Cong Wang | d1f3722 | 2019-12-26 22:32:04 -0800 | [diff] [blame] | 86 | kfree(dmabuf->name); |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 87 | kfree(dmabuf); |
Sumit Semwal | 4ab59c3 | 2020-06-11 17:14:18 +0530 | [diff] [blame] | 88 | } |
| 89 | |
Charan Teja Reddy | 05cd846 | 2021-01-05 20:06:39 +0530 | [diff] [blame] | 90 | static int dma_buf_file_release(struct inode *inode, struct file *file) |
| 91 | { |
| 92 | struct dma_buf *dmabuf; |
| 93 | |
| 94 | if (!is_dma_buf_file(file)) |
| 95 | return -EINVAL; |
| 96 | |
| 97 | dmabuf = file->private_data; |
| 98 | |
| 99 | mutex_lock(&db_list.lock); |
| 100 | list_del(&dmabuf->list_node); |
| 101 | mutex_unlock(&db_list.lock); |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
Sumit Semwal | 4ab59c3 | 2020-06-11 17:14:18 +0530 | [diff] [blame] | 106 | static const struct dentry_operations dma_buf_dentry_ops = { |
| 107 | .d_dname = dmabuffs_dname, |
| 108 | .d_release = dma_buf_release, |
| 109 | }; |
| 110 | |
| 111 | static struct vfsmount *dma_buf_mnt; |
| 112 | |
| 113 | static int dma_buf_fs_init_context(struct fs_context *fc) |
| 114 | { |
| 115 | struct pseudo_fs_context *ctx; |
| 116 | |
| 117 | ctx = init_pseudo(fc, DMA_BUF_MAGIC); |
| 118 | if (!ctx) |
| 119 | return -ENOMEM; |
| 120 | ctx->dops = &dma_buf_dentry_ops; |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 121 | return 0; |
| 122 | } |
| 123 | |
Sumit Semwal | 4ab59c3 | 2020-06-11 17:14:18 +0530 | [diff] [blame] | 124 | static struct file_system_type dma_buf_fs_type = { |
| 125 | .name = "dmabuf", |
| 126 | .init_fs_context = dma_buf_fs_init_context, |
| 127 | .kill_sb = kill_anon_super, |
| 128 | }; |
| 129 | |
Daniel Vetter | 4c78513 | 2012-04-24 14:38:52 +0530 | [diff] [blame] | 130 | static int dma_buf_mmap_internal(struct file *file, struct vm_area_struct *vma) |
| 131 | { |
| 132 | struct dma_buf *dmabuf; |
| 133 | |
| 134 | if (!is_dma_buf_file(file)) |
| 135 | return -EINVAL; |
| 136 | |
| 137 | dmabuf = file->private_data; |
| 138 | |
Andrew F. Davis | e3a9d6c | 2019-03-29 11:52:01 -0500 | [diff] [blame] | 139 | /* check if buffer supports mmap */ |
| 140 | if (!dmabuf->ops->mmap) |
| 141 | return -EINVAL; |
| 142 | |
Daniel Vetter | 4c78513 | 2012-04-24 14:38:52 +0530 | [diff] [blame] | 143 | /* check for overflowing the buffer's size */ |
Muhammad Falak R Wani | b02da6f | 2016-05-23 17:08:42 +0530 | [diff] [blame] | 144 | if (vma->vm_pgoff + vma_pages(vma) > |
Daniel Vetter | 4c78513 | 2012-04-24 14:38:52 +0530 | [diff] [blame] | 145 | dmabuf->size >> PAGE_SHIFT) |
| 146 | return -EINVAL; |
| 147 | |
| 148 | return dmabuf->ops->mmap(dmabuf, vma); |
| 149 | } |
| 150 | |
Christopher James Halse Rogers | 19e8697 | 2013-09-10 11:36:45 +0530 | [diff] [blame] | 151 | static loff_t dma_buf_llseek(struct file *file, loff_t offset, int whence) |
| 152 | { |
| 153 | struct dma_buf *dmabuf; |
| 154 | loff_t base; |
| 155 | |
| 156 | if (!is_dma_buf_file(file)) |
| 157 | return -EBADF; |
| 158 | |
| 159 | dmabuf = file->private_data; |
| 160 | |
| 161 | /* only support discovering the end of the buffer, |
| 162 | but also allow SEEK_SET to maintain the idiomatic |
| 163 | SEEK_END(0), SEEK_CUR(0) pattern */ |
| 164 | if (whence == SEEK_END) |
| 165 | base = dmabuf->size; |
| 166 | else if (whence == SEEK_SET) |
| 167 | base = 0; |
| 168 | else |
| 169 | return -EINVAL; |
| 170 | |
| 171 | if (offset != 0) |
| 172 | return -EINVAL; |
| 173 | |
| 174 | return base + offset; |
| 175 | } |
| 176 | |
Daniel Vetter | e7e21c7 | 2016-12-09 22:50:55 +0100 | [diff] [blame] | 177 | /** |
Daniel Vetter | 102514e | 2020-06-12 09:05:35 +0200 | [diff] [blame] | 178 | * DOC: implicit fence polling |
Daniel Vetter | e7e21c7 | 2016-12-09 22:50:55 +0100 | [diff] [blame] | 179 | * |
| 180 | * To support cross-device and cross-driver synchronization of buffer access |
Daniel Vetter | 102514e | 2020-06-12 09:05:35 +0200 | [diff] [blame] | 181 | * implicit fences (represented internally in the kernel with &struct dma_fence) |
| 182 | * can be attached to a &dma_buf. The glue for that and a few related things are |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 183 | * provided in the &dma_resv structure. |
Daniel Vetter | e7e21c7 | 2016-12-09 22:50:55 +0100 | [diff] [blame] | 184 | * |
| 185 | * Userspace can query the state of these implicitly tracked fences using poll() |
| 186 | * and related system calls: |
| 187 | * |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 188 | * - Checking for EPOLLIN, i.e. read access, can be use to query the state of the |
Daniel Vetter | e7e21c7 | 2016-12-09 22:50:55 +0100 | [diff] [blame] | 189 | * most recent write or exclusive fence. |
| 190 | * |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 191 | * - Checking for EPOLLOUT, i.e. write access, can be used to query the state of |
Daniel Vetter | e7e21c7 | 2016-12-09 22:50:55 +0100 | [diff] [blame] | 192 | * all attached fences, shared and exclusive ones. |
| 193 | * |
| 194 | * Note that this only signals the completion of the respective fences, i.e. the |
| 195 | * DMA transfers are complete. Cache flushing and any other necessary |
| 196 | * preparations before CPU access can begin still need to happen. |
| 197 | */ |
| 198 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 199 | static void dma_buf_poll_cb(struct dma_fence *fence, struct dma_fence_cb *cb) |
Maarten Lankhorst | 9b495a5 | 2014-07-01 12:57:43 +0200 | [diff] [blame] | 200 | { |
| 201 | struct dma_buf_poll_cb_t *dcb = (struct dma_buf_poll_cb_t *)cb; |
| 202 | unsigned long flags; |
| 203 | |
| 204 | spin_lock_irqsave(&dcb->poll->lock, flags); |
| 205 | wake_up_locked_poll(dcb->poll, dcb->active); |
| 206 | dcb->active = 0; |
| 207 | spin_unlock_irqrestore(&dcb->poll->lock, flags); |
| 208 | } |
| 209 | |
Al Viro | afc9a42 | 2017-07-03 06:39:46 -0400 | [diff] [blame] | 210 | static __poll_t dma_buf_poll(struct file *file, poll_table *poll) |
Maarten Lankhorst | 9b495a5 | 2014-07-01 12:57:43 +0200 | [diff] [blame] | 211 | { |
| 212 | struct dma_buf *dmabuf; |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 213 | struct dma_resv *resv; |
| 214 | struct dma_resv_list *fobj; |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 215 | struct dma_fence *fence_excl; |
Al Viro | 0169943 | 2017-07-03 03:14:15 -0400 | [diff] [blame] | 216 | __poll_t events; |
Chris Wilson | b016cd6 | 2019-08-14 19:24:01 +0100 | [diff] [blame] | 217 | unsigned shared_count, seq; |
Maarten Lankhorst | 9b495a5 | 2014-07-01 12:57:43 +0200 | [diff] [blame] | 218 | |
| 219 | dmabuf = file->private_data; |
| 220 | if (!dmabuf || !dmabuf->resv) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 221 | return EPOLLERR; |
Maarten Lankhorst | 9b495a5 | 2014-07-01 12:57:43 +0200 | [diff] [blame] | 222 | |
| 223 | resv = dmabuf->resv; |
| 224 | |
| 225 | poll_wait(file, &dmabuf->poll, poll); |
| 226 | |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 227 | events = poll_requested_events(poll) & (EPOLLIN | EPOLLOUT); |
Maarten Lankhorst | 9b495a5 | 2014-07-01 12:57:43 +0200 | [diff] [blame] | 228 | if (!events) |
| 229 | return 0; |
| 230 | |
Chris Wilson | b016cd6 | 2019-08-14 19:24:01 +0100 | [diff] [blame] | 231 | retry: |
| 232 | seq = read_seqcount_begin(&resv->seq); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 233 | rcu_read_lock(); |
Chris Wilson | b016cd6 | 2019-08-14 19:24:01 +0100 | [diff] [blame] | 234 | |
| 235 | fobj = rcu_dereference(resv->fence); |
| 236 | if (fobj) |
| 237 | shared_count = fobj->shared_count; |
| 238 | else |
| 239 | shared_count = 0; |
Christian König | 6edbd6a | 2021-05-10 16:14:09 +0200 | [diff] [blame] | 240 | fence_excl = dma_resv_excl_fence(resv); |
Chris Wilson | b016cd6 | 2019-08-14 19:24:01 +0100 | [diff] [blame] | 241 | if (read_seqcount_retry(&resv->seq, seq)) { |
| 242 | rcu_read_unlock(); |
| 243 | goto retry; |
| 244 | } |
| 245 | |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 246 | if (fence_excl && (!(events & EPOLLOUT) || shared_count == 0)) { |
Maarten Lankhorst | 9b495a5 | 2014-07-01 12:57:43 +0200 | [diff] [blame] | 247 | struct dma_buf_poll_cb_t *dcb = &dmabuf->cb_excl; |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 248 | __poll_t pevents = EPOLLIN; |
Maarten Lankhorst | 9b495a5 | 2014-07-01 12:57:43 +0200 | [diff] [blame] | 249 | |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 250 | if (shared_count == 0) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 251 | pevents |= EPOLLOUT; |
Maarten Lankhorst | 9b495a5 | 2014-07-01 12:57:43 +0200 | [diff] [blame] | 252 | |
| 253 | spin_lock_irq(&dmabuf->poll.lock); |
| 254 | if (dcb->active) { |
| 255 | dcb->active |= pevents; |
| 256 | events &= ~pevents; |
| 257 | } else |
| 258 | dcb->active = pevents; |
| 259 | spin_unlock_irq(&dmabuf->poll.lock); |
| 260 | |
| 261 | if (events & pevents) { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 262 | if (!dma_fence_get_rcu(fence_excl)) { |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 263 | /* force a recheck */ |
| 264 | events &= ~pevents; |
| 265 | dma_buf_poll_cb(NULL, &dcb->cb); |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 266 | } else if (!dma_fence_add_callback(fence_excl, &dcb->cb, |
| 267 | dma_buf_poll_cb)) { |
Maarten Lankhorst | 9b495a5 | 2014-07-01 12:57:43 +0200 | [diff] [blame] | 268 | events &= ~pevents; |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 269 | dma_fence_put(fence_excl); |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 270 | } else { |
Maarten Lankhorst | 9b495a5 | 2014-07-01 12:57:43 +0200 | [diff] [blame] | 271 | /* |
| 272 | * No callback queued, wake up any additional |
| 273 | * waiters. |
| 274 | */ |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 275 | dma_fence_put(fence_excl); |
Maarten Lankhorst | 9b495a5 | 2014-07-01 12:57:43 +0200 | [diff] [blame] | 276 | dma_buf_poll_cb(NULL, &dcb->cb); |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 277 | } |
Maarten Lankhorst | 9b495a5 | 2014-07-01 12:57:43 +0200 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 281 | if ((events & EPOLLOUT) && shared_count > 0) { |
Maarten Lankhorst | 9b495a5 | 2014-07-01 12:57:43 +0200 | [diff] [blame] | 282 | struct dma_buf_poll_cb_t *dcb = &dmabuf->cb_shared; |
| 283 | int i; |
| 284 | |
| 285 | /* Only queue a new callback if no event has fired yet */ |
| 286 | spin_lock_irq(&dmabuf->poll.lock); |
| 287 | if (dcb->active) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 288 | events &= ~EPOLLOUT; |
Maarten Lankhorst | 9b495a5 | 2014-07-01 12:57:43 +0200 | [diff] [blame] | 289 | else |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 290 | dcb->active = EPOLLOUT; |
Maarten Lankhorst | 9b495a5 | 2014-07-01 12:57:43 +0200 | [diff] [blame] | 291 | spin_unlock_irq(&dmabuf->poll.lock); |
| 292 | |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 293 | if (!(events & EPOLLOUT)) |
Maarten Lankhorst | 9b495a5 | 2014-07-01 12:57:43 +0200 | [diff] [blame] | 294 | goto out; |
| 295 | |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 296 | for (i = 0; i < shared_count; ++i) { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 297 | struct dma_fence *fence = rcu_dereference(fobj->shared[i]); |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 298 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 299 | if (!dma_fence_get_rcu(fence)) { |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 300 | /* |
| 301 | * fence refcount dropped to zero, this means |
| 302 | * that fobj has been freed |
| 303 | * |
| 304 | * call dma_buf_poll_cb and force a recheck! |
| 305 | */ |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 306 | events &= ~EPOLLOUT; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 307 | dma_buf_poll_cb(NULL, &dcb->cb); |
| 308 | break; |
| 309 | } |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 310 | if (!dma_fence_add_callback(fence, &dcb->cb, |
| 311 | dma_buf_poll_cb)) { |
| 312 | dma_fence_put(fence); |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 313 | events &= ~EPOLLOUT; |
Maarten Lankhorst | 9b495a5 | 2014-07-01 12:57:43 +0200 | [diff] [blame] | 314 | break; |
| 315 | } |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 316 | dma_fence_put(fence); |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 317 | } |
Maarten Lankhorst | 9b495a5 | 2014-07-01 12:57:43 +0200 | [diff] [blame] | 318 | |
| 319 | /* No callback queued, wake up any additional waiters. */ |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 320 | if (i == shared_count) |
Maarten Lankhorst | 9b495a5 | 2014-07-01 12:57:43 +0200 | [diff] [blame] | 321 | dma_buf_poll_cb(NULL, &dcb->cb); |
| 322 | } |
| 323 | |
| 324 | out: |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 325 | rcu_read_unlock(); |
Maarten Lankhorst | 9b495a5 | 2014-07-01 12:57:43 +0200 | [diff] [blame] | 326 | return events; |
| 327 | } |
| 328 | |
Greg Hackmann | bb2bb90 | 2019-06-13 15:34:07 -0700 | [diff] [blame] | 329 | /** |
| 330 | * dma_buf_set_name - Set a name to a specific dma_buf to track the usage. |
| 331 | * The name of the dma-buf buffer can only be set when the dma-buf is not |
| 332 | * attached to any devices. It could theoritically support changing the |
| 333 | * name of the dma-buf if the same piece of memory is used for multiple |
| 334 | * purpose between different devices. |
| 335 | * |
Krzysztof Kozlowski | 6d3ba80 | 2020-08-19 19:51:33 +0200 | [diff] [blame] | 336 | * @dmabuf: [in] dmabuf buffer that will be renamed. |
| 337 | * @buf: [in] A piece of userspace memory that contains the name of |
| 338 | * the dma-buf. |
Greg Hackmann | bb2bb90 | 2019-06-13 15:34:07 -0700 | [diff] [blame] | 339 | * |
| 340 | * Returns 0 on success. If the dma-buf buffer is already attached to |
| 341 | * devices, return -EBUSY. |
| 342 | * |
| 343 | */ |
| 344 | static long dma_buf_set_name(struct dma_buf *dmabuf, const char __user *buf) |
| 345 | { |
| 346 | char *name = strndup_user(buf, DMA_BUF_NAME_LEN); |
| 347 | long ret = 0; |
| 348 | |
| 349 | if (IS_ERR(name)) |
| 350 | return PTR_ERR(name); |
| 351 | |
Christian König | 15fd552 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 352 | dma_resv_lock(dmabuf->resv, NULL); |
Greg Hackmann | bb2bb90 | 2019-06-13 15:34:07 -0700 | [diff] [blame] | 353 | if (!list_empty(&dmabuf->attachments)) { |
| 354 | ret = -EBUSY; |
| 355 | kfree(name); |
| 356 | goto out_unlock; |
| 357 | } |
Charan Teja Kalla | 6348dd2 | 2020-06-19 17:27:19 +0530 | [diff] [blame] | 358 | spin_lock(&dmabuf->name_lock); |
Greg Hackmann | bb2bb90 | 2019-06-13 15:34:07 -0700 | [diff] [blame] | 359 | kfree(dmabuf->name); |
| 360 | dmabuf->name = name; |
Charan Teja Kalla | 6348dd2 | 2020-06-19 17:27:19 +0530 | [diff] [blame] | 361 | spin_unlock(&dmabuf->name_lock); |
Greg Hackmann | bb2bb90 | 2019-06-13 15:34:07 -0700 | [diff] [blame] | 362 | |
| 363 | out_unlock: |
Christian König | 15fd552 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 364 | dma_resv_unlock(dmabuf->resv); |
Greg Hackmann | bb2bb90 | 2019-06-13 15:34:07 -0700 | [diff] [blame] | 365 | return ret; |
| 366 | } |
| 367 | |
Daniel Vetter | c11e391 | 2016-02-11 20:04:51 -0200 | [diff] [blame] | 368 | static long dma_buf_ioctl(struct file *file, |
| 369 | unsigned int cmd, unsigned long arg) |
| 370 | { |
| 371 | struct dma_buf *dmabuf; |
| 372 | struct dma_buf_sync sync; |
| 373 | enum dma_data_direction direction; |
Chris Wilson | 18b862d | 2016-03-18 20:02:39 +0000 | [diff] [blame] | 374 | int ret; |
Daniel Vetter | c11e391 | 2016-02-11 20:04:51 -0200 | [diff] [blame] | 375 | |
| 376 | dmabuf = file->private_data; |
| 377 | |
| 378 | switch (cmd) { |
| 379 | case DMA_BUF_IOCTL_SYNC: |
| 380 | if (copy_from_user(&sync, (void __user *) arg, sizeof(sync))) |
| 381 | return -EFAULT; |
| 382 | |
| 383 | if (sync.flags & ~DMA_BUF_SYNC_VALID_FLAGS_MASK) |
| 384 | return -EINVAL; |
| 385 | |
| 386 | switch (sync.flags & DMA_BUF_SYNC_RW) { |
| 387 | case DMA_BUF_SYNC_READ: |
| 388 | direction = DMA_FROM_DEVICE; |
| 389 | break; |
| 390 | case DMA_BUF_SYNC_WRITE: |
| 391 | direction = DMA_TO_DEVICE; |
| 392 | break; |
| 393 | case DMA_BUF_SYNC_RW: |
| 394 | direction = DMA_BIDIRECTIONAL; |
| 395 | break; |
| 396 | default: |
| 397 | return -EINVAL; |
| 398 | } |
| 399 | |
| 400 | if (sync.flags & DMA_BUF_SYNC_END) |
Chris Wilson | 18b862d | 2016-03-18 20:02:39 +0000 | [diff] [blame] | 401 | ret = dma_buf_end_cpu_access(dmabuf, direction); |
Daniel Vetter | c11e391 | 2016-02-11 20:04:51 -0200 | [diff] [blame] | 402 | else |
Chris Wilson | 18b862d | 2016-03-18 20:02:39 +0000 | [diff] [blame] | 403 | ret = dma_buf_begin_cpu_access(dmabuf, direction); |
Daniel Vetter | c11e391 | 2016-02-11 20:04:51 -0200 | [diff] [blame] | 404 | |
Chris Wilson | 18b862d | 2016-03-18 20:02:39 +0000 | [diff] [blame] | 405 | return ret; |
Greg Hackmann | bb2bb90 | 2019-06-13 15:34:07 -0700 | [diff] [blame] | 406 | |
Daniel Vetter | a5bff92 | 2020-04-07 15:30:02 +0200 | [diff] [blame] | 407 | case DMA_BUF_SET_NAME_A: |
| 408 | case DMA_BUF_SET_NAME_B: |
Greg Hackmann | bb2bb90 | 2019-06-13 15:34:07 -0700 | [diff] [blame] | 409 | return dma_buf_set_name(dmabuf, (const char __user *)arg); |
| 410 | |
Daniel Vetter | c11e391 | 2016-02-11 20:04:51 -0200 | [diff] [blame] | 411 | default: |
| 412 | return -ENOTTY; |
| 413 | } |
| 414 | } |
| 415 | |
Greg Hackmann | bcc0711 | 2019-06-13 15:34:08 -0700 | [diff] [blame] | 416 | static void dma_buf_show_fdinfo(struct seq_file *m, struct file *file) |
| 417 | { |
| 418 | struct dma_buf *dmabuf = file->private_data; |
| 419 | |
| 420 | seq_printf(m, "size:\t%zu\n", dmabuf->size); |
| 421 | /* Don't count the temporary reference taken inside procfs seq_show */ |
| 422 | seq_printf(m, "count:\t%ld\n", file_count(dmabuf->file) - 1); |
| 423 | seq_printf(m, "exp_name:\t%s\n", dmabuf->exp_name); |
Charan Teja Kalla | 6348dd2 | 2020-06-19 17:27:19 +0530 | [diff] [blame] | 424 | spin_lock(&dmabuf->name_lock); |
Greg Hackmann | bcc0711 | 2019-06-13 15:34:08 -0700 | [diff] [blame] | 425 | if (dmabuf->name) |
| 426 | seq_printf(m, "name:\t%s\n", dmabuf->name); |
Charan Teja Kalla | 6348dd2 | 2020-06-19 17:27:19 +0530 | [diff] [blame] | 427 | spin_unlock(&dmabuf->name_lock); |
Greg Hackmann | bcc0711 | 2019-06-13 15:34:08 -0700 | [diff] [blame] | 428 | } |
| 429 | |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 430 | static const struct file_operations dma_buf_fops = { |
Charan Teja Reddy | 05cd846 | 2021-01-05 20:06:39 +0530 | [diff] [blame] | 431 | .release = dma_buf_file_release, |
Daniel Vetter | 4c78513 | 2012-04-24 14:38:52 +0530 | [diff] [blame] | 432 | .mmap = dma_buf_mmap_internal, |
Christopher James Halse Rogers | 19e8697 | 2013-09-10 11:36:45 +0530 | [diff] [blame] | 433 | .llseek = dma_buf_llseek, |
Maarten Lankhorst | 9b495a5 | 2014-07-01 12:57:43 +0200 | [diff] [blame] | 434 | .poll = dma_buf_poll, |
Daniel Vetter | c11e391 | 2016-02-11 20:04:51 -0200 | [diff] [blame] | 435 | .unlocked_ioctl = dma_buf_ioctl, |
Arnd Bergmann | 1832f2d | 2018-09-11 21:59:08 +0200 | [diff] [blame] | 436 | .compat_ioctl = compat_ptr_ioctl, |
Greg Hackmann | bcc0711 | 2019-06-13 15:34:08 -0700 | [diff] [blame] | 437 | .show_fdinfo = dma_buf_show_fdinfo, |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 438 | }; |
| 439 | |
| 440 | /* |
| 441 | * is_dma_buf_file - Check if struct file* is associated with dma_buf |
| 442 | */ |
| 443 | static inline int is_dma_buf_file(struct file *file) |
| 444 | { |
| 445 | return file->f_op == &dma_buf_fops; |
| 446 | } |
| 447 | |
Greg Hackmann | ed63bb1 | 2019-06-13 15:34:06 -0700 | [diff] [blame] | 448 | static struct file *dma_buf_getfile(struct dma_buf *dmabuf, int flags) |
| 449 | { |
| 450 | struct file *file; |
| 451 | struct inode *inode = alloc_anon_inode(dma_buf_mnt->mnt_sb); |
| 452 | |
| 453 | if (IS_ERR(inode)) |
| 454 | return ERR_CAST(inode); |
| 455 | |
| 456 | inode->i_size = dmabuf->size; |
| 457 | inode_set_bytes(inode, dmabuf->size); |
| 458 | |
| 459 | file = alloc_file_pseudo(inode, dma_buf_mnt, "dmabuf", |
| 460 | flags, &dma_buf_fops); |
| 461 | if (IS_ERR(file)) |
| 462 | goto err_alloc_file; |
| 463 | file->f_flags = flags & (O_ACCMODE | O_NONBLOCK); |
| 464 | file->private_data = dmabuf; |
Greg Hackmann | bb2bb90 | 2019-06-13 15:34:07 -0700 | [diff] [blame] | 465 | file->f_path.dentry->d_fsdata = dmabuf; |
Greg Hackmann | ed63bb1 | 2019-06-13 15:34:06 -0700 | [diff] [blame] | 466 | |
| 467 | return file; |
| 468 | |
| 469 | err_alloc_file: |
| 470 | iput(inode); |
| 471 | return file; |
| 472 | } |
| 473 | |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 474 | /** |
Daniel Vetter | 2904a8c | 2016-12-09 19:53:07 +0100 | [diff] [blame] | 475 | * DOC: dma buf device access |
| 476 | * |
| 477 | * For device DMA access to a shared DMA buffer the usual sequence of operations |
| 478 | * is fairly simple: |
| 479 | * |
| 480 | * 1. The exporter defines his exporter instance using |
| 481 | * DEFINE_DMA_BUF_EXPORT_INFO() and calls dma_buf_export() to wrap a private |
| 482 | * buffer object into a &dma_buf. It then exports that &dma_buf to userspace |
| 483 | * as a file descriptor by calling dma_buf_fd(). |
| 484 | * |
| 485 | * 2. Userspace passes this file-descriptors to all drivers it wants this buffer |
| 486 | * to share with: First the filedescriptor is converted to a &dma_buf using |
Liviu Dudau | c138782 | 2017-11-01 14:06:30 +0000 | [diff] [blame] | 487 | * dma_buf_get(). Then the buffer is attached to the device using |
Daniel Vetter | 2904a8c | 2016-12-09 19:53:07 +0100 | [diff] [blame] | 488 | * dma_buf_attach(). |
| 489 | * |
| 490 | * Up to this stage the exporter is still free to migrate or reallocate the |
| 491 | * backing storage. |
| 492 | * |
Liviu Dudau | c138782 | 2017-11-01 14:06:30 +0000 | [diff] [blame] | 493 | * 3. Once the buffer is attached to all devices userspace can initiate DMA |
Daniel Vetter | 2904a8c | 2016-12-09 19:53:07 +0100 | [diff] [blame] | 494 | * access to the shared buffer. In the kernel this is done by calling |
| 495 | * dma_buf_map_attachment() and dma_buf_unmap_attachment(). |
| 496 | * |
| 497 | * 4. Once a driver is done with a shared buffer it needs to call |
| 498 | * dma_buf_detach() (after cleaning up any mappings) and then release the |
Daniel Vetter | 85804b7 | 2020-12-11 16:58:41 +0100 | [diff] [blame] | 499 | * reference acquired with dma_buf_get() by calling dma_buf_put(). |
Daniel Vetter | 2904a8c | 2016-12-09 19:53:07 +0100 | [diff] [blame] | 500 | * |
| 501 | * For the detailed semantics exporters are expected to implement see |
| 502 | * &dma_buf_ops. |
| 503 | */ |
| 504 | |
| 505 | /** |
Sumit Semwal | d8fbe34 | 2015-01-23 12:53:43 +0530 | [diff] [blame] | 506 | * dma_buf_export - Creates a new dma_buf, and associates an anon file |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 507 | * with this buffer, so it can be exported. |
| 508 | * Also connect the allocator specific data and ops to the buffer. |
Sumit Semwal | 78df969 | 2013-03-22 18:22:16 +0530 | [diff] [blame] | 509 | * Additionally, provide a name string for exporter; useful in debugging. |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 510 | * |
Sumit Semwal | d8fbe34 | 2015-01-23 12:53:43 +0530 | [diff] [blame] | 511 | * @exp_info: [in] holds all the export related information provided |
Daniel Vetter | f641d3b | 2016-12-29 21:48:24 +0100 | [diff] [blame] | 512 | * by the exporter. see &struct dma_buf_export_info |
Sumit Semwal | d8fbe34 | 2015-01-23 12:53:43 +0530 | [diff] [blame] | 513 | * for further details. |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 514 | * |
Daniel Vetter | 85804b7 | 2020-12-11 16:58:41 +0100 | [diff] [blame] | 515 | * Returns, on success, a newly created struct dma_buf object, which wraps the |
| 516 | * supplied private data and operations for struct dma_buf_ops. On either |
| 517 | * missing ops, or error in allocating struct dma_buf, will return negative |
| 518 | * error. |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 519 | * |
Daniel Vetter | 2904a8c | 2016-12-09 19:53:07 +0100 | [diff] [blame] | 520 | * For most cases the easiest way to create @exp_info is through the |
| 521 | * %DEFINE_DMA_BUF_EXPORT_INFO macro. |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 522 | */ |
Sumit Semwal | d8fbe34 | 2015-01-23 12:53:43 +0530 | [diff] [blame] | 523 | struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info) |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 524 | { |
| 525 | struct dma_buf *dmabuf; |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 526 | struct dma_resv *resv = exp_info->resv; |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 527 | struct file *file; |
Maarten Lankhorst | 3aac450 | 2014-07-01 12:57:26 +0200 | [diff] [blame] | 528 | size_t alloc_size = sizeof(struct dma_buf); |
Chris Wilson | a026df4 | 2016-07-18 12:16:22 +0100 | [diff] [blame] | 529 | int ret; |
Jagan Teki | 5136629 | 2015-05-21 01:09:31 +0530 | [diff] [blame] | 530 | |
Sumit Semwal | d8fbe34 | 2015-01-23 12:53:43 +0530 | [diff] [blame] | 531 | if (!exp_info->resv) |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 532 | alloc_size += sizeof(struct dma_resv); |
Maarten Lankhorst | 3aac450 | 2014-07-01 12:57:26 +0200 | [diff] [blame] | 533 | else |
| 534 | /* prevent &dma_buf[1] == dma_buf->resv */ |
| 535 | alloc_size += 1; |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 536 | |
Sumit Semwal | d8fbe34 | 2015-01-23 12:53:43 +0530 | [diff] [blame] | 537 | if (WARN_ON(!exp_info->priv |
| 538 | || !exp_info->ops |
| 539 | || !exp_info->ops->map_dma_buf |
| 540 | || !exp_info->ops->unmap_dma_buf |
Andrew F. Davis | e3a9d6c | 2019-03-29 11:52:01 -0500 | [diff] [blame] | 541 | || !exp_info->ops->release)) { |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 542 | return ERR_PTR(-EINVAL); |
| 543 | } |
| 544 | |
Christian König | 15fd552 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 545 | if (WARN_ON(exp_info->ops->cache_sgt_mapping && |
Christian König | bd2275e | 2020-02-18 16:57:24 +0100 | [diff] [blame] | 546 | (exp_info->ops->pin || exp_info->ops->unpin))) |
Christian König | 15fd552 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 547 | return ERR_PTR(-EINVAL); |
| 548 | |
Christian König | bd2275e | 2020-02-18 16:57:24 +0100 | [diff] [blame] | 549 | if (WARN_ON(!exp_info->ops->pin != !exp_info->ops->unpin)) |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 550 | return ERR_PTR(-EINVAL); |
| 551 | |
Sumit Semwal | 9abdffe | 2015-05-05 14:56:15 +0530 | [diff] [blame] | 552 | if (!try_module_get(exp_info->owner)) |
| 553 | return ERR_PTR(-ENOENT); |
| 554 | |
Maarten Lankhorst | 3aac450 | 2014-07-01 12:57:26 +0200 | [diff] [blame] | 555 | dmabuf = kzalloc(alloc_size, GFP_KERNEL); |
Sumit Semwal | 9abdffe | 2015-05-05 14:56:15 +0530 | [diff] [blame] | 556 | if (!dmabuf) { |
Chris Wilson | a026df4 | 2016-07-18 12:16:22 +0100 | [diff] [blame] | 557 | ret = -ENOMEM; |
| 558 | goto err_module; |
Sumit Semwal | 9abdffe | 2015-05-05 14:56:15 +0530 | [diff] [blame] | 559 | } |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 560 | |
Sumit Semwal | d8fbe34 | 2015-01-23 12:53:43 +0530 | [diff] [blame] | 561 | dmabuf->priv = exp_info->priv; |
| 562 | dmabuf->ops = exp_info->ops; |
| 563 | dmabuf->size = exp_info->size; |
| 564 | dmabuf->exp_name = exp_info->exp_name; |
Sumit Semwal | 9abdffe | 2015-05-05 14:56:15 +0530 | [diff] [blame] | 565 | dmabuf->owner = exp_info->owner; |
Charan Teja Kalla | 6348dd2 | 2020-06-19 17:27:19 +0530 | [diff] [blame] | 566 | spin_lock_init(&dmabuf->name_lock); |
Maarten Lankhorst | 9b495a5 | 2014-07-01 12:57:43 +0200 | [diff] [blame] | 567 | init_waitqueue_head(&dmabuf->poll); |
| 568 | dmabuf->cb_excl.poll = dmabuf->cb_shared.poll = &dmabuf->poll; |
| 569 | dmabuf->cb_excl.active = dmabuf->cb_shared.active = 0; |
| 570 | |
Maarten Lankhorst | 3aac450 | 2014-07-01 12:57:26 +0200 | [diff] [blame] | 571 | if (!resv) { |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 572 | resv = (struct dma_resv *)&dmabuf[1]; |
| 573 | dma_resv_init(resv); |
Maarten Lankhorst | 3aac450 | 2014-07-01 12:57:26 +0200 | [diff] [blame] | 574 | } |
| 575 | dmabuf->resv = resv; |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 576 | |
Greg Hackmann | ed63bb1 | 2019-06-13 15:34:06 -0700 | [diff] [blame] | 577 | file = dma_buf_getfile(dmabuf, exp_info->flags); |
Tuomas Tynkkynen | 9022e24 | 2013-08-27 16:30:38 +0300 | [diff] [blame] | 578 | if (IS_ERR(file)) { |
Chris Wilson | a026df4 | 2016-07-18 12:16:22 +0100 | [diff] [blame] | 579 | ret = PTR_ERR(file); |
| 580 | goto err_dmabuf; |
Tuomas Tynkkynen | 9022e24 | 2013-08-27 16:30:38 +0300 | [diff] [blame] | 581 | } |
Christopher James Halse Rogers | 19e8697 | 2013-09-10 11:36:45 +0530 | [diff] [blame] | 582 | |
| 583 | file->f_mode |= FMODE_LSEEK; |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 584 | dmabuf->file = file; |
| 585 | |
Hridya Valsaraju | bdb8d06 | 2021-06-03 14:47:51 -0700 | [diff] [blame] | 586 | ret = dma_buf_stats_setup(dmabuf); |
| 587 | if (ret) |
| 588 | goto err_sysfs; |
| 589 | |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 590 | mutex_init(&dmabuf->lock); |
| 591 | INIT_LIST_HEAD(&dmabuf->attachments); |
| 592 | |
Sumit Semwal | b89e3563 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 593 | mutex_lock(&db_list.lock); |
| 594 | list_add(&dmabuf->list_node, &db_list.head); |
| 595 | mutex_unlock(&db_list.lock); |
| 596 | |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 597 | return dmabuf; |
Chris Wilson | a026df4 | 2016-07-18 12:16:22 +0100 | [diff] [blame] | 598 | |
Hridya Valsaraju | bdb8d06 | 2021-06-03 14:47:51 -0700 | [diff] [blame] | 599 | err_sysfs: |
| 600 | /* |
| 601 | * Set file->f_path.dentry->d_fsdata to NULL so that when |
| 602 | * dma_buf_release() gets invoked by dentry_ops, it exits |
| 603 | * early before calling the release() dma_buf op. |
| 604 | */ |
| 605 | file->f_path.dentry->d_fsdata = NULL; |
| 606 | fput(file); |
Chris Wilson | a026df4 | 2016-07-18 12:16:22 +0100 | [diff] [blame] | 607 | err_dmabuf: |
| 608 | kfree(dmabuf); |
| 609 | err_module: |
| 610 | module_put(exp_info->owner); |
| 611 | return ERR_PTR(ret); |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 612 | } |
Sumit Semwal | d8fbe34 | 2015-01-23 12:53:43 +0530 | [diff] [blame] | 613 | EXPORT_SYMBOL_GPL(dma_buf_export); |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 614 | |
| 615 | /** |
Daniel Vetter | 85804b7 | 2020-12-11 16:58:41 +0100 | [diff] [blame] | 616 | * dma_buf_fd - returns a file descriptor for the given struct dma_buf |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 617 | * @dmabuf: [in] pointer to dma_buf for which fd is required. |
Dave Airlie | 55c1c4c | 2012-03-16 10:34:02 +0000 | [diff] [blame] | 618 | * @flags: [in] flags to give to fd |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 619 | * |
| 620 | * On success, returns an associated 'fd'. Else, returns error. |
| 621 | */ |
Dave Airlie | 55c1c4c | 2012-03-16 10:34:02 +0000 | [diff] [blame] | 622 | int dma_buf_fd(struct dma_buf *dmabuf, int flags) |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 623 | { |
Borislav Petkov | f5e097f | 2012-12-11 16:05:26 +0100 | [diff] [blame] | 624 | int fd; |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 625 | |
| 626 | if (!dmabuf || !dmabuf->file) |
| 627 | return -EINVAL; |
| 628 | |
Borislav Petkov | f5e097f | 2012-12-11 16:05:26 +0100 | [diff] [blame] | 629 | fd = get_unused_fd_flags(flags); |
| 630 | if (fd < 0) |
| 631 | return fd; |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 632 | |
| 633 | fd_install(fd, dmabuf->file); |
| 634 | |
| 635 | return fd; |
| 636 | } |
| 637 | EXPORT_SYMBOL_GPL(dma_buf_fd); |
| 638 | |
| 639 | /** |
Daniel Vetter | 85804b7 | 2020-12-11 16:58:41 +0100 | [diff] [blame] | 640 | * dma_buf_get - returns the struct dma_buf related to an fd |
| 641 | * @fd: [in] fd associated with the struct dma_buf to be returned |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 642 | * |
Daniel Vetter | 85804b7 | 2020-12-11 16:58:41 +0100 | [diff] [blame] | 643 | * On success, returns the struct dma_buf associated with an fd; uses |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 644 | * file's refcounting done by fget to increase refcount. returns ERR_PTR |
| 645 | * otherwise. |
| 646 | */ |
| 647 | struct dma_buf *dma_buf_get(int fd) |
| 648 | { |
| 649 | struct file *file; |
| 650 | |
| 651 | file = fget(fd); |
| 652 | |
| 653 | if (!file) |
| 654 | return ERR_PTR(-EBADF); |
| 655 | |
| 656 | if (!is_dma_buf_file(file)) { |
| 657 | fput(file); |
| 658 | return ERR_PTR(-EINVAL); |
| 659 | } |
| 660 | |
| 661 | return file->private_data; |
| 662 | } |
| 663 | EXPORT_SYMBOL_GPL(dma_buf_get); |
| 664 | |
| 665 | /** |
| 666 | * dma_buf_put - decreases refcount of the buffer |
| 667 | * @dmabuf: [in] buffer to reduce refcount of |
| 668 | * |
Daniel Vetter | 2904a8c | 2016-12-09 19:53:07 +0100 | [diff] [blame] | 669 | * Uses file's refcounting done implicitly by fput(). |
| 670 | * |
| 671 | * If, as a result of this call, the refcount becomes 0, the 'release' file |
Daniel Vetter | e9b4d7b | 2016-12-29 21:48:25 +0100 | [diff] [blame] | 672 | * operation related to this fd is called. It calls &dma_buf_ops.release vfunc |
| 673 | * in turn, and frees the memory allocated for dmabuf when exported. |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 674 | */ |
| 675 | void dma_buf_put(struct dma_buf *dmabuf) |
| 676 | { |
| 677 | if (WARN_ON(!dmabuf || !dmabuf->file)) |
| 678 | return; |
| 679 | |
| 680 | fput(dmabuf->file); |
| 681 | } |
| 682 | EXPORT_SYMBOL_GPL(dma_buf_put); |
| 683 | |
Daniel Vetter | 8433567 | 2021-01-15 17:47:39 +0100 | [diff] [blame] | 684 | static void mangle_sg_table(struct sg_table *sg_table) |
| 685 | { |
| 686 | #ifdef CONFIG_DMABUF_DEBUG |
| 687 | int i; |
| 688 | struct scatterlist *sg; |
| 689 | |
| 690 | /* To catch abuse of the underlying struct page by importers mix |
| 691 | * up the bits, but take care to preserve the low SG_ bits to |
| 692 | * not corrupt the sgt. The mixing is undone in __unmap_dma_buf |
| 693 | * before passing the sgt back to the exporter. */ |
| 694 | for_each_sgtable_sg(sg_table, sg, i) |
| 695 | sg->page_link ^= ~0xffUL; |
| 696 | #endif |
| 697 | |
| 698 | } |
| 699 | static struct sg_table * __map_dma_buf(struct dma_buf_attachment *attach, |
| 700 | enum dma_data_direction direction) |
| 701 | { |
| 702 | struct sg_table *sg_table; |
| 703 | |
| 704 | sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction); |
| 705 | |
| 706 | if (!IS_ERR_OR_NULL(sg_table)) |
| 707 | mangle_sg_table(sg_table); |
| 708 | |
| 709 | return sg_table; |
| 710 | } |
| 711 | |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 712 | /** |
Daniel Vetter | 85804b7 | 2020-12-11 16:58:41 +0100 | [diff] [blame] | 713 | * dma_buf_dynamic_attach - Add the device to dma_buf's attachments list |
Christian König | 15fd552 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 714 | * @dmabuf: [in] buffer to attach device to. |
| 715 | * @dev: [in] device to be attached. |
Randy Dunlap | 6f49c25 | 2020-04-07 21:20:34 -0700 | [diff] [blame] | 716 | * @importer_ops: [in] importer operations for the attachment |
| 717 | * @importer_priv: [in] importer private pointer for the attachment |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 718 | * |
Daniel Vetter | 2904a8c | 2016-12-09 19:53:07 +0100 | [diff] [blame] | 719 | * Returns struct dma_buf_attachment pointer for this attachment. Attachments |
| 720 | * must be cleaned up by calling dma_buf_detach(). |
| 721 | * |
Daniel Vetter | 85804b7 | 2020-12-11 16:58:41 +0100 | [diff] [blame] | 722 | * Optionally this calls &dma_buf_ops.attach to allow device-specific attach |
| 723 | * functionality. |
| 724 | * |
Daniel Vetter | 2904a8c | 2016-12-09 19:53:07 +0100 | [diff] [blame] | 725 | * Returns: |
| 726 | * |
| 727 | * A pointer to newly created &dma_buf_attachment on success, or a negative |
| 728 | * error code wrapped into a pointer on failure. |
| 729 | * |
| 730 | * Note that this can fail if the backing storage of @dmabuf is in a place not |
| 731 | * accessible to @dev, and cannot be moved to a more suitable place. This is |
| 732 | * indicated with the error code -EBUSY. |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 733 | */ |
Christian König | 15fd552 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 734 | struct dma_buf_attachment * |
| 735 | dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct device *dev, |
Christian König | bb42df4 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 736 | const struct dma_buf_attach_ops *importer_ops, |
| 737 | void *importer_priv) |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 738 | { |
| 739 | struct dma_buf_attachment *attach; |
| 740 | int ret; |
Hridya Valsaraju | bdb8d06 | 2021-06-03 14:47:51 -0700 | [diff] [blame] | 741 | unsigned int attach_uid; |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 742 | |
Laurent Pinchart | d1aa06a | 2012-01-26 12:27:23 +0100 | [diff] [blame] | 743 | if (WARN_ON(!dmabuf || !dev)) |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 744 | return ERR_PTR(-EINVAL); |
| 745 | |
Christian König | 4981cdb | 2020-02-19 13:32:43 +0100 | [diff] [blame] | 746 | if (WARN_ON(importer_ops && !importer_ops->move_notify)) |
| 747 | return ERR_PTR(-EINVAL); |
| 748 | |
Markus Elfring | db7942b | 2017-05-08 10:50:09 +0200 | [diff] [blame] | 749 | attach = kzalloc(sizeof(*attach), GFP_KERNEL); |
Markus Elfring | 34d84ec | 2017-05-08 10:54:17 +0200 | [diff] [blame] | 750 | if (!attach) |
Laurent Pinchart | a9fbc3b | 2012-01-26 12:27:24 +0100 | [diff] [blame] | 751 | return ERR_PTR(-ENOMEM); |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 752 | |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 753 | attach->dev = dev; |
| 754 | attach->dmabuf = dmabuf; |
Christian König | 09606b5 | 2018-03-22 17:09:42 +0100 | [diff] [blame] | 755 | if (importer_ops) |
| 756 | attach->peer2peer = importer_ops->allow_peer2peer; |
Christian König | bb42df4 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 757 | attach->importer_ops = importer_ops; |
| 758 | attach->importer_priv = importer_priv; |
Laurent Pinchart | 2ed9201 | 2012-01-26 12:27:25 +0100 | [diff] [blame] | 759 | |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 760 | if (dmabuf->ops->attach) { |
Christian König | a19741e | 2018-05-28 11:47:52 +0200 | [diff] [blame] | 761 | ret = dmabuf->ops->attach(dmabuf, attach); |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 762 | if (ret) |
| 763 | goto err_attach; |
| 764 | } |
Christian König | 15fd552 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 765 | dma_resv_lock(dmabuf->resv, NULL); |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 766 | list_add(&attach->node, &dmabuf->attachments); |
Hridya Valsaraju | bdb8d06 | 2021-06-03 14:47:51 -0700 | [diff] [blame] | 767 | attach_uid = dma_buf_update_attach_uid(dmabuf); |
Christian König | 15fd552 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 768 | dma_resv_unlock(dmabuf->resv); |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 769 | |
Hridya Valsaraju | bdb8d06 | 2021-06-03 14:47:51 -0700 | [diff] [blame] | 770 | ret = dma_buf_attach_stats_setup(attach, attach_uid); |
| 771 | if (ret) |
| 772 | goto err_sysfs; |
| 773 | |
Christian König | 15fd552 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 774 | /* When either the importer or the exporter can't handle dynamic |
| 775 | * mappings we cache the mapping here to avoid issues with the |
| 776 | * reservation object lock. |
| 777 | */ |
| 778 | if (dma_buf_attachment_is_dynamic(attach) != |
| 779 | dma_buf_is_dynamic(dmabuf)) { |
| 780 | struct sg_table *sgt; |
| 781 | |
Christian König | bb42df4 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 782 | if (dma_buf_is_dynamic(attach->dmabuf)) { |
Christian König | 15fd552 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 783 | dma_resv_lock(attach->dmabuf->resv, NULL); |
Christian König | 7e008b0 | 2021-05-17 13:20:17 +0200 | [diff] [blame] | 784 | ret = dmabuf->ops->pin(attach); |
Christian König | bb42df4 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 785 | if (ret) |
| 786 | goto err_unlock; |
| 787 | } |
Christian König | 15fd552 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 788 | |
Daniel Vetter | 8433567 | 2021-01-15 17:47:39 +0100 | [diff] [blame] | 789 | sgt = __map_dma_buf(attach, DMA_BIDIRECTIONAL); |
Christian König | 15fd552 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 790 | if (!sgt) |
| 791 | sgt = ERR_PTR(-ENOMEM); |
| 792 | if (IS_ERR(sgt)) { |
| 793 | ret = PTR_ERR(sgt); |
Christian König | bb42df4 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 794 | goto err_unpin; |
Christian König | 15fd552 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 795 | } |
| 796 | if (dma_buf_is_dynamic(attach->dmabuf)) |
| 797 | dma_resv_unlock(attach->dmabuf->resv); |
| 798 | attach->sgt = sgt; |
| 799 | attach->dir = DMA_BIDIRECTIONAL; |
Hridya Valsaraju | bdb8d06 | 2021-06-03 14:47:51 -0700 | [diff] [blame] | 800 | dma_buf_update_attachment_map_count(attach, 1 /* delta */); |
Christian König | 15fd552 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 801 | } |
| 802 | |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 803 | return attach; |
| 804 | |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 805 | err_attach: |
| 806 | kfree(attach); |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 807 | return ERR_PTR(ret); |
Christian König | 15fd552 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 808 | |
Christian König | bb42df4 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 809 | err_unpin: |
| 810 | if (dma_buf_is_dynamic(attach->dmabuf)) |
Christian König | 7e008b0 | 2021-05-17 13:20:17 +0200 | [diff] [blame] | 811 | dmabuf->ops->unpin(attach); |
Christian König | bb42df4 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 812 | |
Christian König | 15fd552 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 813 | err_unlock: |
| 814 | if (dma_buf_is_dynamic(attach->dmabuf)) |
| 815 | dma_resv_unlock(attach->dmabuf->resv); |
| 816 | |
Hridya Valsaraju | bdb8d06 | 2021-06-03 14:47:51 -0700 | [diff] [blame] | 817 | err_sysfs: |
Christian König | 15fd552 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 818 | dma_buf_detach(dmabuf, attach); |
| 819 | return ERR_PTR(ret); |
| 820 | } |
| 821 | EXPORT_SYMBOL_GPL(dma_buf_dynamic_attach); |
| 822 | |
| 823 | /** |
| 824 | * dma_buf_attach - Wrapper for dma_buf_dynamic_attach |
| 825 | * @dmabuf: [in] buffer to attach device to. |
| 826 | * @dev: [in] device to be attached. |
| 827 | * |
| 828 | * Wrapper to call dma_buf_dynamic_attach() for drivers which still use a static |
| 829 | * mapping. |
| 830 | */ |
| 831 | struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf, |
| 832 | struct device *dev) |
| 833 | { |
Christian König | bb42df4 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 834 | return dma_buf_dynamic_attach(dmabuf, dev, NULL, NULL); |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 835 | } |
| 836 | EXPORT_SYMBOL_GPL(dma_buf_attach); |
| 837 | |
Daniel Vetter | 8433567 | 2021-01-15 17:47:39 +0100 | [diff] [blame] | 838 | static void __unmap_dma_buf(struct dma_buf_attachment *attach, |
| 839 | struct sg_table *sg_table, |
| 840 | enum dma_data_direction direction) |
| 841 | { |
| 842 | /* uses XOR, hence this unmangles */ |
| 843 | mangle_sg_table(sg_table); |
| 844 | |
| 845 | attach->dmabuf->ops->unmap_dma_buf(attach, sg_table, direction); |
| 846 | } |
| 847 | |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 848 | /** |
Daniel Vetter | 85804b7 | 2020-12-11 16:58:41 +0100 | [diff] [blame] | 849 | * dma_buf_detach - Remove the given attachment from dmabuf's attachments list |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 850 | * @dmabuf: [in] buffer to detach from. |
| 851 | * @attach: [in] attachment to be detached; is free'd after this call. |
| 852 | * |
Daniel Vetter | 2904a8c | 2016-12-09 19:53:07 +0100 | [diff] [blame] | 853 | * Clean up a device attachment obtained by calling dma_buf_attach(). |
Daniel Vetter | 85804b7 | 2020-12-11 16:58:41 +0100 | [diff] [blame] | 854 | * |
| 855 | * Optionally this calls &dma_buf_ops.detach for device-specific detach. |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 856 | */ |
| 857 | void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach) |
| 858 | { |
Laurent Pinchart | d1aa06a | 2012-01-26 12:27:23 +0100 | [diff] [blame] | 859 | if (WARN_ON(!dmabuf || !attach)) |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 860 | return; |
| 861 | |
Christian König | 15fd552 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 862 | if (attach->sgt) { |
| 863 | if (dma_buf_is_dynamic(attach->dmabuf)) |
| 864 | dma_resv_lock(attach->dmabuf->resv, NULL); |
| 865 | |
Daniel Vetter | 8433567 | 2021-01-15 17:47:39 +0100 | [diff] [blame] | 866 | __unmap_dma_buf(attach, attach->sgt, attach->dir); |
Hridya Valsaraju | bdb8d06 | 2021-06-03 14:47:51 -0700 | [diff] [blame] | 867 | dma_buf_update_attachment_map_count(attach, -1 /* delta */); |
Christian König | f13e143 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 868 | |
Christian König | bb42df4 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 869 | if (dma_buf_is_dynamic(attach->dmabuf)) { |
Christian König | 7e008b0 | 2021-05-17 13:20:17 +0200 | [diff] [blame] | 870 | dmabuf->ops->unpin(attach); |
Christian König | 15fd552 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 871 | dma_resv_unlock(attach->dmabuf->resv); |
Christian König | bb42df4 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 872 | } |
Christian König | 15fd552 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 873 | } |
| 874 | |
Christian König | 15fd552 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 875 | dma_resv_lock(dmabuf->resv, NULL); |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 876 | list_del(&attach->node); |
Christian König | 15fd552 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 877 | dma_resv_unlock(dmabuf->resv); |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 878 | if (dmabuf->ops->detach) |
| 879 | dmabuf->ops->detach(dmabuf, attach); |
| 880 | |
Hridya Valsaraju | bdb8d06 | 2021-06-03 14:47:51 -0700 | [diff] [blame] | 881 | dma_buf_attach_stats_teardown(attach); |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 882 | kfree(attach); |
| 883 | } |
| 884 | EXPORT_SYMBOL_GPL(dma_buf_detach); |
| 885 | |
| 886 | /** |
Christian König | bb42df4 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 887 | * dma_buf_pin - Lock down the DMA-buf |
Christian König | bb42df4 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 888 | * @attach: [in] attachment which should be pinned |
| 889 | * |
Daniel Vetter | c545781 | 2020-12-11 16:58:43 +0100 | [diff] [blame] | 890 | * Only dynamic importers (who set up @attach with dma_buf_dynamic_attach()) may |
| 891 | * call this, and only for limited use cases like scanout and not for temporary |
| 892 | * pin operations. It is not permitted to allow userspace to pin arbitrary |
| 893 | * amounts of buffers through this interface. |
| 894 | * |
| 895 | * Buffers must be unpinned by calling dma_buf_unpin(). |
| 896 | * |
Christian König | bb42df4 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 897 | * Returns: |
| 898 | * 0 on success, negative error code on failure. |
| 899 | */ |
| 900 | int dma_buf_pin(struct dma_buf_attachment *attach) |
| 901 | { |
| 902 | struct dma_buf *dmabuf = attach->dmabuf; |
| 903 | int ret = 0; |
| 904 | |
Daniel Vetter | c545781 | 2020-12-11 16:58:43 +0100 | [diff] [blame] | 905 | WARN_ON(!dma_buf_attachment_is_dynamic(attach)); |
| 906 | |
Christian König | bb42df4 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 907 | dma_resv_assert_held(dmabuf->resv); |
| 908 | |
| 909 | if (dmabuf->ops->pin) |
| 910 | ret = dmabuf->ops->pin(attach); |
| 911 | |
| 912 | return ret; |
| 913 | } |
| 914 | EXPORT_SYMBOL_GPL(dma_buf_pin); |
| 915 | |
| 916 | /** |
Daniel Vetter | c545781 | 2020-12-11 16:58:43 +0100 | [diff] [blame] | 917 | * dma_buf_unpin - Unpin a DMA-buf |
Christian König | bb42df4 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 918 | * @attach: [in] attachment which should be unpinned |
Daniel Vetter | c545781 | 2020-12-11 16:58:43 +0100 | [diff] [blame] | 919 | * |
| 920 | * This unpins a buffer pinned by dma_buf_pin() and allows the exporter to move |
| 921 | * any mapping of @attach again and inform the importer through |
| 922 | * &dma_buf_attach_ops.move_notify. |
Christian König | bb42df4 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 923 | */ |
| 924 | void dma_buf_unpin(struct dma_buf_attachment *attach) |
| 925 | { |
| 926 | struct dma_buf *dmabuf = attach->dmabuf; |
| 927 | |
Daniel Vetter | c545781 | 2020-12-11 16:58:43 +0100 | [diff] [blame] | 928 | WARN_ON(!dma_buf_attachment_is_dynamic(attach)); |
| 929 | |
Christian König | bb42df4 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 930 | dma_resv_assert_held(dmabuf->resv); |
| 931 | |
| 932 | if (dmabuf->ops->unpin) |
| 933 | dmabuf->ops->unpin(attach); |
| 934 | } |
| 935 | EXPORT_SYMBOL_GPL(dma_buf_unpin); |
| 936 | |
| 937 | /** |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 938 | * dma_buf_map_attachment - Returns the scatterlist table of the attachment; |
| 939 | * mapped into _device_ address space. Is a wrapper for map_dma_buf() of the |
| 940 | * dma_buf_ops. |
| 941 | * @attach: [in] attachment whose scatterlist is to be returned |
| 942 | * @direction: [in] direction of DMA transfer |
| 943 | * |
Colin Cross | fee0c54 | 2013-12-20 16:43:50 -0800 | [diff] [blame] | 944 | * Returns sg_table containing the scatterlist to be returned; returns ERR_PTR |
Daniel Vetter | 2904a8c | 2016-12-09 19:53:07 +0100 | [diff] [blame] | 945 | * on error. May return -EINTR if it is interrupted by a signal. |
| 946 | * |
Jianxin Xiong | ac80cd1 | 2020-10-14 09:16:01 -0700 | [diff] [blame] | 947 | * On success, the DMA addresses and lengths in the returned scatterlist are |
| 948 | * PAGE_SIZE aligned. |
| 949 | * |
Liviu Dudau | c138782 | 2017-11-01 14:06:30 +0000 | [diff] [blame] | 950 | * A mapping must be unmapped by using dma_buf_unmap_attachment(). Note that |
Daniel Vetter | 2904a8c | 2016-12-09 19:53:07 +0100 | [diff] [blame] | 951 | * the underlying backing storage is pinned for as long as a mapping exists, |
| 952 | * therefore users/importers should not hold onto a mapping for undue amounts of |
| 953 | * time. |
Daniel Vetter | 89bcadc | 2021-06-21 17:17:58 +0200 | [diff] [blame^] | 954 | * |
| 955 | * Important: Dynamic importers must wait for the exclusive fence of the struct |
| 956 | * dma_resv attached to the DMA-BUF first. |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 957 | */ |
| 958 | struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach, |
| 959 | enum dma_data_direction direction) |
| 960 | { |
Colin Ian King | 531beb0 | 2017-09-15 00:05:16 +0100 | [diff] [blame] | 961 | struct sg_table *sg_table; |
Christian König | bb42df4 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 962 | int r; |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 963 | |
| 964 | might_sleep(); |
| 965 | |
Laurent Pinchart | d1aa06a | 2012-01-26 12:27:23 +0100 | [diff] [blame] | 966 | if (WARN_ON(!attach || !attach->dmabuf)) |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 967 | return ERR_PTR(-EINVAL); |
| 968 | |
Christian König | 15fd552 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 969 | if (dma_buf_attachment_is_dynamic(attach)) |
| 970 | dma_resv_assert_held(attach->dmabuf->resv); |
| 971 | |
Christian König | f13e143 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 972 | if (attach->sgt) { |
| 973 | /* |
| 974 | * Two mappings with different directions for the same |
| 975 | * attachment are not allowed. |
| 976 | */ |
| 977 | if (attach->dir != direction && |
| 978 | attach->dir != DMA_BIDIRECTIONAL) |
| 979 | return ERR_PTR(-EBUSY); |
| 980 | |
| 981 | return attach->sgt; |
| 982 | } |
| 983 | |
Christian König | bb42df4 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 984 | if (dma_buf_is_dynamic(attach->dmabuf)) { |
Christian König | 15fd552 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 985 | dma_resv_assert_held(attach->dmabuf->resv); |
Christian König | 4981cdb | 2020-02-19 13:32:43 +0100 | [diff] [blame] | 986 | if (!IS_ENABLED(CONFIG_DMABUF_MOVE_NOTIFY)) { |
Christian König | 7e008b0 | 2021-05-17 13:20:17 +0200 | [diff] [blame] | 987 | r = attach->dmabuf->ops->pin(attach); |
Christian König | bb42df4 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 988 | if (r) |
| 989 | return ERR_PTR(r); |
| 990 | } |
| 991 | } |
Christian König | 15fd552 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 992 | |
Daniel Vetter | 8433567 | 2021-01-15 17:47:39 +0100 | [diff] [blame] | 993 | sg_table = __map_dma_buf(attach, direction); |
Colin Cross | fee0c54 | 2013-12-20 16:43:50 -0800 | [diff] [blame] | 994 | if (!sg_table) |
| 995 | sg_table = ERR_PTR(-ENOMEM); |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 996 | |
Christian König | bb42df4 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 997 | if (IS_ERR(sg_table) && dma_buf_is_dynamic(attach->dmabuf) && |
Christian König | 4981cdb | 2020-02-19 13:32:43 +0100 | [diff] [blame] | 998 | !IS_ENABLED(CONFIG_DMABUF_MOVE_NOTIFY)) |
Christian König | 7e008b0 | 2021-05-17 13:20:17 +0200 | [diff] [blame] | 999 | attach->dmabuf->ops->unpin(attach); |
Christian König | bb42df4 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 1000 | |
Christian König | f13e143 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 1001 | if (!IS_ERR(sg_table) && attach->dmabuf->ops->cache_sgt_mapping) { |
| 1002 | attach->sgt = sg_table; |
| 1003 | attach->dir = direction; |
| 1004 | } |
| 1005 | |
Jianxin Xiong | ac80cd1 | 2020-10-14 09:16:01 -0700 | [diff] [blame] | 1006 | #ifdef CONFIG_DMA_API_DEBUG |
Jianxin Xiong | 00efd65 | 2020-11-02 19:51:58 -0800 | [diff] [blame] | 1007 | if (!IS_ERR(sg_table)) { |
Jianxin Xiong | ac80cd1 | 2020-10-14 09:16:01 -0700 | [diff] [blame] | 1008 | struct scatterlist *sg; |
| 1009 | u64 addr; |
| 1010 | int len; |
| 1011 | int i; |
| 1012 | |
| 1013 | for_each_sgtable_dma_sg(sg_table, sg, i) { |
| 1014 | addr = sg_dma_address(sg); |
| 1015 | len = sg_dma_len(sg); |
| 1016 | if (!PAGE_ALIGNED(addr) || !PAGE_ALIGNED(len)) { |
| 1017 | pr_debug("%s: addr %llx or len %x is not page aligned!\n", |
| 1018 | __func__, addr, len); |
| 1019 | } |
| 1020 | } |
| 1021 | } |
| 1022 | #endif /* CONFIG_DMA_API_DEBUG */ |
| 1023 | |
Hridya Valsaraju | bdb8d06 | 2021-06-03 14:47:51 -0700 | [diff] [blame] | 1024 | if (!IS_ERR(sg_table)) |
| 1025 | dma_buf_update_attachment_map_count(attach, 1 /* delta */); |
| 1026 | |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 1027 | return sg_table; |
| 1028 | } |
| 1029 | EXPORT_SYMBOL_GPL(dma_buf_map_attachment); |
| 1030 | |
| 1031 | /** |
| 1032 | * dma_buf_unmap_attachment - unmaps and decreases usecount of the buffer;might |
| 1033 | * deallocate the scatterlist associated. Is a wrapper for unmap_dma_buf() of |
| 1034 | * dma_buf_ops. |
| 1035 | * @attach: [in] attachment to unmap buffer from |
| 1036 | * @sg_table: [in] scatterlist info of the buffer to unmap |
Sumit Semwal | 33ea2dc | 2012-01-27 15:09:27 +0530 | [diff] [blame] | 1037 | * @direction: [in] direction of DMA transfer |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 1038 | * |
Daniel Vetter | 2904a8c | 2016-12-09 19:53:07 +0100 | [diff] [blame] | 1039 | * This unmaps a DMA mapping for @attached obtained by dma_buf_map_attachment(). |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 1040 | */ |
| 1041 | void dma_buf_unmap_attachment(struct dma_buf_attachment *attach, |
Sumit Semwal | 33ea2dc | 2012-01-27 15:09:27 +0530 | [diff] [blame] | 1042 | struct sg_table *sg_table, |
| 1043 | enum dma_data_direction direction) |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 1044 | { |
Rob Clark | b6fa0cd | 2012-09-28 09:29:43 +0200 | [diff] [blame] | 1045 | might_sleep(); |
| 1046 | |
Laurent Pinchart | d1aa06a | 2012-01-26 12:27:23 +0100 | [diff] [blame] | 1047 | if (WARN_ON(!attach || !attach->dmabuf || !sg_table)) |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 1048 | return; |
| 1049 | |
Christian König | 15fd552 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 1050 | if (dma_buf_attachment_is_dynamic(attach)) |
| 1051 | dma_resv_assert_held(attach->dmabuf->resv); |
| 1052 | |
Christian König | f13e143 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 1053 | if (attach->sgt == sg_table) |
| 1054 | return; |
| 1055 | |
Christian König | 15fd552 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 1056 | if (dma_buf_is_dynamic(attach->dmabuf)) |
| 1057 | dma_resv_assert_held(attach->dmabuf->resv); |
| 1058 | |
Daniel Vetter | 8433567 | 2021-01-15 17:47:39 +0100 | [diff] [blame] | 1059 | __unmap_dma_buf(attach, sg_table, direction); |
Christian König | bb42df4 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 1060 | |
| 1061 | if (dma_buf_is_dynamic(attach->dmabuf) && |
Christian König | 4981cdb | 2020-02-19 13:32:43 +0100 | [diff] [blame] | 1062 | !IS_ENABLED(CONFIG_DMABUF_MOVE_NOTIFY)) |
Christian König | bb42df4 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 1063 | dma_buf_unpin(attach); |
Hridya Valsaraju | bdb8d06 | 2021-06-03 14:47:51 -0700 | [diff] [blame] | 1064 | |
| 1065 | dma_buf_update_attachment_map_count(attach, -1 /* delta */); |
Sumit Semwal | d15bd7e | 2011-12-26 14:53:15 +0530 | [diff] [blame] | 1066 | } |
| 1067 | EXPORT_SYMBOL_GPL(dma_buf_unmap_attachment); |
Daniel Vetter | fc13020 | 2012-03-20 00:02:37 +0100 | [diff] [blame] | 1068 | |
Daniel Vetter | 0959a16 | 2016-12-09 19:53:08 +0100 | [diff] [blame] | 1069 | /** |
Christian König | bb42df4 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 1070 | * dma_buf_move_notify - notify attachments that DMA-buf is moving |
| 1071 | * |
| 1072 | * @dmabuf: [in] buffer which is moving |
| 1073 | * |
| 1074 | * Informs all attachmenst that they need to destroy and recreated all their |
| 1075 | * mappings. |
| 1076 | */ |
| 1077 | void dma_buf_move_notify(struct dma_buf *dmabuf) |
| 1078 | { |
| 1079 | struct dma_buf_attachment *attach; |
| 1080 | |
| 1081 | dma_resv_assert_held(dmabuf->resv); |
| 1082 | |
| 1083 | list_for_each_entry(attach, &dmabuf->attachments, node) |
Christian König | 4981cdb | 2020-02-19 13:32:43 +0100 | [diff] [blame] | 1084 | if (attach->importer_ops) |
Christian König | bb42df4 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 1085 | attach->importer_ops->move_notify(attach); |
| 1086 | } |
| 1087 | EXPORT_SYMBOL_GPL(dma_buf_move_notify); |
| 1088 | |
| 1089 | /** |
Daniel Vetter | 0959a16 | 2016-12-09 19:53:08 +0100 | [diff] [blame] | 1090 | * DOC: cpu access |
| 1091 | * |
| 1092 | * There are mutliple reasons for supporting CPU access to a dma buffer object: |
| 1093 | * |
| 1094 | * - Fallback operations in the kernel, for example when a device is connected |
| 1095 | * over USB and the kernel needs to shuffle the data around first before |
| 1096 | * sending it away. Cache coherency is handled by braketing any transactions |
| 1097 | * with calls to dma_buf_begin_cpu_access() and dma_buf_end_cpu_access() |
| 1098 | * access. |
| 1099 | * |
Daniel Vetter | 7f0de8d | 2019-11-18 11:35:30 +0100 | [diff] [blame] | 1100 | * Since for most kernel internal dma-buf accesses need the entire buffer, a |
| 1101 | * vmap interface is introduced. Note that on very old 32-bit architectures |
| 1102 | * vmalloc space might be limited and result in vmap calls failing. |
Daniel Vetter | 0959a16 | 2016-12-09 19:53:08 +0100 | [diff] [blame] | 1103 | * |
| 1104 | * Interfaces:: |
Daniel Vetter | de9114e | 2020-12-11 16:58:40 +0100 | [diff] [blame] | 1105 | * |
Daniel Vetter | 0959a16 | 2016-12-09 19:53:08 +0100 | [diff] [blame] | 1106 | * void \*dma_buf_vmap(struct dma_buf \*dmabuf) |
| 1107 | * void dma_buf_vunmap(struct dma_buf \*dmabuf, void \*vaddr) |
| 1108 | * |
| 1109 | * The vmap call can fail if there is no vmap support in the exporter, or if |
Daniel Vetter | de9114e | 2020-12-11 16:58:40 +0100 | [diff] [blame] | 1110 | * it runs out of vmalloc space. Note that the dma-buf layer keeps a reference |
| 1111 | * count for all vmap access and calls down into the exporter's vmap function |
| 1112 | * only when no vmapping exists, and only unmaps it once. Protection against |
| 1113 | * concurrent vmap/vunmap calls is provided by taking the &dma_buf.lock mutex. |
Daniel Vetter | 0959a16 | 2016-12-09 19:53:08 +0100 | [diff] [blame] | 1114 | * |
| 1115 | * - For full compatibility on the importer side with existing userspace |
| 1116 | * interfaces, which might already support mmap'ing buffers. This is needed in |
| 1117 | * many processing pipelines (e.g. feeding a software rendered image into a |
| 1118 | * hardware pipeline, thumbnail creation, snapshots, ...). Also, Android's ION |
| 1119 | * framework already supported this and for DMA buffer file descriptors to |
| 1120 | * replace ION buffers mmap support was needed. |
| 1121 | * |
| 1122 | * There is no special interfaces, userspace simply calls mmap on the dma-buf |
| 1123 | * fd. But like for CPU access there's a need to braket the actual access, |
| 1124 | * which is handled by the ioctl (DMA_BUF_IOCTL_SYNC). Note that |
| 1125 | * DMA_BUF_IOCTL_SYNC can fail with -EAGAIN or -EINTR, in which case it must |
| 1126 | * be restarted. |
| 1127 | * |
| 1128 | * Some systems might need some sort of cache coherency management e.g. when |
| 1129 | * CPU and GPU domains are being accessed through dma-buf at the same time. |
| 1130 | * To circumvent this problem there are begin/end coherency markers, that |
| 1131 | * forward directly to existing dma-buf device drivers vfunc hooks. Userspace |
| 1132 | * can make use of those markers through the DMA_BUF_IOCTL_SYNC ioctl. The |
| 1133 | * sequence would be used like following: |
| 1134 | * |
| 1135 | * - mmap dma-buf fd |
| 1136 | * - for each drawing/upload cycle in CPU 1. SYNC_START ioctl, 2. read/write |
| 1137 | * to mmap area 3. SYNC_END ioctl. This can be repeated as often as you |
| 1138 | * want (with the new data being consumed by say the GPU or the scanout |
| 1139 | * device) |
| 1140 | * - munmap once you don't need the buffer any more |
| 1141 | * |
| 1142 | * For correctness and optimal performance, it is always required to use |
| 1143 | * SYNC_START and SYNC_END before and after, respectively, when accessing the |
| 1144 | * mapped address. Userspace cannot rely on coherent access, even when there |
| 1145 | * are systems where it just works without calling these ioctls. |
| 1146 | * |
| 1147 | * - And as a CPU fallback in userspace processing pipelines. |
| 1148 | * |
| 1149 | * Similar to the motivation for kernel cpu access it is again important that |
| 1150 | * the userspace code of a given importing subsystem can use the same |
| 1151 | * interfaces with a imported dma-buf buffer object as with a native buffer |
| 1152 | * object. This is especially important for drm where the userspace part of |
| 1153 | * contemporary OpenGL, X, and other drivers is huge, and reworking them to |
| 1154 | * use a different way to mmap a buffer rather invasive. |
| 1155 | * |
| 1156 | * The assumption in the current dma-buf interfaces is that redirecting the |
| 1157 | * initial mmap is all that's needed. A survey of some of the existing |
| 1158 | * subsystems shows that no driver seems to do any nefarious thing like |
| 1159 | * syncing up with outstanding asynchronous processing on the device or |
| 1160 | * allocating special resources at fault time. So hopefully this is good |
| 1161 | * enough, since adding interfaces to intercept pagefaults and allow pte |
| 1162 | * shootdowns would increase the complexity quite a bit. |
| 1163 | * |
| 1164 | * Interface:: |
Daniel Vetter | 85804b7 | 2020-12-11 16:58:41 +0100 | [diff] [blame] | 1165 | * |
Daniel Vetter | 0959a16 | 2016-12-09 19:53:08 +0100 | [diff] [blame] | 1166 | * int dma_buf_mmap(struct dma_buf \*, struct vm_area_struct \*, |
| 1167 | * unsigned long); |
| 1168 | * |
| 1169 | * If the importing subsystem simply provides a special-purpose mmap call to |
Daniel Vetter | 85804b7 | 2020-12-11 16:58:41 +0100 | [diff] [blame] | 1170 | * set up a mapping in userspace, calling do_mmap with &dma_buf.file will |
Daniel Vetter | 0959a16 | 2016-12-09 19:53:08 +0100 | [diff] [blame] | 1171 | * equally achieve that for a dma-buf object. |
| 1172 | */ |
| 1173 | |
Chris Wilson | ae4e46b | 2016-08-15 16:42:18 +0100 | [diff] [blame] | 1174 | static int __dma_buf_begin_cpu_access(struct dma_buf *dmabuf, |
| 1175 | enum dma_data_direction direction) |
| 1176 | { |
| 1177 | bool write = (direction == DMA_BIDIRECTIONAL || |
| 1178 | direction == DMA_TO_DEVICE); |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 1179 | struct dma_resv *resv = dmabuf->resv; |
Chris Wilson | ae4e46b | 2016-08-15 16:42:18 +0100 | [diff] [blame] | 1180 | long ret; |
| 1181 | |
| 1182 | /* Wait on any implicit rendering fences */ |
Christian König | d3fae3b | 2021-06-02 13:01:15 +0200 | [diff] [blame] | 1183 | ret = dma_resv_wait_timeout(resv, write, true, MAX_SCHEDULE_TIMEOUT); |
Chris Wilson | ae4e46b | 2016-08-15 16:42:18 +0100 | [diff] [blame] | 1184 | if (ret < 0) |
| 1185 | return ret; |
| 1186 | |
| 1187 | return 0; |
| 1188 | } |
Daniel Vetter | fc13020 | 2012-03-20 00:02:37 +0100 | [diff] [blame] | 1189 | |
| 1190 | /** |
| 1191 | * dma_buf_begin_cpu_access - Must be called before accessing a dma_buf from the |
| 1192 | * cpu in the kernel context. Calls begin_cpu_access to allow exporter-specific |
| 1193 | * preparations. Coherency is only guaranteed in the specified range for the |
| 1194 | * specified access direction. |
Randy Dunlap | efb4df82 | 2012-04-17 17:03:30 -0700 | [diff] [blame] | 1195 | * @dmabuf: [in] buffer to prepare cpu access for. |
Daniel Vetter | fc13020 | 2012-03-20 00:02:37 +0100 | [diff] [blame] | 1196 | * @direction: [in] length of range for cpu access. |
| 1197 | * |
Daniel Vetter | 0959a16 | 2016-12-09 19:53:08 +0100 | [diff] [blame] | 1198 | * After the cpu access is complete the caller should call |
| 1199 | * dma_buf_end_cpu_access(). Only when cpu access is braketed by both calls is |
| 1200 | * it guaranteed to be coherent with other DMA access. |
| 1201 | * |
Daniel Vetter | de9114e | 2020-12-11 16:58:40 +0100 | [diff] [blame] | 1202 | * This function will also wait for any DMA transactions tracked through |
| 1203 | * implicit synchronization in &dma_buf.resv. For DMA transactions with explicit |
| 1204 | * synchronization this function will only ensure cache coherency, callers must |
| 1205 | * ensure synchronization with such DMA transactions on their own. |
| 1206 | * |
Daniel Vetter | fc13020 | 2012-03-20 00:02:37 +0100 | [diff] [blame] | 1207 | * Can return negative error values, returns 0 on success. |
| 1208 | */ |
Tiago Vignatti | 831e9da | 2015-12-22 19:36:45 -0200 | [diff] [blame] | 1209 | int dma_buf_begin_cpu_access(struct dma_buf *dmabuf, |
Daniel Vetter | fc13020 | 2012-03-20 00:02:37 +0100 | [diff] [blame] | 1210 | enum dma_data_direction direction) |
| 1211 | { |
| 1212 | int ret = 0; |
| 1213 | |
| 1214 | if (WARN_ON(!dmabuf)) |
| 1215 | return -EINVAL; |
| 1216 | |
Daniel Vetter | 8ccf0a2 | 2020-12-14 18:16:22 +0100 | [diff] [blame] | 1217 | might_lock(&dmabuf->resv->lock.base); |
| 1218 | |
Daniel Vetter | fc13020 | 2012-03-20 00:02:37 +0100 | [diff] [blame] | 1219 | if (dmabuf->ops->begin_cpu_access) |
Tiago Vignatti | 831e9da | 2015-12-22 19:36:45 -0200 | [diff] [blame] | 1220 | ret = dmabuf->ops->begin_cpu_access(dmabuf, direction); |
Daniel Vetter | fc13020 | 2012-03-20 00:02:37 +0100 | [diff] [blame] | 1221 | |
Chris Wilson | ae4e46b | 2016-08-15 16:42:18 +0100 | [diff] [blame] | 1222 | /* Ensure that all fences are waited upon - but we first allow |
| 1223 | * the native handler the chance to do so more efficiently if it |
| 1224 | * chooses. A double invocation here will be reasonably cheap no-op. |
| 1225 | */ |
| 1226 | if (ret == 0) |
| 1227 | ret = __dma_buf_begin_cpu_access(dmabuf, direction); |
| 1228 | |
Daniel Vetter | fc13020 | 2012-03-20 00:02:37 +0100 | [diff] [blame] | 1229 | return ret; |
| 1230 | } |
| 1231 | EXPORT_SYMBOL_GPL(dma_buf_begin_cpu_access); |
| 1232 | |
| 1233 | /** |
| 1234 | * dma_buf_end_cpu_access - Must be called after accessing a dma_buf from the |
| 1235 | * cpu in the kernel context. Calls end_cpu_access to allow exporter-specific |
| 1236 | * actions. Coherency is only guaranteed in the specified range for the |
| 1237 | * specified access direction. |
Randy Dunlap | efb4df82 | 2012-04-17 17:03:30 -0700 | [diff] [blame] | 1238 | * @dmabuf: [in] buffer to complete cpu access for. |
Daniel Vetter | fc13020 | 2012-03-20 00:02:37 +0100 | [diff] [blame] | 1239 | * @direction: [in] length of range for cpu access. |
| 1240 | * |
Daniel Vetter | 0959a16 | 2016-12-09 19:53:08 +0100 | [diff] [blame] | 1241 | * This terminates CPU access started with dma_buf_begin_cpu_access(). |
| 1242 | * |
Daniel Vetter | 87e332d | 2016-03-21 08:24:22 +0100 | [diff] [blame] | 1243 | * Can return negative error values, returns 0 on success. |
Daniel Vetter | fc13020 | 2012-03-20 00:02:37 +0100 | [diff] [blame] | 1244 | */ |
Chris Wilson | 18b862d | 2016-03-18 20:02:39 +0000 | [diff] [blame] | 1245 | int dma_buf_end_cpu_access(struct dma_buf *dmabuf, |
| 1246 | enum dma_data_direction direction) |
Daniel Vetter | fc13020 | 2012-03-20 00:02:37 +0100 | [diff] [blame] | 1247 | { |
Chris Wilson | 18b862d | 2016-03-18 20:02:39 +0000 | [diff] [blame] | 1248 | int ret = 0; |
| 1249 | |
Daniel Vetter | fc13020 | 2012-03-20 00:02:37 +0100 | [diff] [blame] | 1250 | WARN_ON(!dmabuf); |
| 1251 | |
Daniel Vetter | 8ccf0a2 | 2020-12-14 18:16:22 +0100 | [diff] [blame] | 1252 | might_lock(&dmabuf->resv->lock.base); |
| 1253 | |
Daniel Vetter | fc13020 | 2012-03-20 00:02:37 +0100 | [diff] [blame] | 1254 | if (dmabuf->ops->end_cpu_access) |
Chris Wilson | 18b862d | 2016-03-18 20:02:39 +0000 | [diff] [blame] | 1255 | ret = dmabuf->ops->end_cpu_access(dmabuf, direction); |
| 1256 | |
| 1257 | return ret; |
Daniel Vetter | fc13020 | 2012-03-20 00:02:37 +0100 | [diff] [blame] | 1258 | } |
| 1259 | EXPORT_SYMBOL_GPL(dma_buf_end_cpu_access); |
| 1260 | |
Daniel Vetter | 4c78513 | 2012-04-24 14:38:52 +0530 | [diff] [blame] | 1261 | |
| 1262 | /** |
| 1263 | * dma_buf_mmap - Setup up a userspace mmap with the given vma |
Sumit Semwal | 12c4727 | 2012-05-23 15:27:40 +0530 | [diff] [blame] | 1264 | * @dmabuf: [in] buffer that should back the vma |
Daniel Vetter | 4c78513 | 2012-04-24 14:38:52 +0530 | [diff] [blame] | 1265 | * @vma: [in] vma for the mmap |
| 1266 | * @pgoff: [in] offset in pages where this mmap should start within the |
Jagan Teki | 5136629 | 2015-05-21 01:09:31 +0530 | [diff] [blame] | 1267 | * dma-buf buffer. |
Daniel Vetter | 4c78513 | 2012-04-24 14:38:52 +0530 | [diff] [blame] | 1268 | * |
| 1269 | * This function adjusts the passed in vma so that it points at the file of the |
Javier Martinez Canillas | ecf1dba | 2014-04-10 01:30:05 +0200 | [diff] [blame] | 1270 | * dma_buf operation. It also adjusts the starting pgoff and does bounds |
Daniel Vetter | 4c78513 | 2012-04-24 14:38:52 +0530 | [diff] [blame] | 1271 | * checking on the size of the vma. Then it calls the exporters mmap function to |
| 1272 | * set up the mapping. |
| 1273 | * |
| 1274 | * Can return negative error values, returns 0 on success. |
| 1275 | */ |
| 1276 | int dma_buf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma, |
| 1277 | unsigned long pgoff) |
| 1278 | { |
| 1279 | if (WARN_ON(!dmabuf || !vma)) |
| 1280 | return -EINVAL; |
| 1281 | |
Andrew F. Davis | e3a9d6c | 2019-03-29 11:52:01 -0500 | [diff] [blame] | 1282 | /* check if buffer supports mmap */ |
| 1283 | if (!dmabuf->ops->mmap) |
| 1284 | return -EINVAL; |
| 1285 | |
Daniel Vetter | 4c78513 | 2012-04-24 14:38:52 +0530 | [diff] [blame] | 1286 | /* check for offset overflow */ |
Muhammad Falak R Wani | b02da6f | 2016-05-23 17:08:42 +0530 | [diff] [blame] | 1287 | if (pgoff + vma_pages(vma) < pgoff) |
Daniel Vetter | 4c78513 | 2012-04-24 14:38:52 +0530 | [diff] [blame] | 1288 | return -EOVERFLOW; |
| 1289 | |
| 1290 | /* check for overflowing the buffer's size */ |
Muhammad Falak R Wani | b02da6f | 2016-05-23 17:08:42 +0530 | [diff] [blame] | 1291 | if (pgoff + vma_pages(vma) > |
Daniel Vetter | 4c78513 | 2012-04-24 14:38:52 +0530 | [diff] [blame] | 1292 | dmabuf->size >> PAGE_SHIFT) |
| 1293 | return -EINVAL; |
| 1294 | |
| 1295 | /* readjust the vma */ |
Christian König | 295992f | 2020-09-14 15:09:33 +0200 | [diff] [blame] | 1296 | vma_set_file(vma, dmabuf->file); |
Daniel Vetter | 4c78513 | 2012-04-24 14:38:52 +0530 | [diff] [blame] | 1297 | vma->vm_pgoff = pgoff; |
| 1298 | |
Christian König | 1527f92 | 2020-10-09 15:08:55 +0200 | [diff] [blame] | 1299 | return dmabuf->ops->mmap(dmabuf, vma); |
Daniel Vetter | 4c78513 | 2012-04-24 14:38:52 +0530 | [diff] [blame] | 1300 | } |
| 1301 | EXPORT_SYMBOL_GPL(dma_buf_mmap); |
Dave Airlie | 98f86c9 | 2012-05-20 12:33:56 +0530 | [diff] [blame] | 1302 | |
| 1303 | /** |
Sumit Semwal | 12c4727 | 2012-05-23 15:27:40 +0530 | [diff] [blame] | 1304 | * dma_buf_vmap - Create virtual mapping for the buffer object into kernel |
| 1305 | * address space. Same restrictions as for vmap and friends apply. |
| 1306 | * @dmabuf: [in] buffer to vmap |
Thomas Zimmermann | 6619ccf | 2020-09-25 13:55:59 +0200 | [diff] [blame] | 1307 | * @map: [out] returns the vmap pointer |
Dave Airlie | 98f86c9 | 2012-05-20 12:33:56 +0530 | [diff] [blame] | 1308 | * |
| 1309 | * This call may fail due to lack of virtual mapping address space. |
| 1310 | * These calls are optional in drivers. The intended use for them |
| 1311 | * is for mapping objects linear in kernel space for high use objects. |
Daniel Vetter | de9114e | 2020-12-11 16:58:40 +0100 | [diff] [blame] | 1312 | * |
| 1313 | * To ensure coherency users must call dma_buf_begin_cpu_access() and |
| 1314 | * dma_buf_end_cpu_access() around any cpu access performed through this |
| 1315 | * mapping. |
Colin Cross | fee0c54 | 2013-12-20 16:43:50 -0800 | [diff] [blame] | 1316 | * |
Thomas Zimmermann | 6619ccf | 2020-09-25 13:55:59 +0200 | [diff] [blame] | 1317 | * Returns 0 on success, or a negative errno code otherwise. |
Dave Airlie | 98f86c9 | 2012-05-20 12:33:56 +0530 | [diff] [blame] | 1318 | */ |
Thomas Zimmermann | 6619ccf | 2020-09-25 13:55:59 +0200 | [diff] [blame] | 1319 | int dma_buf_vmap(struct dma_buf *dmabuf, struct dma_buf_map *map) |
Dave Airlie | 98f86c9 | 2012-05-20 12:33:56 +0530 | [diff] [blame] | 1320 | { |
Thomas Zimmermann | 6619ccf | 2020-09-25 13:55:59 +0200 | [diff] [blame] | 1321 | struct dma_buf_map ptr; |
| 1322 | int ret = 0; |
| 1323 | |
| 1324 | dma_buf_map_clear(map); |
Daniel Vetter | f00b4da | 2012-12-20 14:14:23 +0100 | [diff] [blame] | 1325 | |
Dave Airlie | 98f86c9 | 2012-05-20 12:33:56 +0530 | [diff] [blame] | 1326 | if (WARN_ON(!dmabuf)) |
Thomas Zimmermann | 6619ccf | 2020-09-25 13:55:59 +0200 | [diff] [blame] | 1327 | return -EINVAL; |
Dave Airlie | 98f86c9 | 2012-05-20 12:33:56 +0530 | [diff] [blame] | 1328 | |
Daniel Vetter | f00b4da | 2012-12-20 14:14:23 +0100 | [diff] [blame] | 1329 | if (!dmabuf->ops->vmap) |
Thomas Zimmermann | 6619ccf | 2020-09-25 13:55:59 +0200 | [diff] [blame] | 1330 | return -EINVAL; |
Daniel Vetter | f00b4da | 2012-12-20 14:14:23 +0100 | [diff] [blame] | 1331 | |
| 1332 | mutex_lock(&dmabuf->lock); |
| 1333 | if (dmabuf->vmapping_counter) { |
| 1334 | dmabuf->vmapping_counter++; |
Thomas Zimmermann | 01fd30d | 2020-09-25 13:55:58 +0200 | [diff] [blame] | 1335 | BUG_ON(dma_buf_map_is_null(&dmabuf->vmap_ptr)); |
Thomas Zimmermann | 6619ccf | 2020-09-25 13:55:59 +0200 | [diff] [blame] | 1336 | *map = dmabuf->vmap_ptr; |
Daniel Vetter | f00b4da | 2012-12-20 14:14:23 +0100 | [diff] [blame] | 1337 | goto out_unlock; |
| 1338 | } |
| 1339 | |
Thomas Zimmermann | 01fd30d | 2020-09-25 13:55:58 +0200 | [diff] [blame] | 1340 | BUG_ON(dma_buf_map_is_set(&dmabuf->vmap_ptr)); |
Daniel Vetter | f00b4da | 2012-12-20 14:14:23 +0100 | [diff] [blame] | 1341 | |
Thomas Zimmermann | 6619ccf | 2020-09-25 13:55:59 +0200 | [diff] [blame] | 1342 | ret = dmabuf->ops->vmap(dmabuf, &ptr); |
| 1343 | if (WARN_ON_ONCE(ret)) |
Daniel Vetter | f00b4da | 2012-12-20 14:14:23 +0100 | [diff] [blame] | 1344 | goto out_unlock; |
| 1345 | |
Thomas Zimmermann | 6619ccf | 2020-09-25 13:55:59 +0200 | [diff] [blame] | 1346 | dmabuf->vmap_ptr = ptr; |
Daniel Vetter | f00b4da | 2012-12-20 14:14:23 +0100 | [diff] [blame] | 1347 | dmabuf->vmapping_counter = 1; |
| 1348 | |
Thomas Zimmermann | 6619ccf | 2020-09-25 13:55:59 +0200 | [diff] [blame] | 1349 | *map = dmabuf->vmap_ptr; |
| 1350 | |
Daniel Vetter | f00b4da | 2012-12-20 14:14:23 +0100 | [diff] [blame] | 1351 | out_unlock: |
| 1352 | mutex_unlock(&dmabuf->lock); |
Thomas Zimmermann | 6619ccf | 2020-09-25 13:55:59 +0200 | [diff] [blame] | 1353 | return ret; |
Dave Airlie | 98f86c9 | 2012-05-20 12:33:56 +0530 | [diff] [blame] | 1354 | } |
| 1355 | EXPORT_SYMBOL_GPL(dma_buf_vmap); |
| 1356 | |
| 1357 | /** |
| 1358 | * dma_buf_vunmap - Unmap a vmap obtained by dma_buf_vmap. |
Sumit Semwal | 12c4727 | 2012-05-23 15:27:40 +0530 | [diff] [blame] | 1359 | * @dmabuf: [in] buffer to vunmap |
Thomas Zimmermann | 20e76f1 | 2020-09-25 13:56:00 +0200 | [diff] [blame] | 1360 | * @map: [in] vmap pointer to vunmap |
Dave Airlie | 98f86c9 | 2012-05-20 12:33:56 +0530 | [diff] [blame] | 1361 | */ |
Thomas Zimmermann | 20e76f1 | 2020-09-25 13:56:00 +0200 | [diff] [blame] | 1362 | void dma_buf_vunmap(struct dma_buf *dmabuf, struct dma_buf_map *map) |
Dave Airlie | 98f86c9 | 2012-05-20 12:33:56 +0530 | [diff] [blame] | 1363 | { |
| 1364 | if (WARN_ON(!dmabuf)) |
| 1365 | return; |
| 1366 | |
Thomas Zimmermann | 01fd30d | 2020-09-25 13:55:58 +0200 | [diff] [blame] | 1367 | BUG_ON(dma_buf_map_is_null(&dmabuf->vmap_ptr)); |
Daniel Vetter | f00b4da | 2012-12-20 14:14:23 +0100 | [diff] [blame] | 1368 | BUG_ON(dmabuf->vmapping_counter == 0); |
Thomas Zimmermann | 20e76f1 | 2020-09-25 13:56:00 +0200 | [diff] [blame] | 1369 | BUG_ON(!dma_buf_map_is_equal(&dmabuf->vmap_ptr, map)); |
Daniel Vetter | f00b4da | 2012-12-20 14:14:23 +0100 | [diff] [blame] | 1370 | |
| 1371 | mutex_lock(&dmabuf->lock); |
| 1372 | if (--dmabuf->vmapping_counter == 0) { |
| 1373 | if (dmabuf->ops->vunmap) |
Thomas Zimmermann | 20e76f1 | 2020-09-25 13:56:00 +0200 | [diff] [blame] | 1374 | dmabuf->ops->vunmap(dmabuf, map); |
Thomas Zimmermann | 01fd30d | 2020-09-25 13:55:58 +0200 | [diff] [blame] | 1375 | dma_buf_map_clear(&dmabuf->vmap_ptr); |
Daniel Vetter | f00b4da | 2012-12-20 14:14:23 +0100 | [diff] [blame] | 1376 | } |
| 1377 | mutex_unlock(&dmabuf->lock); |
Dave Airlie | 98f86c9 | 2012-05-20 12:33:56 +0530 | [diff] [blame] | 1378 | } |
| 1379 | EXPORT_SYMBOL_GPL(dma_buf_vunmap); |
Sumit Semwal | b89e3563 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 1380 | |
| 1381 | #ifdef CONFIG_DEBUG_FS |
Mathias Krause | eb0b947 | 2016-06-19 14:31:29 +0200 | [diff] [blame] | 1382 | static int dma_buf_debug_show(struct seq_file *s, void *unused) |
Sumit Semwal | b89e3563 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 1383 | { |
Sumit Semwal | b89e3563 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 1384 | struct dma_buf *buf_obj; |
| 1385 | struct dma_buf_attachment *attach_obj; |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 1386 | struct dma_resv *robj; |
| 1387 | struct dma_resv_list *fobj; |
Russell King | 5eb2c72 | 2017-03-31 11:00:42 +0100 | [diff] [blame] | 1388 | struct dma_fence *fence; |
Russell King | 5eb2c72 | 2017-03-31 11:00:42 +0100 | [diff] [blame] | 1389 | int count = 0, attach_count, shared_count, i; |
Sumit Semwal | b89e3563 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 1390 | size_t size = 0; |
Christian König | 680753d | 2021-05-06 15:00:31 +0200 | [diff] [blame] | 1391 | int ret; |
Sumit Semwal | b89e3563 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 1392 | |
| 1393 | ret = mutex_lock_interruptible(&db_list.lock); |
| 1394 | |
| 1395 | if (ret) |
| 1396 | return ret; |
| 1397 | |
Sumit Semwal | c0b00a5 | 2014-02-03 15:09:12 +0530 | [diff] [blame] | 1398 | seq_puts(s, "\nDma-buf Objects:\n"); |
Greg Hackmann | ed63bb1 | 2019-06-13 15:34:06 -0700 | [diff] [blame] | 1399 | seq_printf(s, "%-8s\t%-8s\t%-8s\t%-8s\texp_name\t%-8s\n", |
| 1400 | "size", "flags", "mode", "count", "ino"); |
Sumit Semwal | b89e3563 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 1401 | |
| 1402 | list_for_each_entry(buf_obj, &db_list.head, list_node) { |
Sumit Semwal | b89e3563 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 1403 | |
Christian König | 15fd552 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 1404 | ret = dma_resv_lock_interruptible(buf_obj->resv, NULL); |
| 1405 | if (ret) |
Christian König | f45f57c | 2019-10-18 16:30:19 +0200 | [diff] [blame] | 1406 | goto error_unlock; |
Sumit Semwal | b89e3563 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 1407 | |
Greg Hackmann | bb2bb90 | 2019-06-13 15:34:07 -0700 | [diff] [blame] | 1408 | seq_printf(s, "%08zu\t%08x\t%08x\t%08ld\t%s\t%08lu\t%s\n", |
Sumit Semwal | c0b00a5 | 2014-02-03 15:09:12 +0530 | [diff] [blame] | 1409 | buf_obj->size, |
Sumit Semwal | b89e3563 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 1410 | buf_obj->file->f_flags, buf_obj->file->f_mode, |
Al Viro | a1f6dba | 2014-08-20 11:05:50 -0400 | [diff] [blame] | 1411 | file_count(buf_obj->file), |
Greg Hackmann | ed63bb1 | 2019-06-13 15:34:06 -0700 | [diff] [blame] | 1412 | buf_obj->exp_name, |
Greg Hackmann | bb2bb90 | 2019-06-13 15:34:07 -0700 | [diff] [blame] | 1413 | file_inode(buf_obj->file)->i_ino, |
| 1414 | buf_obj->name ?: ""); |
Sumit Semwal | b89e3563 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 1415 | |
Russell King | 5eb2c72 | 2017-03-31 11:00:42 +0100 | [diff] [blame] | 1416 | robj = buf_obj->resv; |
Christian König | 6edbd6a | 2021-05-10 16:14:09 +0200 | [diff] [blame] | 1417 | fence = dma_resv_excl_fence(robj); |
Russell King | 5eb2c72 | 2017-03-31 11:00:42 +0100 | [diff] [blame] | 1418 | if (fence) |
| 1419 | seq_printf(s, "\tExclusive fence: %s %s %ssignalled\n", |
| 1420 | fence->ops->get_driver_name(fence), |
| 1421 | fence->ops->get_timeline_name(fence), |
| 1422 | dma_fence_is_signaled(fence) ? "" : "un"); |
Christian König | 680753d | 2021-05-06 15:00:31 +0200 | [diff] [blame] | 1423 | |
| 1424 | fobj = rcu_dereference_protected(robj->fence, |
| 1425 | dma_resv_held(robj)); |
| 1426 | shared_count = fobj ? fobj->shared_count : 0; |
Russell King | 5eb2c72 | 2017-03-31 11:00:42 +0100 | [diff] [blame] | 1427 | for (i = 0; i < shared_count; i++) { |
Christian König | 680753d | 2021-05-06 15:00:31 +0200 | [diff] [blame] | 1428 | fence = rcu_dereference_protected(fobj->shared[i], |
| 1429 | dma_resv_held(robj)); |
Russell King | 5eb2c72 | 2017-03-31 11:00:42 +0100 | [diff] [blame] | 1430 | seq_printf(s, "\tShared fence: %s %s %ssignalled\n", |
| 1431 | fence->ops->get_driver_name(fence), |
| 1432 | fence->ops->get_timeline_name(fence), |
| 1433 | dma_fence_is_signaled(fence) ? "" : "un"); |
| 1434 | } |
Russell King | 5eb2c72 | 2017-03-31 11:00:42 +0100 | [diff] [blame] | 1435 | |
Sumit Semwal | c0b00a5 | 2014-02-03 15:09:12 +0530 | [diff] [blame] | 1436 | seq_puts(s, "\tAttached Devices:\n"); |
Sumit Semwal | b89e3563 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 1437 | attach_count = 0; |
| 1438 | |
| 1439 | list_for_each_entry(attach_obj, &buf_obj->attachments, node) { |
Markus Elfring | 9eddb41 | 2017-05-08 10:32:44 +0200 | [diff] [blame] | 1440 | seq_printf(s, "\t%s\n", dev_name(attach_obj->dev)); |
Sumit Semwal | b89e3563 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 1441 | attach_count++; |
| 1442 | } |
Christian König | 15fd552 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 1443 | dma_resv_unlock(buf_obj->resv); |
Sumit Semwal | b89e3563 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 1444 | |
Sumit Semwal | c0b00a5 | 2014-02-03 15:09:12 +0530 | [diff] [blame] | 1445 | seq_printf(s, "Total %d devices attached\n\n", |
Sumit Semwal | b89e3563 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 1446 | attach_count); |
| 1447 | |
| 1448 | count++; |
| 1449 | size += buf_obj->size; |
Sumit Semwal | b89e3563 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 1450 | } |
| 1451 | |
| 1452 | seq_printf(s, "\nTotal %d objects, %zu bytes\n", count, size); |
| 1453 | |
| 1454 | mutex_unlock(&db_list.lock); |
| 1455 | return 0; |
Christian König | 15fd552 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 1456 | |
Christian König | f45f57c | 2019-10-18 16:30:19 +0200 | [diff] [blame] | 1457 | error_unlock: |
Christian König | 15fd552 | 2018-07-03 16:42:26 +0200 | [diff] [blame] | 1458 | mutex_unlock(&db_list.lock); |
| 1459 | return ret; |
Sumit Semwal | b89e3563 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 1460 | } |
| 1461 | |
Yangtao Li | 2674305 | 2018-11-30 11:11:01 -0500 | [diff] [blame] | 1462 | DEFINE_SHOW_ATTRIBUTE(dma_buf_debug); |
Sumit Semwal | b89e3563 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 1463 | |
| 1464 | static struct dentry *dma_buf_debugfs_dir; |
| 1465 | |
| 1466 | static int dma_buf_init_debugfs(void) |
| 1467 | { |
Mathias Krause | bd3e220 | 2016-06-19 14:31:31 +0200 | [diff] [blame] | 1468 | struct dentry *d; |
Sumit Semwal | b89e3563 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 1469 | int err = 0; |
Jagan Teki | 5136629 | 2015-05-21 01:09:31 +0530 | [diff] [blame] | 1470 | |
Mathias Krause | bd3e220 | 2016-06-19 14:31:31 +0200 | [diff] [blame] | 1471 | d = debugfs_create_dir("dma_buf", NULL); |
| 1472 | if (IS_ERR(d)) |
| 1473 | return PTR_ERR(d); |
Jagan Teki | 5136629 | 2015-05-21 01:09:31 +0530 | [diff] [blame] | 1474 | |
Mathias Krause | bd3e220 | 2016-06-19 14:31:31 +0200 | [diff] [blame] | 1475 | dma_buf_debugfs_dir = d; |
Sumit Semwal | b89e3563 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 1476 | |
Mathias Krause | bd3e220 | 2016-06-19 14:31:31 +0200 | [diff] [blame] | 1477 | d = debugfs_create_file("bufinfo", S_IRUGO, dma_buf_debugfs_dir, |
| 1478 | NULL, &dma_buf_debug_fops); |
| 1479 | if (IS_ERR(d)) { |
Sumit Semwal | b89e3563 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 1480 | pr_debug("dma_buf: debugfs: failed to create node bufinfo\n"); |
Mathias Krause | b747999 | 2016-06-19 14:31:30 +0200 | [diff] [blame] | 1481 | debugfs_remove_recursive(dma_buf_debugfs_dir); |
| 1482 | dma_buf_debugfs_dir = NULL; |
Mathias Krause | bd3e220 | 2016-06-19 14:31:31 +0200 | [diff] [blame] | 1483 | err = PTR_ERR(d); |
Mathias Krause | b747999 | 2016-06-19 14:31:30 +0200 | [diff] [blame] | 1484 | } |
Sumit Semwal | b89e3563 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 1485 | |
| 1486 | return err; |
| 1487 | } |
| 1488 | |
| 1489 | static void dma_buf_uninit_debugfs(void) |
| 1490 | { |
Vasyl Gomonovych | 298b6a8 | 2017-11-22 16:22:41 +0100 | [diff] [blame] | 1491 | debugfs_remove_recursive(dma_buf_debugfs_dir); |
Sumit Semwal | b89e3563 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 1492 | } |
Sumit Semwal | b89e3563 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 1493 | #else |
| 1494 | static inline int dma_buf_init_debugfs(void) |
| 1495 | { |
| 1496 | return 0; |
| 1497 | } |
| 1498 | static inline void dma_buf_uninit_debugfs(void) |
| 1499 | { |
| 1500 | } |
| 1501 | #endif |
| 1502 | |
| 1503 | static int __init dma_buf_init(void) |
| 1504 | { |
Hridya Valsaraju | bdb8d06 | 2021-06-03 14:47:51 -0700 | [diff] [blame] | 1505 | int ret; |
| 1506 | |
| 1507 | ret = dma_buf_init_sysfs_statistics(); |
| 1508 | if (ret) |
| 1509 | return ret; |
| 1510 | |
Greg Hackmann | ed63bb1 | 2019-06-13 15:34:06 -0700 | [diff] [blame] | 1511 | dma_buf_mnt = kern_mount(&dma_buf_fs_type); |
| 1512 | if (IS_ERR(dma_buf_mnt)) |
| 1513 | return PTR_ERR(dma_buf_mnt); |
| 1514 | |
Sumit Semwal | b89e3563 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 1515 | mutex_init(&db_list.lock); |
| 1516 | INIT_LIST_HEAD(&db_list.head); |
| 1517 | dma_buf_init_debugfs(); |
| 1518 | return 0; |
| 1519 | } |
| 1520 | subsys_initcall(dma_buf_init); |
| 1521 | |
| 1522 | static void __exit dma_buf_deinit(void) |
| 1523 | { |
| 1524 | dma_buf_uninit_debugfs(); |
Greg Hackmann | ed63bb1 | 2019-06-13 15:34:06 -0700 | [diff] [blame] | 1525 | kern_unmount(dma_buf_mnt); |
Hridya Valsaraju | bdb8d06 | 2021-06-03 14:47:51 -0700 | [diff] [blame] | 1526 | dma_buf_uninit_sysfs_statistics(); |
Sumit Semwal | b89e3563 | 2013-04-04 11:44:37 +0530 | [diff] [blame] | 1527 | } |
| 1528 | __exitcall(dma_buf_deinit); |