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