MALI: rockchip: upgrade midgard DDK to r13p0-00rel0
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / arm / midgard / mali_kbase_sync_user.c
1 /*
2  *
3  * (C) COPYRIGHT 2012-2016 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
37 static int kbase_stream_close(struct inode *inode, struct file *file)
38 {
39         struct sync_timeline *tl;
40
41         tl = (struct sync_timeline *)file->private_data;
42         BUG_ON(!tl);
43         sync_timeline_destroy(tl);
44         return 0;
45 }
46
47 static const struct file_operations stream_fops = {
48         .owner = THIS_MODULE,
49         .release = kbase_stream_close,
50 };
51
52 int kbase_stream_create(const char *name, int *const out_fd)
53 {
54         struct sync_timeline *tl;
55
56         BUG_ON(!out_fd);
57
58         tl = kbase_sync_timeline_alloc(name);
59         if (!tl)
60                 return -EINVAL;
61
62         *out_fd = anon_inode_getfd(name, &stream_fops, tl, O_RDONLY | O_CLOEXEC);
63
64         if (*out_fd < 0) {
65                 sync_timeline_destroy(tl);
66                 return -EINVAL;
67         }
68
69         return 0;
70 }
71
72 int kbase_stream_create_fence(int tl_fd)
73 {
74         struct sync_timeline *tl;
75         struct sync_pt *pt;
76         struct sync_fence *fence;
77 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 0)
78         struct files_struct *files;
79         struct fdtable *fdt;
80 #endif
81         int fd;
82         struct file *tl_file;
83
84         tl_file = fget(tl_fd);
85         if (tl_file == NULL)
86                 return -EBADF;
87
88         if (tl_file->f_op != &stream_fops) {
89                 fd = -EBADF;
90                 goto out;
91         }
92
93         tl = tl_file->private_data;
94
95         pt = kbase_sync_pt_alloc(tl);
96         if (!pt) {
97                 fd = -EFAULT;
98                 goto out;
99         }
100
101         fence = sync_fence_create("mali_fence", pt);
102         if (!fence) {
103                 sync_pt_free(pt);
104                 fd = -EFAULT;
105                 goto out;
106         }
107
108         /* from here the fence owns the sync_pt */
109
110         /* create a fd representing the fence */
111 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 0)
112         fd = get_unused_fd_flags(O_RDWR | O_CLOEXEC);
113         if (fd < 0) {
114                 sync_fence_put(fence);
115                 goto out;
116         }
117 #else
118         fd = get_unused_fd();
119         if (fd < 0) {
120                 sync_fence_put(fence);
121                 goto out;
122         }
123
124         files = current->files;
125         spin_lock(&files->file_lock);
126         fdt = files_fdtable(files);
127 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0)
128         __set_close_on_exec(fd, fdt);
129 #else
130         FD_SET(fd, fdt->close_on_exec);
131 #endif
132         spin_unlock(&files->file_lock);
133 #endif  /* LINUX_VERSION_CODE >= KERNEL_VERSION(3, 7, 0) */
134
135         /* bind fence to the new fd */
136         sync_fence_install(fence, fd);
137
138  out:
139         fput(tl_file);
140
141         return fd;
142 }
143
144 int kbase_fence_validate(int fd)
145 {
146         struct sync_fence *fence;
147
148         fence = sync_fence_fdget(fd);
149         if (!fence)
150                 return -EINVAL;
151
152         sync_fence_put(fence);
153         return 0;
154 }
155
156 #endif                          /* CONFIG_SYNC */