gpu: pvr: pdumpfs: add stream_frames debugfs entry
[sgx.git] / pvr / module.c
1 /**********************************************************************
2  *
3  * Copyright(c) 2008 Imagination Technologies Ltd. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful but, except
10  * as otherwise stated in writing, without any warranty; without even the
11  * implied warranty of merchantability or fitness for a particular purpose.
12  * See the GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17  *
18  * The full GNU General Public License is included in this distribution in
19  * the file called "COPYING".
20  *
21  * Contact Information:
22  * Imagination Technologies Ltd. <gpl-support@imgtec.com>
23  * Home Park Estate, Kings Langley, Herts, WD4 8LZ, UK
24  *
25  ******************************************************************************/
26
27 #include <linux/init.h>
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/version.h>
31 #include <linux/fs.h>
32 #include <linux/proc_fs.h>
33 #include <linux/miscdevice.h>
34
35 #include <linux/platform_device.h>
36
37 #include "img_defs.h"
38 #include "services.h"
39 #include "kerneldisplay.h"
40 #include "kernelbuffer.h"
41 #include "syscommon.h"
42 #include "pvrmmap.h"
43 #include "mutils.h"
44 #include "mm.h"
45 #include "mmap.h"
46 #include "pvr_debug.h"
47 #include "srvkm.h"
48 #include "perproc.h"
49 #include "handle.h"
50 #include "pvr_bridge_km.h"
51 #include "sgx_bridge_km.h"
52 #include "proc.h"
53 #include "pvrmodule.h"
54 #include "private_data.h"
55 #include "pvr_events.h"
56
57 #ifdef CONFIG_DEBUG_FS
58 #include "pvr_debugfs.h"
59 #endif
60
61 #define DRVNAME         "pvrsrvkm"
62
63 #ifdef CONFIG_PVR_DEBUG_EXTRA
64 static int debug = DBGPRIV_WARNING;
65 #include <linux/moduleparam.h>
66 module_param(debug, int, 0);
67 #endif
68
69 static int pvr_open(struct inode unref__ * inode, struct file *filp)
70 {
71         struct PVRSRV_FILE_PRIVATE_DATA *priv;
72         void *block_alloc;
73         int ret = -ENOMEM;
74         enum PVRSRV_ERROR err;
75         u32 pid;
76
77         pvr_lock();
78
79         if (pvr_is_disabled()) {
80                 ret = -ENODEV;
81                 goto err_unlock;
82         }
83
84         pid = OSGetCurrentProcessIDKM();
85
86         if (PVRSRVProcessConnect(pid) != PVRSRV_OK)
87                 goto err_unlock;
88
89         err = OSAllocMem(PVRSRV_OS_NON_PAGEABLE_HEAP,
90                             sizeof(*priv),
91                             (void **)&priv, &block_alloc);
92
93         if (err != PVRSRV_OK)
94                 goto err_unlock;
95
96         priv->ui32OpenPID = pid;
97         priv->hBlockAlloc = block_alloc;
98         filp->private_data = priv;
99
100         INIT_LIST_HEAD(&priv->event_list);
101         init_waitqueue_head(&priv->event_wait);
102         priv->event_space = 4096; /* set aside 4k for event buffer */
103
104         ret = 0;
105 err_unlock:
106         pvr_unlock();
107
108         return ret;
109 }
110
111 static int pvr_release(struct inode unref__ * inode, struct file *filp)
112 {
113         struct PVRSRV_FILE_PRIVATE_DATA *priv;
114
115         pvr_lock();
116
117         priv = filp->private_data;
118
119         pvr_release_events(priv);
120
121         PVRSRVProcessDisconnect(priv->ui32OpenPID);
122
123         OSFreeMem(PVRSRV_OS_NON_PAGEABLE_HEAP,
124                   sizeof(*priv),
125                   priv, priv->hBlockAlloc);
126
127         pvr_unlock();
128
129         return 0;
130 }
131
132 static const struct file_operations pvr_fops = {
133         .owner          = THIS_MODULE,
134         .unlocked_ioctl = PVRSRV_BridgeDispatchKM,
135         .open           = pvr_open,
136         .release        = pvr_release,
137         .mmap           = PVRMMap,
138         .poll           = pvr_poll,
139         .read           = pvr_read,
140 };
141
142 static void pvr_shutdown(struct platform_device *pdev)
143 {
144         PVR_TRACE("pvr_shutdown(pdev=%p)", pdev);
145
146         (void)PVRSRVSetPowerStateKM(PVRSRV_POWER_STATE_D3);
147 }
148
149 static int pvr_suspend(struct platform_device *pdev, pm_message_t state)
150 {
151         PVR_TRACE("pvr_suspend(pdev=%p)", pdev);
152
153         if (PVRSRVSetPowerStateKM(PVRSRV_POWER_STATE_D3) != PVRSRV_OK)
154                 return -EINVAL;
155         return 0;
156 }
157
158 static int pvr_resume(struct platform_device *pdev)
159 {
160         PVR_TRACE("pvr_resume(pdev=%p)", pdev);
161
162         if (PVRSRVSetPowerStateKM(PVRSRV_POWER_STATE_D0) != PVRSRV_OK)
163                 return -EINVAL;
164         return 0;
165 }
166
167 static struct miscdevice pvr_miscdevice = {
168         .minor = MISC_DYNAMIC_MINOR,
169         .name = DRVNAME,
170         .fops = &pvr_fops,
171 };
172
173 static int __devinit pvr_probe(struct platform_device *pdev)
174 {
175         struct SYS_DATA *sysdata;
176         int ret;
177
178         PVR_TRACE("pvr_probe(pdev=%p)", pdev);
179
180         if (SysAcquireData(&sysdata) != PVRSRV_OK &&
181             SysInitialise(pdev) != PVRSRV_OK) {
182                 ret = -ENODEV;
183                 goto err_exit;
184         }
185
186         ret = misc_register(&pvr_miscdevice);
187         if (ret < 0)
188                 goto err_exit;
189
190         return 0;
191
192 err_exit:
193         dev_err(&pdev->dev, "probe failed (%d)\n", ret);
194
195         return ret;
196 }
197
198 static int __devexit pvr_remove(struct platform_device *pdev)
199 {
200         struct SYS_DATA *sysdata;
201         int ret;
202
203         PVR_TRACE("pvr_remove(pdev=%p)", pdev);
204
205         ret = misc_deregister(&pvr_miscdevice);
206         if (ret < 0) {
207                 dev_err(&pdev->dev, "remove failed (%d)\n", ret);
208                 return ret;
209         }
210
211         if (SysAcquireData(&sysdata) == PVRSRV_OK)
212                 SysDeinitialise(sysdata);
213
214         return 0;
215 }
216
217
218 static struct platform_driver pvr_driver = {
219         .driver = {
220                    .name = DRVNAME,
221         },
222         .probe          = pvr_probe,
223         .remove         = __devexit_p(pvr_remove),
224         .suspend        = pvr_suspend,
225         .resume         = pvr_resume,
226         .shutdown       = pvr_shutdown,
227 };
228
229 static int __init pvr_init(void)
230 {
231         int error;
232
233         pvr_dbg_init();
234
235         PVR_TRACE("pvr_init");
236
237 #ifdef CONFIG_PVR_DEBUG_EXTRA
238         PVRDebugSetLevel(debug);
239 #endif
240
241 #ifdef CONFIG_DEBUG_FS
242         pvr_debugfs_init();
243 #endif
244
245         error = CreateProcEntries();
246         if (error < 0)
247                 goto err1;
248
249         error = -ENOMEM;
250         if (LinuxMMInit() != PVRSRV_OK)
251                 goto err2;
252
253         if (LinuxBridgeInit() != PVRSRV_OK)
254                 goto err3;
255
256         PVRMMapInit();
257
258         error = platform_driver_register(&pvr_driver);
259         if (error < 0)
260                 goto err4;
261
262         pvr_init_events();
263
264         return 0;
265
266 err4:
267         PVRMMapCleanup();
268         LinuxBridgeDeInit();
269 err3:
270         LinuxMMCleanup();
271 err2:
272         RemoveProcEntries();
273 err1:
274         pr_err("%s: failed (%d)\n", __func__, error);
275
276         return error;
277 }
278
279 static void __exit pvr_cleanup(void)
280 {
281         PVR_TRACE("pvr_cleanup");
282
283         pvr_exit_events();
284
285         platform_driver_unregister(&pvr_driver);
286
287         PVRMMapCleanup();
288         LinuxMMCleanup();
289         LinuxBridgeDeInit();
290         RemoveProcEntries();
291
292         PVR_TRACE("pvr_cleanup: unloading");
293
294 #ifdef CONFIG_DEBUG_FS
295         pvr_debugfs_cleanup();
296 #endif
297         pvr_dbg_cleanup();
298 }
299
300 module_init(pvr_init);
301 module_exit(pvr_cleanup);
302
303 MODULE_SUPPORTED_DEVICE(DRVNAME);
304 MODULE_ALIAS("platform:" DRVNAME);
305