Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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