ddd0847a69c5d7153e537ae50efd3960d8c99a43
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / arm / midgard_for_linux / mali_kbase_sync_user.c
1 /*
2  *
3  * (C) COPYRIGHT 2012-2015 ARM Limited. All rights reserved.
4  *
5  * This program is free software and is provided to you under the terms of the
6  * GNU General Public License version 2 as published by the Free Software
7  * Foundation, and any use by you of this program is subject to the terms
8  * of such GNU licence.
9  *
10  * A copy of the licence is included with the program, and can also be obtained
11  * from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
12  * Boston, MA  02110-1301, USA.
13  *
14  */
15
16
17
18
19
20 /**
21  * @file mali_kbase_sync_user.c
22  *
23  */
24
25 #ifdef CONFIG_SYNC
26
27 #include <linux/sched.h>
28 #include <linux/fdtable.h>
29 #include <linux/file.h>
30 #include <linux/fs.h>
31 #include <linux/module.h>
32 #include <linux/anon_inodes.h>
33 #include <linux/version.h>
34 #include <linux/uaccess.h>
35 #include <mali_kbase_sync.h>
36 #include <mali_base_kernel_sync.h>
37
38 static int kbase_stream_close(struct inode *inode, struct file *file)
39 {
40         struct sync_timeline *tl;
41
42         tl = (struct sync_timeline *)file->private_data;
43         BUG_ON(!tl);
44         sync_timeline_destroy(tl);
45         return 0;
46 }
47
48 static const struct file_operations stream_fops = {
49         .owner = THIS_MODULE,
50         .release = kbase_stream_close,
51 };
52
53 int kbase_stream_create(const char *name, int *const out_fd)
54 {
55         struct sync_timeline *tl;
56
57         BUG_ON(!out_fd);
58
59         tl = kbase_sync_timeline_alloc(name);
60         if (!tl)
61                 return -EINVAL;
62
63         *out_fd = anon_inode_getfd(name, &stream_fops, tl, O_RDONLY | O_CLOEXEC);
64
65         if (*out_fd < 0) {
66                 sync_timeline_destroy(tl);
67                 return -EINVAL;
68         }
69
70         return 0;
71 }
72
73 int kbase_stream_create_fence(int tl_fd)
74 {
75         struct sync_timeline *tl;
76         struct sync_pt *pt;
77         struct sync_fence *fence;
78 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 0)
79         struct files_struct *files;
80         struct fdtable *fdt;
81 #endif
82         int fd;
83         struct file *tl_file;
84
85         tl_file = fget(tl_fd);
86         if (tl_file == NULL)
87                 return -EBADF;
88
89         if (tl_file->f_op != &stream_fops) {
90                 fd = -EBADF;
91                 goto out;
92         }
93
94         tl = tl_file->private_data;
95
96         pt = kbase_sync_pt_alloc(tl);
97         if (!pt) {
98                 fd = -EFAULT;
99                 goto out;
100         }
101
102         fence = sync_fence_create("mali_fence", pt);
103         if (!fence) {
104                 sync_pt_free(pt);
105                 fd = -EFAULT;
106                 goto out;
107         }
108
109         /* from here the fence owns the sync_pt */
110
111         /* create a fd representing the fence */
112 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 0)
113         fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
114         if (fd < 0) {
115                 sync_fence_put(fence);
116                 goto out;
117         }
118 #else
119         fd = get_unused_fd();
120         if (fd < 0) {
121                 sync_fence_put(fence);
122                 goto out;
123         }
124
125         files = current->files;
126         spin_lock(&files->file_lock);
127         fdt = files_fdtable(files);
128 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0)
129         __set_close_on_exec(fd, fdt);
130 #else
131         FD_SET(fd, fdt->close_on_exec);
132 #endif
133         spin_unlock(&files->file_lock);
134 #endif  /* LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 0) */
135
136         /* bind fence to the new fd */
137         sync_fence_install(fence, fd);
138
139  out:
140         fput(tl_file);
141
142         return fd;
143 }
144
145 int kbase_fence_validate(int fd)
146 {
147         struct sync_fence *fence;
148
149         fence = sync_fence_fdget(fd);
150         if (!fence)
151                 return -EINVAL;
152
153         sync_fence_put(fence);
154         return 0;
155 }
156
157 #endif                          /* CONFIG_SYNC */