| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 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 <[email protected]> |
| 7 |
* |
| 8 |
* Many thanks to linaro-mm-sig list, and specially |
| 9 |
* Arnd Bergmann <[email protected]>, Rob Clark <[email protected]> and |
| 10 |
* Daniel Vetter <[email protected]> for their support in creation and |
| 11 |
* refining of this idea. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include <linux/fs.h> |
| 15 |
#include <linux/slab.h> |
| 16 |
#include <linux/dma-buf.h> |
| 17 |
#include <linux/dma-fence.h> |
| 18 |
#include <linux/dma-fence-unwrap.h> |
| 19 |
#include <linux/anon_inodes.h> |
| 20 |
#include <linux/export.h> |
| 21 |
#include <linux/debugfs.h> |
| 22 |
#include <linux/list.h> |
| 23 |
#include <linux/module.h> |
| 24 |
#include <linux/mutex.h> |
| 25 |
#include <linux/seq_file.h> |
| 26 |
#include <linux/sync_file.h> |
| 27 |
#include <linux/poll.h> |
| 28 |
#include <linux/dma-resv.h> |
| 29 |
#include <linux/mm.h> |
| 30 |
#include <linux/mount.h> |
| 31 |
#include <linux/pseudo_fs.h> |
| 32 |
|
| 33 |
#include <uapi/linux/dma-buf.h> |
| 34 |
#include <uapi/linux/magic.h> |
| 35 |
|
| 36 |
#define CREATE_TRACE_POINTS |
| 37 |
#include <trace/events/dma_buf.h> |
| 38 |
|
| 39 |
/* |
| 40 |
* dmabuf->name must be accessed with holding dmabuf->name_lock. |
| 41 |
* we need to take the lock around the tracepoint call itself where |
| 42 |
* it is called in the code. |
| 43 |
* |
| 44 |
* Note: FUNC##_enabled() is a static branch that will only |
| 45 |
* be set when the trace event is enabled. |
| 46 |
*/ |
| 47 |
#define DMA_BUF_TRACE(FUNC, ...) \ |
| 48 |
do { \ |
| 49 |
/* Always expose lock if lockdep is enabled */ \ |
| 50 |
if (IS_ENABLED(CONFIG_LOCKDEP) || FUNC##_enabled()) { \ |
| 51 |
guard(spinlock)(&dmabuf->name_lock); \ |
| 52 |
FUNC(__VA_ARGS__); \ |
| 53 |
} \ |
| 54 |
} while (0) |
| 55 |
|
| 56 |
/* Wrapper to hide the sg_table page link from the importer */ |
| 57 |
struct dma_buf_sg_table_wrapper { |
| 58 |
struct sg_table *original; |
| 59 |
struct sg_table wrapper; |
| 60 |
}; |
| 61 |
|
| 62 |
static inline int is_dma_buf_file(struct file *); |
| 63 |
|
| 64 |
static DEFINE_MUTEX(dmabuf_list_mutex); |
| 65 |
static LIST_HEAD(dmabuf_list); |
| 66 |
|
| 67 |
static void __dma_buf_list_add(struct dma_buf *dmabuf) |
| 68 |
{ |
| 69 |
mutex_lock(&dmabuf_list_mutex); |
| 70 |
list_add(&dmabuf->list_node, &dmabuf_list); |
| 71 |
mutex_unlock(&dmabuf_list_mutex); |
| 72 |
} |
| 73 |
|
| 74 |
static void __dma_buf_list_del(struct dma_buf *dmabuf) |
| 75 |
{ |
| 76 |
if (!dmabuf) |
| 77 |
return; |
| 78 |
|
| 79 |
mutex_lock(&dmabuf_list_mutex); |
| 80 |
list_del(&dmabuf->list_node); |
| 81 |
mutex_unlock(&dmabuf_list_mutex); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* dma_buf_iter_begin - begin iteration through global list of all DMA buffers |
| 86 |
* |
| 87 |
* Returns the first buffer in the global list of DMA-bufs that's not in the |
| 88 |
* process of being destroyed. Increments that buffer's reference count to |
| 89 |
* prevent buffer destruction. Callers must release the reference, either by |
| 90 |
* continuing iteration with dma_buf_iter_next(), or with dma_buf_put(). |
| 91 |
* |
| 92 |
* Return: |
| 93 |
* * First buffer from global list, with refcount elevated |
| 94 |
* * NULL if no active buffers are present |
| 95 |
*/ |
| 96 |
struct dma_buf *dma_buf_iter_begin(void) |
| 97 |
{ |
| 98 |
struct dma_buf *ret = NULL, *dmabuf; |
| 99 |
|
| 100 |
/* |
| 101 |
* The list mutex does not protect a dmabuf's refcount, so it can be |
| 102 |
* zeroed while we are iterating. We cannot call get_dma_buf() since the |
| 103 |
* caller may not already own a reference to the buffer. |
| 104 |
*/ |
| 105 |
mutex_lock(&dmabuf_list_mutex); |
| 106 |
list_for_each_entry(dmabuf, &dmabuf_list, list_node) { |
| 107 |
if (file_ref_get(&dmabuf->file->f_ref)) { |
| 108 |
ret = dmabuf; |
| 109 |
break; |
| 110 |
} |
| 111 |
} |
| 112 |
mutex_unlock(&dmabuf_list_mutex); |
| 113 |
return ret; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* dma_buf_iter_next - continue iteration through global list of all DMA buffers |
| 118 |
* @dmabuf: [in] pointer to dma_buf |
| 119 |
* |
| 120 |
* Decrements the reference count on the provided buffer. Returns the next |
| 121 |
* buffer from the remainder of the global list of DMA-bufs with its reference |
| 122 |
* count incremented. Callers must release the reference, either by continuing |
| 123 |
* iteration with dma_buf_iter_next(), or with dma_buf_put(). |
| 124 |
* |
| 125 |
* Return: |
| 126 |
* * Next buffer from global list, with refcount elevated |
| 127 |
* * NULL if no additional active buffers are present |
| 128 |
*/ |
| 129 |
struct dma_buf *dma_buf_iter_next(struct dma_buf *dmabuf) |
| 130 |
{ |
| 131 |
struct dma_buf *ret = NULL; |
| 132 |
|
| 133 |
/* |
| 134 |
* The list mutex does not protect a dmabuf's refcount, so it can be |
| 135 |
* zeroed while we are iterating. We cannot call get_dma_buf() since the |
| 136 |
* caller may not already own a reference to the buffer. |
| 137 |
*/ |
| 138 |
mutex_lock(&dmabuf_list_mutex); |
| 139 |
dma_buf_put(dmabuf); |
| 140 |
list_for_each_entry_continue(dmabuf, &dmabuf_list, list_node) { |
| 141 |
if (file_ref_get(&dmabuf->file->f_ref)) { |
| 142 |
ret = dmabuf; |
| 143 |
break; |
| 144 |
} |
| 145 |
} |
| 146 |
mutex_unlock(&dmabuf_list_mutex); |
| 147 |
return ret; |
| 148 |
} |
| 149 |
|
| 150 |
static char *dmabuffs_dname(struct dentry *dentry, char *buffer, int buflen) |
| 151 |
{ |
| 152 |
struct dma_buf *dmabuf; |
| 153 |
char name[DMA_BUF_NAME_LEN]; |
| 154 |
ssize_t ret = 0; |
| 155 |
|
| 156 |
dmabuf = dentry->d_fsdata; |
| 157 |
spin_lock(&dmabuf->name_lock); |
| 158 |
if (dmabuf->name) |
| 159 |
ret = strscpy(name, dmabuf->name, sizeof(name)); |
| 160 |
spin_unlock(&dmabuf->name_lock); |
| 161 |
|
| 162 |
return dynamic_dname(buffer, buflen, "/%s:%s", |
| 163 |
dentry->d_name.name, ret > 0 ? name : ""); |
| 164 |
} |
| 165 |
|
| 166 |
static void dma_buf_release(struct dentry *dentry) |
| 167 |
{ |
| 168 |
struct dma_buf *dmabuf; |
| 169 |
|
| 170 |
dmabuf = dentry->d_fsdata; |
| 171 |
if (unlikely(!dmabuf)) |
| 172 |
return; |
| 173 |
|
| 174 |
BUG_ON(dmabuf->vmapping_counter); |
| 175 |
|
| 176 |
/* |
| 177 |
* If you hit this BUG() it could mean: |
| 178 |
* * There's a file reference imbalance in dma_buf_poll / dma_buf_poll_cb or somewhere else |
| 179 |
* * dmabuf->cb_in/out.active are non-0 despite no pending fence callback |
| 180 |
*/ |
| 181 |
BUG_ON(dmabuf->cb_in.active || dmabuf->cb_out.active); |
| 182 |
|
| 183 |
dmabuf->ops->release(dmabuf); |
| 184 |
|
| 185 |
if (dmabuf->resv == (struct dma_resv *)&dmabuf[1]) |
| 186 |
dma_resv_fini(dmabuf->resv); |
| 187 |
|
| 188 |
WARN_ON(!list_empty(&dmabuf->attachments)); |
| 189 |
module_put(dmabuf->owner); |
| 190 |
kfree(dmabuf->name); |
| 191 |
kfree(dmabuf); |
| 192 |
} |
| 193 |
|
| 194 |
static int dma_buf_file_release(struct inode *inode, struct file *file) |
| 195 |
{ |
| 196 |
if (!is_dma_buf_file(file)) |
| 197 |
return -EINVAL; |
| 198 |
|
| 199 |
__dma_buf_list_del(file->private_data); |
| 200 |
|
| 201 |
return 0; |
| 202 |
} |
| 203 |
|
| 204 |
static const struct dentry_operations dma_buf_dentry_ops = { |
| 205 |
.d_dname = dmabuffs_dname, |
| 206 |
.d_release = dma_buf_release, |
| 207 |
}; |
| 208 |
|
| 209 |
static struct vfsmount *dma_buf_mnt; |
| 210 |
|
| 211 |
static int dma_buf_fs_init_context(struct fs_context *fc) |
| 212 |
{ |
| 213 |
struct pseudo_fs_context *ctx; |
| 214 |
|
| 215 |
ctx = init_pseudo(fc, DMA_BUF_MAGIC); |
| 216 |
if (!ctx) |
| 217 |
return -ENOMEM; |
| 218 |
ctx->dops = &dma_buf_dentry_ops; |
| 219 |
return 0; |
| 220 |
} |
| 221 |
|
| 222 |
static struct file_system_type dma_buf_fs_type = { |
| 223 |
.name = "dmabuf", |
| 224 |
.init_fs_context = dma_buf_fs_init_context, |
| 225 |
.kill_sb = kill_anon_super, |
| 226 |
}; |
| 227 |
|
| 228 |
static int dma_buf_mmap_internal(struct file *file, struct vm_area_struct *vma) |
| 229 |
{ |
| 230 |
struct dma_buf *dmabuf; |
| 231 |
|
| 232 |
if (!is_dma_buf_file(file)) |
| 233 |
return -EINVAL; |
| 234 |
|
| 235 |
dmabuf = file->private_data; |
| 236 |
|
| 237 |
/* check if buffer supports mmap */ |
| 238 |
if (!dmabuf->ops->mmap) |
| 239 |
return -EINVAL; |
| 240 |
|
| 241 |
/* check for overflowing the buffer's size */ |
| 242 |
if (vma->vm_pgoff + vma_pages(vma) > |
| 243 |
dmabuf->size >> PAGE_SHIFT) |
| 244 |
return -EINVAL; |
| 245 |
|
| 246 |
DMA_BUF_TRACE(trace_dma_buf_mmap_internal, dmabuf); |
| 247 |
|
| 248 |
return dmabuf->ops->mmap(dmabuf, vma); |
| 249 |
} |
| 250 |
|
| 251 |
static loff_t dma_buf_llseek(struct file *file, loff_t offset, int whence) |
| 252 |
{ |
| 253 |
struct dma_buf *dmabuf; |
| 254 |
loff_t base; |
| 255 |
|
| 256 |
if (!is_dma_buf_file(file)) |
| 257 |
return -EBADF; |
| 258 |
|
| 259 |
dmabuf = file->private_data; |
| 260 |
|
| 261 |
/* only support discovering the end of the buffer, |
| 262 |
* but also allow SEEK_SET to maintain the idiomatic |
| 263 |
* SEEK_END(0), SEEK_CUR(0) pattern. |
| 264 |
*/ |
| 265 |
if (whence == SEEK_END) |
| 266 |
base = dmabuf->size; |
| 267 |
else if (whence == SEEK_SET) |
| 268 |
base = 0; |
| 269 |
else |
| 270 |
return -EINVAL; |
| 271 |
|
| 272 |
if (offset != 0) |
| 273 |
return -EINVAL; |
| 274 |
|
| 275 |
return base + offset; |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* DOC: implicit fence polling |
| 280 |
* |
| 281 |
* To support cross-device and cross-driver synchronization of buffer access |
| 282 |
* implicit fences (represented internally in the kernel with &struct dma_fence) |
| 283 |
* can be attached to a &dma_buf. The glue for that and a few related things are |
| 284 |
* provided in the &dma_resv structure. |
| 285 |
* |
| 286 |
* Userspace can query the state of these implicitly tracked fences using poll() |
| 287 |
* and related system calls: |
| 288 |
* |
| 289 |
* - Checking for EPOLLIN, i.e. read access, can be use to query the state of the |
| 290 |
* most recent write or exclusive fence. |
| 291 |
* |
| 292 |
* - Checking for EPOLLOUT, i.e. write access, can be used to query the state of |
| 293 |
* all attached fences, shared and exclusive ones. |
| 294 |
* |
| 295 |
* Note that this only signals the completion of the respective fences, i.e. the |
| 296 |
* DMA transfers are complete. Cache flushing and any other necessary |
| 297 |
* preparations before CPU access can begin still need to happen. |
| 298 |
* |
| 299 |
* As an alternative to poll(), the set of fences on DMA buffer can be |
| 300 |
* exported as a &sync_file using &dma_buf_sync_file_export. |
| 301 |
*/ |
| 302 |
|
| 303 |
static void dma_buf_poll_cb(struct dma_fence *fence, struct dma_fence_cb *cb) |
| 304 |
{ |
| 305 |
struct dma_buf_poll_cb_t *dcb = (struct dma_buf_poll_cb_t *)cb; |
| 306 |
struct dma_buf *dmabuf = container_of(dcb->poll, struct dma_buf, poll); |
| 307 |
unsigned long flags; |
| 308 |
|
| 309 |
spin_lock_irqsave(&dcb->poll->lock, flags); |
| 310 |
wake_up_locked_poll(dcb->poll, dcb->active); |
| 311 |
dcb->active = 0; |
| 312 |
spin_unlock_irqrestore(&dcb->poll->lock, flags); |
| 313 |
dma_fence_put(fence); |
| 314 |
/* Paired with get_file in dma_buf_poll */ |
| 315 |
fput(dmabuf->file); |
| 316 |
} |
| 317 |
|
| 318 |
static bool dma_buf_poll_add_cb(struct dma_resv *resv, bool write, |
| 319 |
struct dma_buf_poll_cb_t *dcb) |
| 320 |
{ |
| 321 |
struct dma_resv_iter cursor; |
| 322 |
struct dma_fence *fence; |
| 323 |
int r; |
| 324 |
|
| 325 |
dma_resv_for_each_fence(&cursor, resv, dma_resv_usage_rw(write), |
| 326 |
fence) { |
| 327 |
dma_fence_get(fence); |
| 328 |
r = dma_fence_add_callback(fence, &dcb->cb, dma_buf_poll_cb); |
| 329 |
if (!r) |
| 330 |
return true; |
| 331 |
dma_fence_put(fence); |
| 332 |
} |
| 333 |
|
| 334 |
return false; |
| 335 |
} |
| 336 |
|
| 337 |
static __poll_t dma_buf_poll(struct file *file, poll_table *poll) |
| 338 |
{ |
| 339 |
struct dma_buf *dmabuf; |
| 340 |
struct dma_resv *resv; |
| 341 |
__poll_t events; |
| 342 |
|
| 343 |
dmabuf = file->private_data; |
| 344 |
if (!dmabuf || !dmabuf->resv) |
| 345 |
return EPOLLERR; |
| 346 |
|
| 347 |
resv = dmabuf->resv; |
| 348 |
|
| 349 |
poll_wait(file, &dmabuf->poll, poll); |
| 350 |
|
| 351 |
events = poll_requested_events(poll) & (EPOLLIN | EPOLLOUT); |
| 352 |
if (!events) |
| 353 |
return 0; |
| 354 |
|
| 355 |
dma_resv_lock(resv, NULL); |
| 356 |
|
| 357 |
if (events & EPOLLOUT) { |
| 358 |
struct dma_buf_poll_cb_t *dcb = &dmabuf->cb_out; |
| 359 |
|
| 360 |
/* Check that callback isn't busy */ |
| 361 |
spin_lock_irq(&dmabuf->poll.lock); |
| 362 |
if (dcb->active) |
| 363 |
events &= ~EPOLLOUT; |
| 364 |
else |
| 365 |
dcb->active = EPOLLOUT; |
| 366 |
spin_unlock_irq(&dmabuf->poll.lock); |
| 367 |
|
| 368 |
if (events & EPOLLOUT) { |
| 369 |
/* Paired with fput in dma_buf_poll_cb */ |
| 370 |
get_file(dmabuf->file); |
| 371 |
|
| 372 |
if (!dma_buf_poll_add_cb(resv, true, dcb)) |
| 373 |
/* No callback queued, wake up any other waiters */ |
| 374 |
dma_buf_poll_cb(NULL, &dcb->cb); |
| 375 |
else |
| 376 |
events &= ~EPOLLOUT; |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
if (events & EPOLLIN) { |
| 381 |
struct dma_buf_poll_cb_t *dcb = &dmabuf->cb_in; |
| 382 |
|
| 383 |
/* Check that callback isn't busy */ |
| 384 |
spin_lock_irq(&dmabuf->poll.lock); |
| 385 |
if (dcb->active) |
| 386 |
events &= ~EPOLLIN; |
| 387 |
else |
| 388 |
dcb->active = EPOLLIN; |
| 389 |
spin_unlock_irq(&dmabuf->poll.lock); |
| 390 |
|
| 391 |
if (events & EPOLLIN) { |
| 392 |
/* Paired with fput in dma_buf_poll_cb */ |
| 393 |
get_file(dmabuf->file); |
| 394 |
|
| 395 |
if (!dma_buf_poll_add_cb(resv, false, dcb)) |
| 396 |
/* No callback queued, wake up any other waiters */ |
| 397 |
dma_buf_poll_cb(NULL, &dcb->cb); |
| 398 |
else |
| 399 |
events &= ~EPOLLIN; |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
dma_resv_unlock(resv); |
| 404 |
return events; |
| 405 |
} |
| 406 |
|
| 407 |
/** |
| 408 |
* dma_buf_set_name - Set a name to a specific dma_buf to track the usage. |
| 409 |
* It could support changing the name of the dma-buf if the same |
| 410 |
* piece of memory is used for multiple purpose between different devices. |
| 411 |
* |
| 412 |
* @dmabuf: [in] dmabuf buffer that will be renamed. |
| 413 |
* @buf: [in] A piece of userspace memory that contains the name of |
| 414 |
* the dma-buf. |
| 415 |
* |
| 416 |
* Returns 0 on success. If the dma-buf buffer is already attached to |
| 417 |
* devices, return -EBUSY. |
| 418 |
* |
| 419 |
*/ |
| 420 |
static long dma_buf_set_name(struct dma_buf *dmabuf, const char __user *buf) |
| 421 |
{ |
| 422 |
char *name = strndup_user(buf, DMA_BUF_NAME_LEN); |
| 423 |
|
| 424 |
if (IS_ERR(name)) |
| 425 |
return PTR_ERR(name); |
| 426 |
|
| 427 |
spin_lock(&dmabuf->name_lock); |
| 428 |
kfree(dmabuf->name); |
| 429 |
dmabuf->name = name; |
| 430 |
spin_unlock(&dmabuf->name_lock); |
| 431 |
|
| 432 |
return 0; |
| 433 |
} |
| 434 |
|
| 435 |
#if IS_ENABLED(CONFIG_SYNC_FILE) |
| 436 |
static long dma_buf_export_sync_file(struct dma_buf *dmabuf, |
| 437 |
void __user *user_data) |
| 438 |
{ |
| 439 |
struct dma_buf_export_sync_file arg; |
| 440 |
enum dma_resv_usage usage; |
| 441 |
struct dma_fence *fence = NULL; |
| 442 |
struct sync_file *sync_file; |
| 443 |
int fd, ret; |
| 444 |
|
| 445 |
if (copy_from_user(&arg, user_data, sizeof(arg))) |
| 446 |
return -EFAULT; |
| 447 |
|
| 448 |
if (arg.flags & ~DMA_BUF_SYNC_RW) |
| 449 |
return -EINVAL; |
| 450 |
|
| 451 |
if ((arg.flags & DMA_BUF_SYNC_RW) == 0) |
| 452 |
return -EINVAL; |
| 453 |
|
| 454 |
fd = get_unused_fd_flags(O_CLOEXEC); |
| 455 |
if (fd < 0) |
| 456 |
return fd; |
| 457 |
|
| 458 |
usage = dma_resv_usage_rw(arg.flags & DMA_BUF_SYNC_WRITE); |
| 459 |
ret = dma_resv_get_singleton(dmabuf->resv, usage, &fence); |
| 460 |
if (ret) |
| 461 |
goto err_put_fd; |
| 462 |
|
| 463 |
if (!fence) |
| 464 |
fence = dma_fence_get_stub(); |
| 465 |
|
| 466 |
sync_file = sync_file_create(fence); |
| 467 |
|
| 468 |
dma_fence_put(fence); |
| 469 |
|
| 470 |
if (!sync_file) { |
| 471 |
ret = -ENOMEM; |
| 472 |
goto err_put_fd; |
| 473 |
} |
| 474 |
|
| 475 |
arg.fd = fd; |
| 476 |
if (copy_to_user(user_data, &arg, sizeof(arg))) { |
| 477 |
ret = -EFAULT; |
| 478 |
goto err_put_file; |
| 479 |
} |
| 480 |
|
| 481 |
fd_install(fd, sync_file->file); |
| 482 |
|
| 483 |
return 0; |
| 484 |
|
| 485 |
err_put_file: |
| 486 |
fput(sync_file->file); |
| 487 |
err_put_fd: |
| 488 |
put_unused_fd(fd); |
| 489 |
return ret; |
| 490 |
} |
| 491 |
|
| 492 |
static long dma_buf_import_sync_file(struct dma_buf *dmabuf, |
| 493 |
const void __user *user_data) |
| 494 |
{ |
| 495 |
struct dma_buf_import_sync_file arg; |
| 496 |
struct dma_fence *fence, *f; |
| 497 |
enum dma_resv_usage usage; |
| 498 |
struct dma_fence_unwrap iter; |
| 499 |
unsigned int num_fences; |
| 500 |
int ret = 0; |
| 501 |
|
| 502 |
if (copy_from_user(&arg, user_data, sizeof(arg))) |
| 503 |
return -EFAULT; |
| 504 |
|
| 505 |
if (arg.flags & ~DMA_BUF_SYNC_RW) |
| 506 |
return -EINVAL; |
| 507 |
|
| 508 |
if ((arg.flags & DMA_BUF_SYNC_RW) == 0) |
| 509 |
return -EINVAL; |
| 510 |
|
| 511 |
fence = sync_file_get_fence(arg.fd); |
| 512 |
if (!fence) |
| 513 |
return -EINVAL; |
| 514 |
|
| 515 |
usage = (arg.flags & DMA_BUF_SYNC_WRITE) ? DMA_RESV_USAGE_WRITE : |
| 516 |
DMA_RESV_USAGE_READ; |
| 517 |
|
| 518 |
num_fences = 0; |
| 519 |
dma_fence_unwrap_for_each(f, &iter, fence) |
| 520 |
++num_fences; |
| 521 |
|
| 522 |
if (num_fences > 0) { |
| 523 |
dma_resv_lock(dmabuf->resv, NULL); |
| 524 |
|
| 525 |
ret = dma_resv_reserve_fences(dmabuf->resv, num_fences); |
| 526 |
if (!ret) { |
| 527 |
dma_fence_unwrap_for_each(f, &iter, fence) |
| 528 |
dma_resv_add_fence(dmabuf->resv, f, usage); |
| 529 |
} |
| 530 |
|
| 531 |
dma_resv_unlock(dmabuf->resv); |
| 532 |
} |
| 533 |
|
| 534 |
dma_fence_put(fence); |
| 535 |
|
| 536 |
return ret; |
| 537 |
} |
| 538 |
#endif |
| 539 |
|
| 540 |
static long dma_buf_ioctl(struct file *file, |
| 541 |
unsigned int cmd, unsigned long arg) |
| 542 |
{ |
| 543 |
struct dma_buf *dmabuf; |
| 544 |
struct dma_buf_sync sync; |
| 545 |
enum dma_data_direction direction; |
| 546 |
int ret; |
| 547 |
|
| 548 |
dmabuf = file->private_data; |
| 549 |
|
| 550 |
switch (cmd) { |
| 551 |
case DMA_BUF_IOCTL_SYNC: |
| 552 |
if (copy_from_user(&sync, (void __user *) arg, sizeof(sync))) |
| 553 |
return -EFAULT; |
| 554 |
|
| 555 |
if (sync.flags & ~DMA_BUF_SYNC_VALID_FLAGS_MASK) |
| 556 |
return -EINVAL; |
| 557 |
|
| 558 |
switch (sync.flags & DMA_BUF_SYNC_RW) { |
| 559 |
case DMA_BUF_SYNC_READ: |
| 560 |
direction = DMA_FROM_DEVICE; |
| 561 |
break; |
| 562 |
case DMA_BUF_SYNC_WRITE: |
| 563 |
direction = DMA_TO_DEVICE; |
| 564 |
break; |
| 565 |
case DMA_BUF_SYNC_RW: |
| 566 |
direction = DMA_BIDIRECTIONAL; |
| 567 |
break; |
| 568 |
default: |
| 569 |
return -EINVAL; |
| 570 |
} |
| 571 |
|
| 572 |
if (sync.flags & DMA_BUF_SYNC_END) |
| 573 |
ret = dma_buf_end_cpu_access(dmabuf, direction); |
| 574 |
else |
| 575 |
ret = dma_buf_begin_cpu_access(dmabuf, direction); |
| 576 |
|
| 577 |
return ret; |
| 578 |
|
| 579 |
case DMA_BUF_SET_NAME_A: |
| 580 |
case DMA_BUF_SET_NAME_B: |
| 581 |
return dma_buf_set_name(dmabuf, (const char __user *)arg); |
| 582 |
|
| 583 |
#if IS_ENABLED(CONFIG_SYNC_FILE) |
| 584 |
case DMA_BUF_IOCTL_EXPORT_SYNC_FILE: |
| 585 |
return dma_buf_export_sync_file(dmabuf, (void __user *)arg); |
| 586 |
case DMA_BUF_IOCTL_IMPORT_SYNC_FILE: |
| 587 |
return dma_buf_import_sync_file(dmabuf, (const void __user *)arg); |
| 588 |
#endif |
| 589 |
|
| 590 |
default: |
| 591 |
return -ENOTTY; |
| 592 |
} |
| 593 |
} |
| 594 |
|
| 595 |
static void dma_buf_show_fdinfo(struct seq_file *m, struct file *file) |
| 596 |
{ |
| 597 |
struct dma_buf *dmabuf = file->private_data; |
| 598 |
|
| 599 |
seq_printf(m, "size:\t%zu\n", dmabuf->size); |
| 600 |
/* Don't count the temporary reference taken inside procfs seq_show */ |
| 601 |
seq_printf(m, "count:\t%ld\n", file_count(dmabuf->file) - 1); |
| 602 |
seq_printf(m, "exp_name:\t%s\n", dmabuf->exp_name); |
| 603 |
spin_lock(&dmabuf->name_lock); |
| 604 |
if (dmabuf->name) |
| 605 |
seq_printf(m, "name:\t%s\n", dmabuf->name); |
| 606 |
spin_unlock(&dmabuf->name_lock); |
| 607 |
} |
| 608 |
|
| 609 |
static const struct file_operations dma_buf_fops = { |
| 610 |
.release = dma_buf_file_release, |
| 611 |
.mmap = dma_buf_mmap_internal, |
| 612 |
.llseek = dma_buf_llseek, |
| 613 |
.poll = dma_buf_poll, |
| 614 |
.unlocked_ioctl = dma_buf_ioctl, |
| 615 |
.compat_ioctl = compat_ptr_ioctl, |
| 616 |
.show_fdinfo = dma_buf_show_fdinfo, |
| 617 |
}; |
| 618 |
|
| 619 |
/* |
| 620 |
* is_dma_buf_file - Check if struct file* is associated with dma_buf |
| 621 |
*/ |
| 622 |
static inline int is_dma_buf_file(struct file *file) |
| 623 |
{ |
| 624 |
return file->f_op == &dma_buf_fops; |
| 625 |
} |
| 626 |
|
| 627 |
static struct file *dma_buf_getfile(size_t size, int flags) |
| 628 |
{ |
| 629 |
static atomic64_t dmabuf_inode = ATOMIC64_INIT(0); |
| 630 |
struct inode *inode = alloc_anon_inode(dma_buf_mnt->mnt_sb); |
| 631 |
struct file *file; |
| 632 |
|
| 633 |
if (IS_ERR(inode)) |
| 634 |
return ERR_CAST(inode); |
| 635 |
|
| 636 |
inode->i_size = size; |
| 637 |
inode_set_bytes(inode, size); |
| 638 |
|
| 639 |
/* |
| 640 |
* The ->i_ino acquired from get_next_ino() is not unique thus |
| 641 |
* not suitable for using it as dentry name by dmabuf stats. |
| 642 |
* Override ->i_ino with the unique and dmabuffs specific |
| 643 |
* value. |
| 644 |
*/ |
| 645 |
inode->i_ino = atomic64_inc_return(&dmabuf_inode); |
| 646 |
flags &= O_ACCMODE | O_NONBLOCK; |
| 647 |
file = alloc_file_pseudo(inode, dma_buf_mnt, "dmabuf", |
| 648 |
flags, &dma_buf_fops); |
| 649 |
if (IS_ERR(file)) |
| 650 |
goto err_alloc_file; |
| 651 |
|
| 652 |
return file; |
| 653 |
|
| 654 |
err_alloc_file: |
| 655 |
iput(inode); |
| 656 |
return file; |
| 657 |
} |
| 658 |
|
| 659 |
/** |
| 660 |
* DOC: dma buf device access |
| 661 |
* |
| 662 |
* For device DMA access to a shared DMA buffer the usual sequence of operations |
| 663 |
* is fairly simple: |
| 664 |
* |
| 665 |
* 1. The exporter defines his exporter instance using |
| 666 |
* DEFINE_DMA_BUF_EXPORT_INFO() and calls dma_buf_export() to wrap a private |
| 667 |
* buffer object into a &dma_buf. It then exports that &dma_buf to userspace |
| 668 |
* as a file descriptor by calling dma_buf_fd(). |
| 669 |
* |
| 670 |
* 2. Userspace passes this file-descriptors to all drivers it wants this buffer |
| 671 |
* to share with: First the file descriptor is converted to a &dma_buf using |
| 672 |
* dma_buf_get(). Then the buffer is attached to the device using |
| 673 |
* dma_buf_attach(). |
| 674 |
* |
| 675 |
* Up to this stage the exporter is still free to migrate or reallocate the |
| 676 |
* backing storage. |
| 677 |
* |
| 678 |
* 3. Once the buffer is attached to all devices userspace can initiate DMA |
| 679 |
* access to the shared buffer. In the kernel this is done by calling |
| 680 |
* dma_buf_map_attachment() and dma_buf_unmap_attachment(). |
| 681 |
* |
| 682 |
* 4. Once a driver is done with a shared buffer it needs to call |
| 683 |
* dma_buf_detach() (after cleaning up any mappings) and then release the |
| 684 |
* reference acquired with dma_buf_get() by calling dma_buf_put(). |
| 685 |
* |
| 686 |
* For the detailed semantics exporters are expected to implement see |
| 687 |
* &dma_buf_ops. |
| 688 |
*/ |
| 689 |
|
| 690 |
/** |
| 691 |
* dma_buf_export - Creates a new dma_buf, and associates an anon file |
| 692 |
* with this buffer, so it can be exported. |
| 693 |
* Also connect the allocator specific data and ops to the buffer. |
| 694 |
* Additionally, provide a name string for exporter; useful in debugging. |
| 695 |
* |
| 696 |
* @exp_info: [in] holds all the export related information provided |
| 697 |
* by the exporter. see &struct dma_buf_export_info |
| 698 |
* for further details. |
| 699 |
* |
| 700 |
* Returns, on success, a newly created struct dma_buf object, which wraps the |
| 701 |
* supplied private data and operations for struct dma_buf_ops. On either |
| 702 |
* missing ops, or error in allocating struct dma_buf, will return negative |
| 703 |
* error. |
| 704 |
* |
| 705 |
* For most cases the easiest way to create @exp_info is through the |
| 706 |
* %DEFINE_DMA_BUF_EXPORT_INFO macro. |
| 707 |
*/ |
| 708 |
struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info) |
| 709 |
{ |
| 710 |
struct dma_buf *dmabuf; |
| 711 |
struct dma_resv *resv = exp_info->resv; |
| 712 |
struct file *file; |
| 713 |
size_t alloc_size = sizeof(struct dma_buf); |
| 714 |
int ret; |
| 715 |
|
| 716 |
if (WARN_ON(!exp_info->priv || !exp_info->ops |
| 717 |
|| !exp_info->ops->map_dma_buf |
| 718 |
|| !exp_info->ops->unmap_dma_buf |
| 719 |
|| !exp_info->ops->release)) |
| 720 |
return ERR_PTR(-EINVAL); |
| 721 |
|
| 722 |
if (WARN_ON(!exp_info->ops->pin != !exp_info->ops->unpin)) |
| 723 |
return ERR_PTR(-EINVAL); |
| 724 |
|
| 725 |
if (!try_module_get(exp_info->owner)) |
| 726 |
return ERR_PTR(-ENOENT); |
| 727 |
|
| 728 |
file = dma_buf_getfile(exp_info->size, exp_info->flags); |
| 729 |
if (IS_ERR(file)) { |
| 730 |
ret = PTR_ERR(file); |
| 731 |
goto err_module; |
| 732 |
} |
| 733 |
|
| 734 |
if (!exp_info->resv) |
| 735 |
alloc_size += sizeof(struct dma_resv); |
| 736 |
else |
| 737 |
/* prevent &dma_buf[1] == dma_buf->resv */ |
| 738 |
alloc_size += 1; |
| 739 |
dmabuf = kzalloc(alloc_size, GFP_KERNEL); |
| 740 |
if (!dmabuf) { |
| 741 |
ret = -ENOMEM; |
| 742 |
goto err_file; |
| 743 |
} |
| 744 |
|
| 745 |
dmabuf->priv = exp_info->priv; |
| 746 |
dmabuf->ops = exp_info->ops; |
| 747 |
dmabuf->size = exp_info->size; |
| 748 |
dmabuf->exp_name = exp_info->exp_name; |
| 749 |
dmabuf->owner = exp_info->owner; |
| 750 |
spin_lock_init(&dmabuf->name_lock); |
| 751 |
init_waitqueue_head(&dmabuf->poll); |
| 752 |
dmabuf->cb_in.poll = dmabuf->cb_out.poll = &dmabuf->poll; |
| 753 |
dmabuf->cb_in.active = dmabuf->cb_out.active = 0; |
| 754 |
INIT_LIST_HEAD(&dmabuf->attachments); |
| 755 |
|
| 756 |
if (!resv) { |
| 757 |
dmabuf->resv = (struct dma_resv *)&dmabuf[1]; |
| 758 |
dma_resv_init(dmabuf->resv); |
| 759 |
} else { |
| 760 |
dmabuf->resv = resv; |
| 761 |
} |
| 762 |
|
| 763 |
file->private_data = dmabuf; |
| 764 |
file->f_path.dentry->d_fsdata = dmabuf; |
| 765 |
dmabuf->file = file; |
| 766 |
|
| 767 |
__dma_buf_list_add(dmabuf); |
| 768 |
|
| 769 |
DMA_BUF_TRACE(trace_dma_buf_export, dmabuf); |
| 770 |
|
| 771 |
return dmabuf; |
| 772 |
|
| 773 |
err_file: |
| 774 |
fput(file); |
| 775 |
err_module: |
| 776 |
module_put(exp_info->owner); |
| 777 |
return ERR_PTR(ret); |
| 778 |
} |
| 779 |
EXPORT_SYMBOL_NS_GPL(dma_buf_export, "DMA_BUF"); |
| 780 |
|
| 781 |
/** |
| 782 |
* dma_buf_fd - returns a file descriptor for the given struct dma_buf |
| 783 |
* @dmabuf: [in] pointer to dma_buf for which fd is required. |
| 784 |
* @flags: [in] flags to give to fd |
| 785 |
* |
| 786 |
* On success, returns an associated 'fd'. Else, returns error. |
| 787 |
*/ |
| 788 |
int dma_buf_fd(struct dma_buf *dmabuf, int flags) |
| 789 |
{ |
| 790 |
int fd; |
| 791 |
|
| 792 |
if (!dmabuf || !dmabuf->file) |
| 793 |
return -EINVAL; |
| 794 |
|
| 795 |
fd = get_unused_fd_flags(flags); |
| 796 |
if (fd < 0) |
| 797 |
return fd; |
| 798 |
|
| 799 |
DMA_BUF_TRACE(trace_dma_buf_fd, dmabuf, fd); |
| 800 |
|
| 801 |
fd_install(fd, dmabuf->file); |
| 802 |
return fd; |
| 803 |
} |
| 804 |
EXPORT_SYMBOL_NS_GPL(dma_buf_fd, "DMA_BUF"); |
| 805 |
|
| 806 |
/** |
| 807 |
* dma_buf_get - returns the struct dma_buf related to an fd |
| 808 |
* @fd: [in] fd associated with the struct dma_buf to be returned |
| 809 |
* |
| 810 |
* On success, returns the struct dma_buf associated with an fd; uses |
| 811 |
* file's refcounting done by fget to increase refcount. returns ERR_PTR |
| 812 |
* otherwise. |
| 813 |
*/ |
| 814 |
struct dma_buf *dma_buf_get(int fd) |
| 815 |
{ |
| 816 |
struct file *file; |
| 817 |
struct dma_buf *dmabuf; |
| 818 |
|
| 819 |
file = fget(fd); |
| 820 |
|
| 821 |
if (!file) |
| 822 |
return ERR_PTR(-EBADF); |
| 823 |
|
| 824 |
if (!is_dma_buf_file(file)) { |
| 825 |
fput(file); |
| 826 |
return ERR_PTR(-EINVAL); |
| 827 |
} |
| 828 |
|
| 829 |
dmabuf = file->private_data; |
| 830 |
|
| 831 |
DMA_BUF_TRACE(trace_dma_buf_get, dmabuf, fd); |
| 832 |
|
| 833 |
return dmabuf; |
| 834 |
} |
| 835 |
EXPORT_SYMBOL_NS_GPL(dma_buf_get, "DMA_BUF"); |
| 836 |
|
| 837 |
/** |
| 838 |
* dma_buf_put - decreases refcount of the buffer |
| 839 |
* @dmabuf: [in] buffer to reduce refcount of |
| 840 |
* |
| 841 |
* Uses file's refcounting done implicitly by fput(). |
| 842 |
* |
| 843 |
* If, as a result of this call, the refcount becomes 0, the 'release' file |
| 844 |
* operation related to this fd is called. It calls &dma_buf_ops.release vfunc |
| 845 |
* in turn, and frees the memory allocated for dmabuf when exported. |
| 846 |
*/ |
| 847 |
void dma_buf_put(struct dma_buf *dmabuf) |
| 848 |
{ |
| 849 |
if (WARN_ON(!dmabuf || !dmabuf->file)) |
| 850 |
return; |
| 851 |
|
| 852 |
fput(dmabuf->file); |
| 853 |
|
| 854 |
DMA_BUF_TRACE(trace_dma_buf_put, dmabuf); |
| 855 |
} |
| 856 |
EXPORT_SYMBOL_NS_GPL(dma_buf_put, "DMA_BUF"); |
| 857 |
|
| 858 |
static int dma_buf_wrap_sg_table(struct sg_table **sg_table) |
| 859 |
{ |
| 860 |
struct scatterlist *to_sg, *from_sg; |
| 861 |
struct sg_table *from = *sg_table; |
| 862 |
struct dma_buf_sg_table_wrapper *to; |
| 863 |
int i, ret; |
| 864 |
|
| 865 |
if (!IS_ENABLED(CONFIG_DMABUF_DEBUG)) |
| 866 |
return 0; |
| 867 |
|
| 868 |
/* |
| 869 |
* To catch abuse of the underlying struct page by importers copy the |
| 870 |
* sg_table without copying the page_link and give only the copy back to |
| 871 |
* the importer. |
| 872 |
*/ |
| 873 |
to = kzalloc_obj(*to); |
| 874 |
if (!to) |
| 875 |
return -ENOMEM; |
| 876 |
|
| 877 |
ret = sg_alloc_table(&to->wrapper, from->nents, GFP_KERNEL); |
| 878 |
if (ret) |
| 879 |
goto free_to; |
| 880 |
|
| 881 |
to_sg = to->wrapper.sgl; |
| 882 |
for_each_sgtable_dma_sg(from, from_sg, i) { |
| 883 |
to_sg->offset = 0; |
| 884 |
to_sg->length = 0; |
| 885 |
sg_assign_page(to_sg, NULL); |
| 886 |
sg_dma_address(to_sg) = sg_dma_address(from_sg); |
| 887 |
sg_dma_len(to_sg) = sg_dma_len(from_sg); |
| 888 |
to_sg = sg_next(to_sg); |
| 889 |
} |
| 890 |
|
| 891 |
to->original = from; |
| 892 |
*sg_table = &to->wrapper; |
| 893 |
return 0; |
| 894 |
|
| 895 |
free_to: |
| 896 |
kfree(to); |
| 897 |
return ret; |
| 898 |
} |
| 899 |
|
| 900 |
static void dma_buf_unwrap_sg_table(struct sg_table **sg_table) |
| 901 |
{ |
| 902 |
struct dma_buf_sg_table_wrapper *copy; |
| 903 |
|
| 904 |
if (!IS_ENABLED(CONFIG_DMABUF_DEBUG)) |
| 905 |
return; |
| 906 |
|
| 907 |
copy = container_of(*sg_table, typeof(*copy), wrapper); |
| 908 |
*sg_table = copy->original; |
| 909 |
sg_free_table(©->wrapper); |
| 910 |
kfree(copy); |
| 911 |
} |
| 912 |
|
| 913 |
static inline bool |
| 914 |
dma_buf_attachment_is_dynamic(struct dma_buf_attachment *attach) |
| 915 |
{ |
| 916 |
return !!attach->importer_ops; |
| 917 |
} |
| 918 |
|
| 919 |
static bool |
| 920 |
dma_buf_pin_on_map(struct dma_buf_attachment *attach) |
| 921 |
{ |
| 922 |
return attach->dmabuf->ops->pin && |
| 923 |
(!dma_buf_attachment_is_dynamic(attach) || |
| 924 |
!IS_ENABLED(CONFIG_DMABUF_MOVE_NOTIFY)); |
| 925 |
} |
| 926 |
|
| 927 |
/** |
| 928 |
* DOC: locking convention |
| 929 |
* |
| 930 |
* In order to avoid deadlock situations between dma-buf exports and importers, |
| 931 |
* all dma-buf API users must follow the common dma-buf locking convention. |
| 932 |
* |
| 933 |
* Convention for importers |
| 934 |
* |
| 935 |
* 1. Importers must hold the dma-buf reservation lock when calling these |
| 936 |
* functions: |
| 937 |
* |
| 938 |
* - dma_buf_pin() |
| 939 |
* - dma_buf_unpin() |
| 940 |
* - dma_buf_map_attachment() |
| 941 |
* - dma_buf_unmap_attachment() |
| 942 |
* - dma_buf_vmap() |
| 943 |
* - dma_buf_vunmap() |
| 944 |
* |
| 945 |
* 2. Importers must not hold the dma-buf reservation lock when calling these |
| 946 |
* functions: |
| 947 |
* |
| 948 |
* - dma_buf_attach() |
| 949 |
* - dma_buf_dynamic_attach() |
| 950 |
* - dma_buf_detach() |
| 951 |
* - dma_buf_export() |
| 952 |
* - dma_buf_fd() |
| 953 |
* - dma_buf_get() |
| 954 |
* - dma_buf_put() |
| 955 |
* - dma_buf_mmap() |
| 956 |
* - dma_buf_begin_cpu_access() |
| 957 |
* - dma_buf_end_cpu_access() |
| 958 |
* - dma_buf_map_attachment_unlocked() |
| 959 |
* - dma_buf_unmap_attachment_unlocked() |
| 960 |
* - dma_buf_vmap_unlocked() |
| 961 |
* - dma_buf_vunmap_unlocked() |
| 962 |
* |
| 963 |
* Convention for exporters |
| 964 |
* |
| 965 |
* 1. These &dma_buf_ops callbacks are invoked with unlocked dma-buf |
| 966 |
* reservation and exporter can take the lock: |
| 967 |
* |
| 968 |
* - &dma_buf_ops.attach() |
| 969 |
* - &dma_buf_ops.detach() |
| 970 |
* - &dma_buf_ops.release() |
| 971 |
* - &dma_buf_ops.begin_cpu_access() |
| 972 |
* - &dma_buf_ops.end_cpu_access() |
| 973 |
* - &dma_buf_ops.mmap() |
| 974 |
* |
| 975 |
* 2. These &dma_buf_ops callbacks are invoked with locked dma-buf |
| 976 |
* reservation and exporter can't take the lock: |
| 977 |
* |
| 978 |
* - &dma_buf_ops.pin() |
| 979 |
* - &dma_buf_ops.unpin() |
| 980 |
* - &dma_buf_ops.map_dma_buf() |
| 981 |
* - &dma_buf_ops.unmap_dma_buf() |
| 982 |
* - &dma_buf_ops.vmap() |
| 983 |
* - &dma_buf_ops.vunmap() |
| 984 |
* |
| 985 |
* 3. Exporters must hold the dma-buf reservation lock when calling these |
| 986 |
* functions: |
| 987 |
* |
| 988 |
* - dma_buf_move_notify() |
| 989 |
*/ |
| 990 |
|
| 991 |
/** |
| 992 |
* dma_buf_dynamic_attach - Add the device to dma_buf's attachments list |
| 993 |
* @dmabuf: [in] buffer to attach device to. |
| 994 |
* @dev: [in] device to be attached. |
| 995 |
* @importer_ops: [in] importer operations for the attachment |
| 996 |
* @importer_priv: [in] importer private pointer for the attachment |
| 997 |
* |
| 998 |
* Returns struct dma_buf_attachment pointer for this attachment. Attachments |
| 999 |
* must be cleaned up by calling dma_buf_detach(). |
| 1000 |
* |
| 1001 |
* Optionally this calls &dma_buf_ops.attach to allow device-specific attach |
| 1002 |
* functionality. |
| 1003 |
* |
| 1004 |
* Returns: |
| 1005 |
* |
| 1006 |
* A pointer to newly created &dma_buf_attachment on success, or a negative |
| 1007 |
* error code wrapped into a pointer on failure. |
| 1008 |
* |
| 1009 |
* Note that this can fail if the backing storage of @dmabuf is in a place not |
| 1010 |
* accessible to @dev, and cannot be moved to a more suitable place. This is |
| 1011 |
* indicated with the error code -EBUSY. |
| 1012 |
*/ |
| 1013 |
struct dma_buf_attachment * |
| 1014 |
dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct device *dev, |
| 1015 |
const struct dma_buf_attach_ops *importer_ops, |
| 1016 |
void *importer_priv) |
| 1017 |
{ |
| 1018 |
struct dma_buf_attachment *attach; |
| 1019 |
int ret; |
| 1020 |
|
| 1021 |
if (WARN_ON(!dmabuf || !dev)) |
| 1022 |
return ERR_PTR(-EINVAL); |
| 1023 |
|
| 1024 |
if (WARN_ON(importer_ops && !importer_ops->move_notify)) |
| 1025 |
return ERR_PTR(-EINVAL); |
| 1026 |
|
| 1027 |
attach = kzalloc_obj(*attach); |
| 1028 |
if (!attach) |
| 1029 |
return ERR_PTR(-ENOMEM); |
| 1030 |
|
| 1031 |
attach->dev = dev; |
| 1032 |
attach->dmabuf = dmabuf; |
| 1033 |
if (importer_ops) |
| 1034 |
attach->peer2peer = importer_ops->allow_peer2peer; |
| 1035 |
attach->importer_ops = importer_ops; |
| 1036 |
attach->importer_priv = importer_priv; |
| 1037 |
|
| 1038 |
if (dmabuf->ops->attach) { |
| 1039 |
ret = dmabuf->ops->attach(dmabuf, attach); |
| 1040 |
if (ret) |
| 1041 |
goto err_attach; |
| 1042 |
} |
| 1043 |
dma_resv_lock(dmabuf->resv, NULL); |
| 1044 |
list_add(&attach->node, &dmabuf->attachments); |
| 1045 |
dma_resv_unlock(dmabuf->resv); |
| 1046 |
|
| 1047 |
DMA_BUF_TRACE(trace_dma_buf_dynamic_attach, dmabuf, attach, |
| 1048 |
dma_buf_attachment_is_dynamic(attach), dev); |
| 1049 |
|
| 1050 |
return attach; |
| 1051 |
|
| 1052 |
err_attach: |
| 1053 |
kfree(attach); |
| 1054 |
return ERR_PTR(ret); |
| 1055 |
} |
| 1056 |
EXPORT_SYMBOL_NS_GPL(dma_buf_dynamic_attach, "DMA_BUF"); |
| 1057 |
|
| 1058 |
/** |
| 1059 |
* dma_buf_attach - Wrapper for dma_buf_dynamic_attach |
| 1060 |
* @dmabuf: [in] buffer to attach device to. |
| 1061 |
* @dev: [in] device to be attached. |
| 1062 |
* |
| 1063 |
* Wrapper to call dma_buf_dynamic_attach() for drivers which still use a static |
| 1064 |
* mapping. |
| 1065 |
*/ |
| 1066 |
struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf, |
| 1067 |
struct device *dev) |
| 1068 |
{ |
| 1069 |
return dma_buf_dynamic_attach(dmabuf, dev, NULL, NULL); |
| 1070 |
} |
| 1071 |
EXPORT_SYMBOL_NS_GPL(dma_buf_attach, "DMA_BUF"); |
| 1072 |
|
| 1073 |
/** |
| 1074 |
* dma_buf_detach - Remove the given attachment from dmabuf's attachments list |
| 1075 |
* @dmabuf: [in] buffer to detach from. |
| 1076 |
* @attach: [in] attachment to be detached; is free'd after this call. |
| 1077 |
* |
| 1078 |
* Clean up a device attachment obtained by calling dma_buf_attach(). |
| 1079 |
* |
| 1080 |
* Optionally this calls &dma_buf_ops.detach for device-specific detach. |
| 1081 |
*/ |
| 1082 |
void dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach) |
| 1083 |
{ |
| 1084 |
if (WARN_ON(!dmabuf || !attach || dmabuf != attach->dmabuf)) |
| 1085 |
return; |
| 1086 |
|
| 1087 |
dma_resv_lock(dmabuf->resv, NULL); |
| 1088 |
list_del(&attach->node); |
| 1089 |
dma_resv_unlock(dmabuf->resv); |
| 1090 |
|
| 1091 |
if (dmabuf->ops->detach) |
| 1092 |
dmabuf->ops->detach(dmabuf, attach); |
| 1093 |
|
| 1094 |
DMA_BUF_TRACE(trace_dma_buf_detach, dmabuf, attach, |
| 1095 |
dma_buf_attachment_is_dynamic(attach), attach->dev); |
| 1096 |
|
| 1097 |
kfree(attach); |
| 1098 |
} |
| 1099 |
EXPORT_SYMBOL_NS_GPL(dma_buf_detach, "DMA_BUF"); |
| 1100 |
|
| 1101 |
/** |
| 1102 |
* dma_buf_pin - Lock down the DMA-buf |
| 1103 |
* @attach: [in] attachment which should be pinned |
| 1104 |
* |
| 1105 |
* Only dynamic importers (who set up @attach with dma_buf_dynamic_attach()) may |
| 1106 |
* call this, and only for limited use cases like scanout and not for temporary |
| 1107 |
* pin operations. It is not permitted to allow userspace to pin arbitrary |
| 1108 |
* amounts of buffers through this interface. |
| 1109 |
* |
| 1110 |
* Buffers must be unpinned by calling dma_buf_unpin(). |
| 1111 |
* |
| 1112 |
* Returns: |
| 1113 |
* 0 on success, negative error code on failure. |
| 1114 |
*/ |
| 1115 |
int dma_buf_pin(struct dma_buf_attachment *attach) |
| 1116 |
{ |
| 1117 |
struct dma_buf *dmabuf = attach->dmabuf; |
| 1118 |
int ret = 0; |
| 1119 |
|
| 1120 |
WARN_ON(!attach->importer_ops); |
| 1121 |
|
| 1122 |
dma_resv_assert_held(dmabuf->resv); |
| 1123 |
|
| 1124 |
if (dmabuf->ops->pin) |
| 1125 |
ret = dmabuf->ops->pin(attach); |
| 1126 |
|
| 1127 |
return ret; |
| 1128 |
} |
| 1129 |
EXPORT_SYMBOL_NS_GPL(dma_buf_pin, "DMA_BUF"); |
| 1130 |
|
| 1131 |
/** |
| 1132 |
* dma_buf_unpin - Unpin a DMA-buf |
| 1133 |
* @attach: [in] attachment which should be unpinned |
| 1134 |
* |
| 1135 |
* This unpins a buffer pinned by dma_buf_pin() and allows the exporter to move |
| 1136 |
* any mapping of @attach again and inform the importer through |
| 1137 |
* &dma_buf_attach_ops.move_notify. |
| 1138 |
*/ |
| 1139 |
void dma_buf_unpin(struct dma_buf_attachment *attach) |
| 1140 |
{ |
| 1141 |
struct dma_buf *dmabuf = attach->dmabuf; |
| 1142 |
|
| 1143 |
WARN_ON(!attach->importer_ops); |
| 1144 |
|
| 1145 |
dma_resv_assert_held(dmabuf->resv); |
| 1146 |
|
| 1147 |
if (dmabuf->ops->unpin) |
| 1148 |
dmabuf->ops->unpin(attach); |
| 1149 |
} |
| 1150 |
EXPORT_SYMBOL_NS_GPL(dma_buf_unpin, "DMA_BUF"); |
| 1151 |
|
| 1152 |
/** |
| 1153 |
* dma_buf_map_attachment - Returns the scatterlist table of the attachment; |
| 1154 |
* mapped into _device_ address space. Is a wrapper for map_dma_buf() of the |
| 1155 |
* dma_buf_ops. |
| 1156 |
* @attach: [in] attachment whose scatterlist is to be returned |
| 1157 |
* @direction: [in] direction of DMA transfer |
| 1158 |
* |
| 1159 |
* Returns sg_table containing the scatterlist to be returned; returns ERR_PTR |
| 1160 |
* on error. May return -EINTR if it is interrupted by a signal. |
| 1161 |
* |
| 1162 |
* On success, the DMA addresses and lengths in the returned scatterlist are |
| 1163 |
* PAGE_SIZE aligned. |
| 1164 |
* |
| 1165 |
* A mapping must be unmapped by using dma_buf_unmap_attachment(). Note that |
| 1166 |
* the underlying backing storage is pinned for as long as a mapping exists, |
| 1167 |
* therefore users/importers should not hold onto a mapping for undue amounts of |
| 1168 |
* time. |
| 1169 |
* |
| 1170 |
* Important: Dynamic importers must wait for the exclusive fence of the struct |
| 1171 |
* dma_resv attached to the DMA-BUF first. |
| 1172 |
*/ |
| 1173 |
struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *attach, |
| 1174 |
enum dma_data_direction direction) |
| 1175 |
{ |
| 1176 |
struct sg_table *sg_table; |
| 1177 |
signed long ret; |
| 1178 |
|
| 1179 |
might_sleep(); |
| 1180 |
|
| 1181 |
if (WARN_ON(!attach || !attach->dmabuf)) |
| 1182 |
return ERR_PTR(-EINVAL); |
| 1183 |
|
| 1184 |
dma_resv_assert_held(attach->dmabuf->resv); |
| 1185 |
|
| 1186 |
if (dma_buf_pin_on_map(attach)) { |
| 1187 |
ret = attach->dmabuf->ops->pin(attach); |
| 1188 |
/* |
| 1189 |
* Catch exporters making buffers inaccessible even when |
| 1190 |
* attachments preventing that exist. |
| 1191 |
*/ |
| 1192 |
WARN_ON_ONCE(ret == -EBUSY); |
| 1193 |
if (ret) |
| 1194 |
return ERR_PTR(ret); |
| 1195 |
} |
| 1196 |
|
| 1197 |
sg_table = attach->dmabuf->ops->map_dma_buf(attach, direction); |
| 1198 |
if (!sg_table) |
| 1199 |
sg_table = ERR_PTR(-ENOMEM); |
| 1200 |
if (IS_ERR(sg_table)) |
| 1201 |
goto error_unpin; |
| 1202 |
|
| 1203 |
/* |
| 1204 |
* Importers with static attachments don't wait for fences. |
| 1205 |
*/ |
| 1206 |
if (!dma_buf_attachment_is_dynamic(attach)) { |
| 1207 |
ret = dma_resv_wait_timeout(attach->dmabuf->resv, |
| 1208 |
DMA_RESV_USAGE_KERNEL, true, |
| 1209 |
MAX_SCHEDULE_TIMEOUT); |
| 1210 |
if (ret < 0) |
| 1211 |
goto error_unmap; |
| 1212 |
} |
| 1213 |
ret = dma_buf_wrap_sg_table(&sg_table); |
| 1214 |
if (ret) |
| 1215 |
goto error_unmap; |
| 1216 |
|
| 1217 |
if (IS_ENABLED(CONFIG_DMA_API_DEBUG)) { |
| 1218 |
struct scatterlist *sg; |
| 1219 |
u64 addr; |
| 1220 |
int len; |
| 1221 |
int i; |
| 1222 |
|
| 1223 |
for_each_sgtable_dma_sg(sg_table, sg, i) { |
| 1224 |
addr = sg_dma_address(sg); |
| 1225 |
len = sg_dma_len(sg); |
| 1226 |
if (!PAGE_ALIGNED(addr) || !PAGE_ALIGNED(len)) { |
| 1227 |
pr_debug("%s: addr %llx or len %x is not page aligned!\n", |
| 1228 |
__func__, addr, len); |
| 1229 |
break; |
| 1230 |
} |
| 1231 |
} |
| 1232 |
} |
| 1233 |
return sg_table; |
| 1234 |
|
| 1235 |
error_unmap: |
| 1236 |
attach->dmabuf->ops->unmap_dma_buf(attach, sg_table, direction); |
| 1237 |
sg_table = ERR_PTR(ret); |
| 1238 |
|
| 1239 |
error_unpin: |
| 1240 |
if (dma_buf_pin_on_map(attach)) |
| 1241 |
attach->dmabuf->ops->unpin(attach); |
| 1242 |
|
| 1243 |
return sg_table; |
| 1244 |
} |
| 1245 |
EXPORT_SYMBOL_NS_GPL(dma_buf_map_attachment, "DMA_BUF"); |
| 1246 |
|
| 1247 |
/** |
| 1248 |
* dma_buf_map_attachment_unlocked - Returns the scatterlist table of the attachment; |
| 1249 |
* mapped into _device_ address space. Is a wrapper for map_dma_buf() of the |
| 1250 |
* dma_buf_ops. |
| 1251 |
* @attach: [in] attachment whose scatterlist is to be returned |
| 1252 |
* @direction: [in] direction of DMA transfer |
| 1253 |
* |
| 1254 |
* Unlocked variant of dma_buf_map_attachment(). |
| 1255 |
*/ |
| 1256 |
struct sg_table * |
| 1257 |
dma_buf_map_attachment_unlocked(struct dma_buf_attachment *attach, |
| 1258 |
enum dma_data_direction direction) |
| 1259 |
{ |
| 1260 |
struct sg_table *sg_table; |
| 1261 |
|
| 1262 |
might_sleep(); |
| 1263 |
|
| 1264 |
if (WARN_ON(!attach || !attach->dmabuf)) |
| 1265 |
return ERR_PTR(-EINVAL); |
| 1266 |
|
| 1267 |
dma_resv_lock(attach->dmabuf->resv, NULL); |
| 1268 |
sg_table = dma_buf_map_attachment(attach, direction); |
| 1269 |
dma_resv_unlock(attach->dmabuf->resv); |
| 1270 |
|
| 1271 |
return sg_table; |
| 1272 |
} |
| 1273 |
EXPORT_SYMBOL_NS_GPL(dma_buf_map_attachment_unlocked, "DMA_BUF"); |
| 1274 |
|
| 1275 |
/** |
| 1276 |
* dma_buf_unmap_attachment - unmaps and decreases usecount of the buffer;might |
| 1277 |
* deallocate the scatterlist associated. Is a wrapper for unmap_dma_buf() of |
| 1278 |
* dma_buf_ops. |
| 1279 |
* @attach: [in] attachment to unmap buffer from |
| 1280 |
* @sg_table: [in] scatterlist info of the buffer to unmap |
| 1281 |
* @direction: [in] direction of DMA transfer |
| 1282 |
* |
| 1283 |
* This unmaps a DMA mapping for @attached obtained by dma_buf_map_attachment(). |
| 1284 |
*/ |
| 1285 |
void dma_buf_unmap_attachment(struct dma_buf_attachment *attach, |
| 1286 |
struct sg_table *sg_table, |
| 1287 |
enum dma_data_direction direction) |
| 1288 |
{ |
| 1289 |
might_sleep(); |
| 1290 |
|
| 1291 |
if (WARN_ON(!attach || !attach->dmabuf || !sg_table)) |
| 1292 |
return; |
| 1293 |
|
| 1294 |
dma_resv_assert_held(attach->dmabuf->resv); |
| 1295 |
|
| 1296 |
dma_buf_unwrap_sg_table(&sg_table); |
| 1297 |
attach->dmabuf->ops->unmap_dma_buf(attach, sg_table, direction); |
| 1298 |
|
| 1299 |
if (dma_buf_pin_on_map(attach)) |
| 1300 |
attach->dmabuf->ops->unpin(attach); |
| 1301 |
} |
| 1302 |
EXPORT_SYMBOL_NS_GPL(dma_buf_unmap_attachment, "DMA_BUF"); |
| 1303 |
|
| 1304 |
/** |
| 1305 |
* dma_buf_unmap_attachment_unlocked - unmaps and decreases usecount of the buffer;might |
| 1306 |
* deallocate the scatterlist associated. Is a wrapper for unmap_dma_buf() of |
| 1307 |
* dma_buf_ops. |
| 1308 |
* @attach: [in] attachment to unmap buffer from |
| 1309 |
* @sg_table: [in] scatterlist info of the buffer to unmap |
| 1310 |
* @direction: [in] direction of DMA transfer |
| 1311 |
* |
| 1312 |
* Unlocked variant of dma_buf_unmap_attachment(). |
| 1313 |
*/ |
| 1314 |
void dma_buf_unmap_attachment_unlocked(struct dma_buf_attachment *attach, |
| 1315 |
struct sg_table *sg_table, |
| 1316 |
enum dma_data_direction direction) |
| 1317 |
{ |
| 1318 |
might_sleep(); |
| 1319 |
|
| 1320 |
if (WARN_ON(!attach || !attach->dmabuf || !sg_table)) |
| 1321 |
return; |
| 1322 |
|
| 1323 |
dma_resv_lock(attach->dmabuf->resv, NULL); |
| 1324 |
dma_buf_unmap_attachment(attach, sg_table, direction); |
| 1325 |
dma_resv_unlock(attach->dmabuf->resv); |
| 1326 |
} |
| 1327 |
EXPORT_SYMBOL_NS_GPL(dma_buf_unmap_attachment_unlocked, "DMA_BUF"); |
| 1328 |
|
| 1329 |
/** |
| 1330 |
* dma_buf_move_notify - notify attachments that DMA-buf is moving |
| 1331 |
* |
| 1332 |
* @dmabuf: [in] buffer which is moving |
| 1333 |
* |
| 1334 |
* Informs all attachments that they need to destroy and recreate all their |
| 1335 |
* mappings. |
| 1336 |
*/ |
| 1337 |
void dma_buf_move_notify(struct dma_buf *dmabuf) |
| 1338 |
{ |
| 1339 |
struct dma_buf_attachment *attach; |
| 1340 |
|
| 1341 |
dma_resv_assert_held(dmabuf->resv); |
| 1342 |
|
| 1343 |
list_for_each_entry(attach, &dmabuf->attachments, node) |
| 1344 |
if (attach->importer_ops) |
| 1345 |
attach->importer_ops->move_notify(attach); |
| 1346 |
} |
| 1347 |
EXPORT_SYMBOL_NS_GPL(dma_buf_move_notify, "DMA_BUF"); |
| 1348 |
|
| 1349 |
/** |
| 1350 |
* DOC: cpu access |
| 1351 |
* |
| 1352 |
* There are multiple reasons for supporting CPU access to a dma buffer object: |
| 1353 |
* |
| 1354 |
* - Fallback operations in the kernel, for example when a device is connected |
| 1355 |
* over USB and the kernel needs to shuffle the data around first before |
| 1356 |
* sending it away. Cache coherency is handled by bracketing any transactions |
| 1357 |
* with calls to dma_buf_begin_cpu_access() and dma_buf_end_cpu_access() |
| 1358 |
* access. |
| 1359 |
* |
| 1360 |
* Since for most kernel internal dma-buf accesses need the entire buffer, a |
| 1361 |
* vmap interface is introduced. Note that on very old 32-bit architectures |
| 1362 |
* vmalloc space might be limited and result in vmap calls failing. |
| 1363 |
* |
| 1364 |
* Interfaces: |
| 1365 |
* |
| 1366 |
* .. code-block:: c |
| 1367 |
* |
| 1368 |
* void *dma_buf_vmap(struct dma_buf *dmabuf, struct iosys_map *map) |
| 1369 |
* void dma_buf_vunmap(struct dma_buf *dmabuf, struct iosys_map *map) |
| 1370 |
* |
| 1371 |
* The vmap call can fail if there is no vmap support in the exporter, or if |
| 1372 |
* it runs out of vmalloc space. Note that the dma-buf layer keeps a reference |
| 1373 |
* count for all vmap access and calls down into the exporter's vmap function |
| 1374 |
* only when no vmapping exists, and only unmaps it once. Protection against |
| 1375 |
* concurrent vmap/vunmap calls is provided by taking the &dma_buf.lock mutex. |
| 1376 |
* |
| 1377 |
* - For full compatibility on the importer side with existing userspace |
| 1378 |
* interfaces, which might already support mmap'ing buffers. This is needed in |
| 1379 |
* many processing pipelines (e.g. feeding a software rendered image into a |
| 1380 |
* hardware pipeline, thumbnail creation, snapshots, ...). Also, Android's ION |
| 1381 |
* framework already supported this and for DMA buffer file descriptors to |
| 1382 |
* replace ION buffers mmap support was needed. |
| 1383 |
* |
| 1384 |
* There is no special interfaces, userspace simply calls mmap on the dma-buf |
| 1385 |
* fd. But like for CPU access there's a need to bracket the actual access, |
| 1386 |
* which is handled by the ioctl (DMA_BUF_IOCTL_SYNC). Note that |
| 1387 |
* DMA_BUF_IOCTL_SYNC can fail with -EAGAIN or -EINTR, in which case it must |
| 1388 |
* be restarted. |
| 1389 |
* |
| 1390 |
* Some systems might need some sort of cache coherency management e.g. when |
| 1391 |
* CPU and GPU domains are being accessed through dma-buf at the same time. |
| 1392 |
* To circumvent this problem there are begin/end coherency markers, that |
| 1393 |
* forward directly to existing dma-buf device drivers vfunc hooks. Userspace |
| 1394 |
* can make use of those markers through the DMA_BUF_IOCTL_SYNC ioctl. The |
| 1395 |
* sequence would be used like following: |
| 1396 |
* |
| 1397 |
* - mmap dma-buf fd |
| 1398 |
* - for each drawing/upload cycle in CPU 1. SYNC_START ioctl, 2. read/write |
| 1399 |
* to mmap area 3. SYNC_END ioctl. This can be repeated as often as you |
| 1400 |
* want (with the new data being consumed by say the GPU or the scanout |
| 1401 |
* device) |
| 1402 |
* - munmap once you don't need the buffer any more |
| 1403 |
* |
| 1404 |
* For correctness and optimal performance, it is always required to use |
| 1405 |
* SYNC_START and SYNC_END before and after, respectively, when accessing the |
| 1406 |
* mapped address. Userspace cannot rely on coherent access, even when there |
| 1407 |
* are systems where it just works without calling these ioctls. |
| 1408 |
* |
| 1409 |
* - And as a CPU fallback in userspace processing pipelines. |
| 1410 |
* |
| 1411 |
* Similar to the motivation for kernel cpu access it is again important that |
| 1412 |
* the userspace code of a given importing subsystem can use the same |
| 1413 |
* interfaces with a imported dma-buf buffer object as with a native buffer |
| 1414 |
* object. This is especially important for drm where the userspace part of |
| 1415 |
* contemporary OpenGL, X, and other drivers is huge, and reworking them to |
| 1416 |
* use a different way to mmap a buffer rather invasive. |
| 1417 |
* |
| 1418 |
* The assumption in the current dma-buf interfaces is that redirecting the |
| 1419 |
* initial mmap is all that's needed. A survey of some of the existing |
| 1420 |
* subsystems shows that no driver seems to do any nefarious thing like |
| 1421 |
* syncing up with outstanding asynchronous processing on the device or |
| 1422 |
* allocating special resources at fault time. So hopefully this is good |
| 1423 |
* enough, since adding interfaces to intercept pagefaults and allow pte |
| 1424 |
* shootdowns would increase the complexity quite a bit. |
| 1425 |
* |
| 1426 |
* Interface: |
| 1427 |
* |
| 1428 |
* .. code-block:: c |
| 1429 |
* |
| 1430 |
* int dma_buf_mmap(struct dma_buf *, struct vm_area_struct *, unsigned long); |
| 1431 |
* |
| 1432 |
* If the importing subsystem simply provides a special-purpose mmap call to |
| 1433 |
* set up a mapping in userspace, calling do_mmap with &dma_buf.file will |
| 1434 |
* equally achieve that for a dma-buf object. |
| 1435 |
*/ |
| 1436 |
|
| 1437 |
static int __dma_buf_begin_cpu_access(struct dma_buf *dmabuf, |
| 1438 |
enum dma_data_direction direction) |
| 1439 |
{ |
| 1440 |
bool write = (direction == DMA_BIDIRECTIONAL || |
| 1441 |
direction == DMA_TO_DEVICE); |
| 1442 |
struct dma_resv *resv = dmabuf->resv; |
| 1443 |
long ret; |
| 1444 |
|
| 1445 |
/* Wait on any implicit rendering fences */ |
| 1446 |
ret = dma_resv_wait_timeout(resv, dma_resv_usage_rw(write), |
| 1447 |
true, MAX_SCHEDULE_TIMEOUT); |
| 1448 |
if (ret < 0) |
| 1449 |
return ret; |
| 1450 |
|
| 1451 |
return 0; |
| 1452 |
} |
| 1453 |
|
| 1454 |
/** |
| 1455 |
* dma_buf_begin_cpu_access - Must be called before accessing a dma_buf from the |
| 1456 |
* cpu in the kernel context. Calls begin_cpu_access to allow exporter-specific |
| 1457 |
* preparations. Coherency is only guaranteed in the specified range for the |
| 1458 |
* specified access direction. |
| 1459 |
* @dmabuf: [in] buffer to prepare cpu access for. |
| 1460 |
* @direction: [in] direction of access. |
| 1461 |
* |
| 1462 |
* After the cpu access is complete the caller should call |
| 1463 |
* dma_buf_end_cpu_access(). Only when cpu access is bracketed by both calls is |
| 1464 |
* it guaranteed to be coherent with other DMA access. |
| 1465 |
* |
| 1466 |
* This function will also wait for any DMA transactions tracked through |
| 1467 |
* implicit synchronization in &dma_buf.resv. For DMA transactions with explicit |
| 1468 |
* synchronization this function will only ensure cache coherency, callers must |
| 1469 |
* ensure synchronization with such DMA transactions on their own. |
| 1470 |
* |
| 1471 |
* Can return negative error values, returns 0 on success. |
| 1472 |
*/ |
| 1473 |
int dma_buf_begin_cpu_access(struct dma_buf *dmabuf, |
| 1474 |
enum dma_data_direction direction) |
| 1475 |
{ |
| 1476 |
int ret = 0; |
| 1477 |
|
| 1478 |
if (WARN_ON(!dmabuf)) |
| 1479 |
return -EINVAL; |
| 1480 |
|
| 1481 |
might_lock(&dmabuf->resv->lock.base); |
| 1482 |
|
| 1483 |
if (dmabuf->ops->begin_cpu_access) |
| 1484 |
ret = dmabuf->ops->begin_cpu_access(dmabuf, direction); |
| 1485 |
|
| 1486 |
/* Ensure that all fences are waited upon - but we first allow |
| 1487 |
* the native handler the chance to do so more efficiently if it |
| 1488 |
* chooses. A double invocation here will be reasonably cheap no-op. |
| 1489 |
*/ |
| 1490 |
if (ret == 0) |
| 1491 |
ret = __dma_buf_begin_cpu_access(dmabuf, direction); |
| 1492 |
|
| 1493 |
return ret; |
| 1494 |
} |
| 1495 |
EXPORT_SYMBOL_NS_GPL(dma_buf_begin_cpu_access, "DMA_BUF"); |
| 1496 |
|
| 1497 |
/** |
| 1498 |
* dma_buf_end_cpu_access - Must be called after accessing a dma_buf from the |
| 1499 |
* cpu in the kernel context. Calls end_cpu_access to allow exporter-specific |
| 1500 |
* actions. Coherency is only guaranteed in the specified range for the |
| 1501 |
* specified access direction. |
| 1502 |
* @dmabuf: [in] buffer to complete cpu access for. |
| 1503 |
* @direction: [in] direction of access. |
| 1504 |
* |
| 1505 |
* This terminates CPU access started with dma_buf_begin_cpu_access(). |
| 1506 |
* |
| 1507 |
* Can return negative error values, returns 0 on success. |
| 1508 |
*/ |
| 1509 |
int dma_buf_end_cpu_access(struct dma_buf *dmabuf, |
| 1510 |
enum dma_data_direction direction) |
| 1511 |
{ |
| 1512 |
int ret = 0; |
| 1513 |
|
| 1514 |
WARN_ON(!dmabuf); |
| 1515 |
|
| 1516 |
might_lock(&dmabuf->resv->lock.base); |
| 1517 |
|
| 1518 |
if (dmabuf->ops->end_cpu_access) |
| 1519 |
ret = dmabuf->ops->end_cpu_access(dmabuf, direction); |
| 1520 |
|
| 1521 |
return ret; |
| 1522 |
} |
| 1523 |
EXPORT_SYMBOL_NS_GPL(dma_buf_end_cpu_access, "DMA_BUF"); |
| 1524 |
|
| 1525 |
|
| 1526 |
/** |
| 1527 |
* dma_buf_mmap - Setup up a userspace mmap with the given vma |
| 1528 |
* @dmabuf: [in] buffer that should back the vma |
| 1529 |
* @vma: [in] vma for the mmap |
| 1530 |
* @pgoff: [in] offset in pages where this mmap should start within the |
| 1531 |
* dma-buf buffer. |
| 1532 |
* |
| 1533 |
* This function adjusts the passed in vma so that it points at the file of the |
| 1534 |
* dma_buf operation. It also adjusts the starting pgoff and does bounds |
| 1535 |
* checking on the size of the vma. Then it calls the exporters mmap function to |
| 1536 |
* set up the mapping. |
| 1537 |
* |
| 1538 |
* Can return negative error values, returns 0 on success. |
| 1539 |
*/ |
| 1540 |
int dma_buf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma, |
| 1541 |
unsigned long pgoff) |
| 1542 |
{ |
| 1543 |
if (WARN_ON(!dmabuf || !vma)) |
| 1544 |
return -EINVAL; |
| 1545 |
|
| 1546 |
/* check if buffer supports mmap */ |
| 1547 |
if (!dmabuf->ops->mmap) |
| 1548 |
return -EINVAL; |
| 1549 |
|
| 1550 |
/* check for offset overflow */ |
| 1551 |
if (pgoff + vma_pages(vma) < pgoff) |
| 1552 |
return -EOVERFLOW; |
| 1553 |
|
| 1554 |
/* check for overflowing the buffer's size */ |
| 1555 |
if (pgoff + vma_pages(vma) > |
| 1556 |
dmabuf->size >> PAGE_SHIFT) |
| 1557 |
return -EINVAL; |
| 1558 |
|
| 1559 |
/* readjust the vma */ |
| 1560 |
vma_set_file(vma, dmabuf->file); |
| 1561 |
vma->vm_pgoff = pgoff; |
| 1562 |
|
| 1563 |
DMA_BUF_TRACE(trace_dma_buf_mmap, dmabuf); |
| 1564 |
|
| 1565 |
return dmabuf->ops->mmap(dmabuf, vma); |
| 1566 |
} |
| 1567 |
EXPORT_SYMBOL_NS_GPL(dma_buf_mmap, "DMA_BUF"); |
| 1568 |
|
| 1569 |
/** |
| 1570 |
* dma_buf_vmap - Create virtual mapping for the buffer object into kernel |
| 1571 |
* address space. Same restrictions as for vmap and friends apply. |
| 1572 |
* @dmabuf: [in] buffer to vmap |
| 1573 |
* @map: [out] returns the vmap pointer |
| 1574 |
* |
| 1575 |
* This call may fail due to lack of virtual mapping address space. |
| 1576 |
* These calls are optional in drivers. The intended use for them |
| 1577 |
* is for mapping objects linear in kernel space for high use objects. |
| 1578 |
* |
| 1579 |
* To ensure coherency users must call dma_buf_begin_cpu_access() and |
| 1580 |
* dma_buf_end_cpu_access() around any cpu access performed through this |
| 1581 |
* mapping. |
| 1582 |
* |
| 1583 |
* Returns 0 on success, or a negative errno code otherwise. |
| 1584 |
*/ |
| 1585 |
int dma_buf_vmap(struct dma_buf *dmabuf, struct iosys_map *map) |
| 1586 |
{ |
| 1587 |
struct iosys_map ptr; |
| 1588 |
int ret; |
| 1589 |
|
| 1590 |
iosys_map_clear(map); |
| 1591 |
|
| 1592 |
if (WARN_ON(!dmabuf)) |
| 1593 |
return -EINVAL; |
| 1594 |
|
| 1595 |
dma_resv_assert_held(dmabuf->resv); |
| 1596 |
|
| 1597 |
if (!dmabuf->ops->vmap) |
| 1598 |
return -EINVAL; |
| 1599 |
|
| 1600 |
if (dmabuf->vmapping_counter) { |
| 1601 |
dmabuf->vmapping_counter++; |
| 1602 |
BUG_ON(iosys_map_is_null(&dmabuf->vmap_ptr)); |
| 1603 |
*map = dmabuf->vmap_ptr; |
| 1604 |
return 0; |
| 1605 |
} |
| 1606 |
|
| 1607 |
BUG_ON(iosys_map_is_set(&dmabuf->vmap_ptr)); |
| 1608 |
|
| 1609 |
ret = dmabuf->ops->vmap(dmabuf, &ptr); |
| 1610 |
if (WARN_ON_ONCE(ret)) |
| 1611 |
return ret; |
| 1612 |
|
| 1613 |
dmabuf->vmap_ptr = ptr; |
| 1614 |
dmabuf->vmapping_counter = 1; |
| 1615 |
|
| 1616 |
*map = dmabuf->vmap_ptr; |
| 1617 |
|
| 1618 |
return 0; |
| 1619 |
} |
| 1620 |
EXPORT_SYMBOL_NS_GPL(dma_buf_vmap, "DMA_BUF"); |
| 1621 |
|
| 1622 |
/** |
| 1623 |
* dma_buf_vmap_unlocked - Create virtual mapping for the buffer object into kernel |
| 1624 |
* address space. Same restrictions as for vmap and friends apply. |
| 1625 |
* @dmabuf: [in] buffer to vmap |
| 1626 |
* @map: [out] returns the vmap pointer |
| 1627 |
* |
| 1628 |
* Unlocked version of dma_buf_vmap() |
| 1629 |
* |
| 1630 |
* Returns 0 on success, or a negative errno code otherwise. |
| 1631 |
*/ |
| 1632 |
int dma_buf_vmap_unlocked(struct dma_buf *dmabuf, struct iosys_map *map) |
| 1633 |
{ |
| 1634 |
int ret; |
| 1635 |
|
| 1636 |
iosys_map_clear(map); |
| 1637 |
|
| 1638 |
if (WARN_ON(!dmabuf)) |
| 1639 |
return -EINVAL; |
| 1640 |
|
| 1641 |
dma_resv_lock(dmabuf->resv, NULL); |
| 1642 |
ret = dma_buf_vmap(dmabuf, map); |
| 1643 |
dma_resv_unlock(dmabuf->resv); |
| 1644 |
|
| 1645 |
return ret; |
| 1646 |
} |
| 1647 |
EXPORT_SYMBOL_NS_GPL(dma_buf_vmap_unlocked, "DMA_BUF"); |
| 1648 |
|
| 1649 |
/** |
| 1650 |
* dma_buf_vunmap - Unmap a vmap obtained by dma_buf_vmap. |
| 1651 |
* @dmabuf: [in] buffer to vunmap |
| 1652 |
* @map: [in] vmap pointer to vunmap |
| 1653 |
*/ |
| 1654 |
void dma_buf_vunmap(struct dma_buf *dmabuf, struct iosys_map *map) |
| 1655 |
{ |
| 1656 |
if (WARN_ON(!dmabuf)) |
| 1657 |
return; |
| 1658 |
|
| 1659 |
dma_resv_assert_held(dmabuf->resv); |
| 1660 |
|
| 1661 |
BUG_ON(iosys_map_is_null(&dmabuf->vmap_ptr)); |
| 1662 |
BUG_ON(dmabuf->vmapping_counter == 0); |
| 1663 |
BUG_ON(!iosys_map_is_equal(&dmabuf->vmap_ptr, map)); |
| 1664 |
|
| 1665 |
if (--dmabuf->vmapping_counter == 0) { |
| 1666 |
if (dmabuf->ops->vunmap) |
| 1667 |
dmabuf->ops->vunmap(dmabuf, map); |
| 1668 |
iosys_map_clear(&dmabuf->vmap_ptr); |
| 1669 |
} |
| 1670 |
} |
| 1671 |
EXPORT_SYMBOL_NS_GPL(dma_buf_vunmap, "DMA_BUF"); |
| 1672 |
|
| 1673 |
/** |
| 1674 |
* dma_buf_vunmap_unlocked - Unmap a vmap obtained by dma_buf_vmap. |
| 1675 |
* @dmabuf: [in] buffer to vunmap |
| 1676 |
* @map: [in] vmap pointer to vunmap |
| 1677 |
*/ |
| 1678 |
void dma_buf_vunmap_unlocked(struct dma_buf *dmabuf, struct iosys_map *map) |
| 1679 |
{ |
| 1680 |
if (WARN_ON(!dmabuf)) |
| 1681 |
return; |
| 1682 |
|
| 1683 |
dma_resv_lock(dmabuf->resv, NULL); |
| 1684 |
dma_buf_vunmap(dmabuf, map); |
| 1685 |
dma_resv_unlock(dmabuf->resv); |
| 1686 |
} |
| 1687 |
EXPORT_SYMBOL_NS_GPL(dma_buf_vunmap_unlocked, "DMA_BUF"); |
| 1688 |
|
| 1689 |
#ifdef CONFIG_DEBUG_FS |
| 1690 |
static int dma_buf_debug_show(struct seq_file *s, void *unused) |
| 1691 |
{ |
| 1692 |
struct dma_buf *buf_obj; |
| 1693 |
struct dma_buf_attachment *attach_obj; |
| 1694 |
int count = 0, attach_count; |
| 1695 |
size_t size = 0; |
| 1696 |
int ret; |
| 1697 |
|
| 1698 |
ret = mutex_lock_interruptible(&dmabuf_list_mutex); |
| 1699 |
|
| 1700 |
if (ret) |
| 1701 |
return ret; |
| 1702 |
|
| 1703 |
seq_puts(s, "\nDma-buf Objects:\n"); |
| 1704 |
seq_printf(s, "%-8s\t%-8s\t%-8s\t%-8s\texp_name\t%-8s\tname\n", |
| 1705 |
"size", "flags", "mode", "count", "ino"); |
| 1706 |
|
| 1707 |
list_for_each_entry(buf_obj, &dmabuf_list, list_node) { |
| 1708 |
|
| 1709 |
ret = dma_resv_lock_interruptible(buf_obj->resv, NULL); |
| 1710 |
if (ret) |
| 1711 |
goto error_unlock; |
| 1712 |
|
| 1713 |
|
| 1714 |
spin_lock(&buf_obj->name_lock); |
| 1715 |
seq_printf(s, "%08zu\t%08x\t%08x\t%08ld\t%s\t%08lu\t%s\n", |
| 1716 |
buf_obj->size, |
| 1717 |
buf_obj->file->f_flags, buf_obj->file->f_mode, |
| 1718 |
file_count(buf_obj->file), |
| 1719 |
buf_obj->exp_name, |
| 1720 |
file_inode(buf_obj->file)->i_ino, |
| 1721 |
buf_obj->name ?: "<none>"); |
| 1722 |
spin_unlock(&buf_obj->name_lock); |
| 1723 |
|
| 1724 |
dma_resv_describe(buf_obj->resv, s); |
| 1725 |
|
| 1726 |
seq_puts(s, "\tAttached Devices:\n"); |
| 1727 |
attach_count = 0; |
| 1728 |
|
| 1729 |
list_for_each_entry(attach_obj, &buf_obj->attachments, node) { |
| 1730 |
seq_printf(s, "\t%s\n", dev_name(attach_obj->dev)); |
| 1731 |
attach_count++; |
| 1732 |
} |
| 1733 |
dma_resv_unlock(buf_obj->resv); |
| 1734 |
|
| 1735 |
seq_printf(s, "Total %d devices attached\n\n", |
| 1736 |
attach_count); |
| 1737 |
|
| 1738 |
count++; |
| 1739 |
size += buf_obj->size; |
| 1740 |
} |
| 1741 |
|
| 1742 |
seq_printf(s, "\nTotal %d objects, %zu bytes\n", count, size); |
| 1743 |
|
| 1744 |
mutex_unlock(&dmabuf_list_mutex); |
| 1745 |
return 0; |
| 1746 |
|
| 1747 |
error_unlock: |
| 1748 |
mutex_unlock(&dmabuf_list_mutex); |
| 1749 |
return ret; |
| 1750 |
} |
| 1751 |
|
| 1752 |
DEFINE_SHOW_ATTRIBUTE(dma_buf_debug); |
| 1753 |
|
| 1754 |
static struct dentry *dma_buf_debugfs_dir; |
| 1755 |
|
| 1756 |
static int dma_buf_init_debugfs(void) |
| 1757 |
{ |
| 1758 |
struct dentry *d; |
| 1759 |
int err = 0; |
| 1760 |
|
| 1761 |
d = debugfs_create_dir("dma_buf", NULL); |
| 1762 |
if (IS_ERR(d)) |
| 1763 |
return PTR_ERR(d); |
| 1764 |
|
| 1765 |
dma_buf_debugfs_dir = d; |
| 1766 |
|
| 1767 |
d = debugfs_create_file("bufinfo", 0444, dma_buf_debugfs_dir, |
| 1768 |
NULL, &dma_buf_debug_fops); |
| 1769 |
if (IS_ERR(d)) { |
| 1770 |
pr_debug("dma_buf: debugfs: failed to create node bufinfo\n"); |
| 1771 |
debugfs_remove_recursive(dma_buf_debugfs_dir); |
| 1772 |
dma_buf_debugfs_dir = NULL; |
| 1773 |
err = PTR_ERR(d); |
| 1774 |
} |
| 1775 |
|
| 1776 |
return err; |
| 1777 |
} |
| 1778 |
|
| 1779 |
static void dma_buf_uninit_debugfs(void) |
| 1780 |
{ |
| 1781 |
debugfs_remove_recursive(dma_buf_debugfs_dir); |
| 1782 |
} |
| 1783 |
#else |
| 1784 |
static inline int dma_buf_init_debugfs(void) |
| 1785 |
{ |
| 1786 |
return 0; |
| 1787 |
} |
| 1788 |
static inline void dma_buf_uninit_debugfs(void) |
| 1789 |
{ |
| 1790 |
} |
| 1791 |
#endif |
| 1792 |
|
| 1793 |
static int __init dma_buf_init(void) |
| 1794 |
{ |
| 1795 |
dma_buf_mnt = kern_mount(&dma_buf_fs_type); |
| 1796 |
if (IS_ERR(dma_buf_mnt)) |
| 1797 |
return PTR_ERR(dma_buf_mnt); |
| 1798 |
|
| 1799 |
dma_buf_init_debugfs(); |
| 1800 |
return 0; |
| 1801 |
} |
| 1802 |
subsys_initcall(dma_buf_init); |
| 1803 |
|
| 1804 |
static void __exit dma_buf_deinit(void) |
| 1805 |
{ |
| 1806 |
dma_buf_uninit_debugfs(); |
| 1807 |
kern_unmount(dma_buf_mnt); |
| 1808 |
} |
| 1809 |
__exitcall(dma_buf_deinit); |