gpu: pvr: get rid of unnecessary hash lookups for the proc object
[sgx.git] / pvr / bufferclass_example_linux.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/kernel.h>
28 #include <linux/module.h>
29 #include <linux/fs.h>
30 #include <linux/uaccess.h>
31 #include <linux/io.h>
32
33 #include <linux/dma-mapping.h>
34
35 #include "kernelbuffer.h"
36 #include "bufferclass_example.h"
37 #include "bufferclass_example_linux.h"
38 #include "bufferclass_example_private.h"
39 #include "pvrmodule.h"
40
41 #define DEVNAME "bc_example"
42 #define DRVNAME DEVNAME
43
44 MODULE_SUPPORTED_DEVICE(DEVNAME);
45
46 static int AssignedMajorNumber;
47
48 #define unref__ __attribute__ ((unused))
49
50
51
52 void *BCAllocKernelMem(u32 ui32Size)
53 {
54         return kmalloc(ui32Size, GFP_KERNEL);
55 }
56
57 void BCFreeKernelMem(void *pvMem)
58 {
59         kfree(pvMem);
60 }
61
62 enum PVRSRV_ERROR BCAllocContigMemory(u32 ui32Size, void *unref__ * phMemHandle,
63                                       void __iomem **pLinAddr,
64                                       struct IMG_CPU_PHYADDR *pPhysAddr)
65 {
66         dma_addr_t dma;
67         void *pvLinAddr;
68
69         pvLinAddr = dma_alloc_coherent(NULL, ui32Size, &dma, GFP_KERNEL);
70
71         if (pvLinAddr == NULL)
72                 return PVRSRV_ERROR_OUT_OF_MEMORY;
73
74         pPhysAddr->uiAddr = dma;
75         *pLinAddr = (void __force __iomem *)pvLinAddr;
76
77         return PVRSRV_OK;
78 }
79
80 void BCFreeContigMemory(u32 ui32Size, void *unref__ hMemHandle,
81                         void __iomem *LinAddr, struct IMG_CPU_PHYADDR PhysAddr)
82 {
83         dma_free_coherent(NULL, ui32Size, (void __force *)LinAddr,
84                           (dma_addr_t)PhysAddr.uiAddr);
85 }
86
87 struct IMG_SYS_PHYADDR CpuPAddrToSysPAddrBC(struct IMG_CPU_PHYADDR cpu_paddr)
88 {
89         struct IMG_SYS_PHYADDR sys_paddr;
90
91         sys_paddr.uiAddr = cpu_paddr.uiAddr;
92         return sys_paddr;
93 }
94
95 struct IMG_CPU_PHYADDR SysPAddrToCpuPAddrBC(struct IMG_SYS_PHYADDR sys_paddr)
96 {
97
98         struct IMG_CPU_PHYADDR cpu_paddr;
99
100         cpu_paddr.uiAddr = sys_paddr.uiAddr;
101         return cpu_paddr;
102 }
103
104 enum PVRSRV_ERROR BCOpenPVRServices(void **phPVRServices)
105 {
106
107         *phPVRServices = NULL;
108         return PVRSRV_OK;
109 }
110
111 enum PVRSRV_ERROR BCClosePVRServices(void *unref__ hPVRServices)
112 {
113
114         return PVRSRV_OK;
115 }
116
117 enum PVRSRV_ERROR BCGetLibFuncAddr(void *unref__ hExtDrv, char *szFunctionName,
118           IMG_BOOL (**ppfnFuncTable)(struct PVRSRV_BC_BUFFER2SRV_KMJTABLE *))
119 {
120         if (strcmp("PVRGetBufferClassJTable", szFunctionName) != 0)
121                 return PVRSRV_ERROR_INVALID_PARAMS;
122
123         *ppfnFuncTable = PVRGetBufferClassJTable;
124
125         return PVRSRV_OK;
126 }
127
128 static int BC_Example_Bridge(struct inode *inode, struct file *file,
129                              unsigned int cmd, unsigned long arg)
130 {
131         int err = -EFAULT;
132         int command = _IOC_NR(cmd);
133         struct BC_Example_ioctl_package __user *psBridge =
134                         (struct BC_Example_ioctl_package __user *)arg;
135
136         if (!access_ok
137             (VERIFY_WRITE, psBridge, sizeof(struct BC_Example_ioctl_package)))
138                 return err;
139
140         switch (command) {
141         case _IOC_NR(BC_Example_ioctl_fill_buffer):
142                 if (FillBuffer(psBridge->inputparam) == -1)
143                         return err;
144                 break;
145         case _IOC_NR(BC_Example_ioctl_get_buffer_count):
146                 if (GetBufferCount(&psBridge->outputparam) == -1)
147                         return err;
148                 break;
149         default:
150                 return err;
151         }
152
153         return 0;
154 }
155
156 static const struct file_operations bufferclass_example_fops = {
157         .ioctl = BC_Example_Bridge,
158 };
159
160 static int __init BC_Example_ModInit(void)
161 {
162         AssignedMajorNumber =
163             register_chrdev(0, DEVNAME, &bufferclass_example_fops);
164
165         if (AssignedMajorNumber <= 0) {
166                 printk(KERN_ERR DRVNAME
167                        ": BC_Example_ModInit: unable to get major number\n");
168
169                 goto ExitDisable;
170         }
171 #if defined(CONFIG_PVR_DEBUG_EXTRA)
172         printk(KERN_ERR DRVNAME ": BC_Example_ModInit: major device %d\n",
173                AssignedMajorNumber);
174 #endif
175
176
177         if (BC_Example_Init() != PVRSRV_OK) {
178                 printk(KERN_ERR DRVNAME
179                        ": BC_Example_ModInit: can't init device\n");
180                 goto ExitUnregister;
181         }
182
183         return 0;
184
185 ExitUnregister:
186         unregister_chrdev(AssignedMajorNumber, DEVNAME);
187 ExitDisable:
188         return -EBUSY;
189 }
190
191 static void __exit BC_Example_ModCleanup(void)
192 {
193         unregister_chrdev(AssignedMajorNumber, DEVNAME);
194
195         if (BC_Example_Deinit() != PVRSRV_OK)
196                 printk(KERN_ERR DRVNAME
197                        ": BC_Example_ModCleanup: can't deinit device\n");
198
199 }
200
201 module_init(BC_Example_ModInit);
202 module_exit(BC_Example_ModCleanup);