upload tizen1.0 source
[kernel/linux-2.6.36.git] / drivers / media / video / samsung / ump / linux / ump_kernel_linux.c
1 /*
2  * Copyright (C) 2010 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 #include <linux/module.h>            /* kernel module definitions */
12 #include <linux/fs.h>                /* file system operations */
13 #include <linux/cdev.h>              /* character device definitions */
14 #include <linux/ioport.h>            /* request_mem_region */
15 #include <linux/mm.h>                /* memory management functions and types */
16 #include <asm/uaccess.h>             /* user space access */
17 #include <asm/atomic.h>
18 #include <linux/device.h>
19 #include <linux/debugfs.h>
20
21 #include "arch/config.h"             /* Configuration for current platform. The symlinc for arch is set by Makefile */
22 #include "ump_ioctl.h"
23 #include "ump_kernel_common.h"
24 #include "ump_kernel_interface.h"
25 #include "ump_kernel_interface_ref_drv.h"
26 #include "ump_kernel_descriptor_mapping.h"
27 #include "ump_kernel_memory_backend.h"
28 #include "ump_kernel_memory_backend_os.h"
29 #include "ump_kernel_memory_backend_dedicated.h"
30 #include "ump_kernel_license.h"
31
32 #include "ump_osk.h"
33 #include "ump_ukk.h"
34 #include "ump_uk_types.h"
35 #include "ump_ukk_wrappers.h"
36 #include "ump_ukk_ref_wrappers.h"
37
38
39 /* Module parameter to control log level */
40 int ump_debug_level = 3;
41 module_param(ump_debug_level, int, S_IRUSR | S_IWUSR | S_IWGRP | S_IRGRP | S_IROTH); /* rw-rw-r-- */
42 MODULE_PARM_DESC(ump_debug_level, "Higher number, more dmesg output");
43
44 /* By default the module uses any available major, but it's possible to set it at load time to a specific number */
45 int ump_major = 243;
46 module_param(ump_major, int, S_IRUGO); /* r--r--r-- */
47 MODULE_PARM_DESC(ump_major, "Device major number");
48
49 /* Name of the UMP device driver */
50 static char ump_dev_name[] = "ump"; /* should be const, but the functions we call requires non-cost */
51
52
53 static struct dentry *ump_debugfs_dir = NULL;
54
55 /*
56  * The data which we attached to each virtual memory mapping request we get.
57  * Each memory mapping has a reference to the UMP memory it maps.
58  * We release this reference when the last memory mapping is unmapped.
59  */
60 typedef struct ump_vma_usage_tracker
61 {
62         int references;
63         ump_dd_handle handle;
64 } ump_vma_usage_tracker;
65
66 struct ump_device
67 {
68         struct cdev cdev;
69 #if UMP_LICENSE_IS_GPL
70         struct class * ump_class;
71 #endif
72 };
73
74 /* The global variable containing the global device data */
75 static struct ump_device ump_device;
76
77
78 /* Forward declare static functions */
79 static int ump_file_open(struct inode *inode, struct file *filp);
80 static int ump_file_release(struct inode *inode, struct file *filp);
81 #ifdef HAVE_UNLOCKED_IOCTL
82 static long ump_file_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
83 #else
84 static int ump_file_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg);
85 #endif
86 static int ump_file_mmap(struct file * filp, struct vm_area_struct * vma);
87
88 #ifdef CONFIG_VIDEO_MALI400MP
89 extern int map_errcode( _mali_osk_errcode_t err );
90 #endif
91
92 /* This variable defines the file operations this UMP device driver offer */
93 static struct file_operations ump_fops =
94 {
95         .owner   = THIS_MODULE,
96         .open    = ump_file_open,
97         .release = ump_file_release,
98 #ifdef HAVE_UNLOCKED_IOCTL
99         .unlocked_ioctl   = ump_file_ioctl,
100 #else
101         .ioctl   = ump_file_ioctl,
102 #endif
103         .mmap    = ump_file_mmap
104 };
105
106
107 /* This function is called by Linux to initialize this module.
108  * All we do is initialize the UMP device driver.
109  */
110 static int ump_initialize_module(void)
111 {
112         _mali_osk_errcode_t err;
113
114         DBG_MSG(2, ("Inserting UMP device driver. Compiled: %s, time: %s\n", __DATE__, __TIME__));
115
116         err = ump_kernel_constructor();
117         if (_MALI_OSK_ERR_OK != err)
118         {
119                 MSG_ERR(("UMP device driver init failed\n"));
120                 return map_errcode(err);
121         }
122
123         MSG(("UMP device driver %s loaded\n", SVN_REV_STRING));
124         return 0;
125 }
126
127
128
129 /*
130  * This function is called by Linux to unload/terminate/exit/cleanup this module.
131  * All we do is terminate the UMP device driver.
132  */
133 static void ump_cleanup_module(void)
134 {
135         DBG_MSG(2, ("Unloading UMP device driver\n"));
136         ump_kernel_destructor();
137         DBG_MSG(2, ("Module unloaded\n"));
138 }
139
140
141
142 static ssize_t ump_memory_used_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
143 {
144         char buf[64];
145         size_t r;
146         u32 mem = _ump_ukk_report_memory_usage();
147
148         r = snprintf(buf, 64, "%u\n", mem);
149         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
150 }
151
152 static const struct file_operations ump_memory_usage_fops = {
153         .owner = THIS_MODULE,
154         .read = ump_memory_used_read,
155 };
156
157 /*
158  * Initialize the UMP device driver.
159  */
160 int ump_kernel_device_initialize(void)
161 {
162         int err;
163         dev_t dev = 0;
164         ump_debugfs_dir = debugfs_create_dir(ump_dev_name, NULL);
165         if (ERR_PTR(-ENODEV) == ump_debugfs_dir)
166         {
167                         ump_debugfs_dir = NULL;
168         }
169         else
170         {
171                 debugfs_create_file("memory_usage", 0400, ump_debugfs_dir, NULL, &ump_memory_usage_fops);
172         }
173
174         if (0 == ump_major)
175         {
176                 /* auto select a major */
177                 err = alloc_chrdev_region(&dev, 0, 1, ump_dev_name);
178                 ump_major = MAJOR(dev);
179         }
180         else
181         {
182                 /* use load time defined major number */
183                 dev = MKDEV(ump_major, 0);
184                 err = register_chrdev_region(dev, 1, ump_dev_name);
185         }
186
187         if (0 == err)
188         {
189                 memset(&ump_device, 0, sizeof(ump_device));
190
191                 /* initialize our char dev data */
192                 cdev_init(&ump_device.cdev, &ump_fops);
193                 ump_device.cdev.owner = THIS_MODULE;
194                 ump_device.cdev.ops = &ump_fops;
195
196                 /* register char dev with the kernel */
197                 err = cdev_add(&ump_device.cdev, dev, 1/*count*/);
198                 if (0 == err)
199                 {
200
201 #if UMP_LICENSE_IS_GPL
202                         ump_device.ump_class = class_create(THIS_MODULE, ump_dev_name);
203                         if (IS_ERR(ump_device.ump_class))
204                         {
205                                 err = PTR_ERR(ump_device.ump_class);
206                         }
207                         else
208                         {
209                                 struct device * mdev;
210                                 mdev = device_create(ump_device.ump_class, NULL, dev, NULL, ump_dev_name);
211                                 if (!IS_ERR(mdev))
212                                 {
213                                         return 0;
214                                 }
215
216                                 err = PTR_ERR(mdev);
217                         }
218                         cdev_del(&ump_device.cdev);
219 #else
220                         return 0;
221 #endif
222                 }
223
224                 unregister_chrdev_region(dev, 1);
225         }
226
227         return err;
228 }
229
230
231
232 /*
233  * Terminate the UMP device driver
234  */
235 void ump_kernel_device_terminate(void)
236 {
237         dev_t dev = MKDEV(ump_major, 0);
238
239 #if UMP_LICENSE_IS_GPL
240         device_destroy(ump_device.ump_class, dev);
241         class_destroy(ump_device.ump_class);
242 #endif
243
244         /* unregister char device */
245         cdev_del(&ump_device.cdev);
246
247         /* free major */
248         unregister_chrdev_region(dev, 1);
249
250         if(ump_debugfs_dir)
251                 debugfs_remove_recursive(ump_debugfs_dir);
252 }
253
254 /*
255  * Open a new session. User space has called open() on us.
256  */
257 static int ump_file_open(struct inode *inode, struct file *filp)
258 {
259         struct ump_session_data * session_data;
260         _mali_osk_errcode_t err;
261
262         /* input validation */
263         if (0 != MINOR(inode->i_rdev))
264         {
265                 MSG_ERR(("Minor not zero in ump_file_open()\n"));
266                 return -ENODEV;
267         }
268
269         /* Call the OS-Independent UMP Open function */
270         err = _ump_ukk_open((void**) &session_data );
271         if( _MALI_OSK_ERR_OK != err )
272         {
273                 MSG_ERR(("Ump failed to open a new session\n"));
274                 return map_errcode( err );
275         }
276
277         filp->private_data = (void*)session_data;
278         filp->f_pos = 0;
279
280         return 0; /* success */
281 }
282
283
284
285 /*
286  * Close a session. User space has called close() or crashed/terminated.
287  */
288 static int ump_file_release(struct inode *inode, struct file *filp)
289 {
290         _mali_osk_errcode_t err;
291
292         err = _ump_ukk_close((void**) &filp->private_data );
293         if( _MALI_OSK_ERR_OK != err )
294         {
295                 return map_errcode( err );
296         }
297
298         return 0;  /* success */
299 }
300
301
302
303 /*
304  * Handle IOCTL requests.
305  */
306 #ifdef HAVE_UNLOCKED_IOCTL
307 static long ump_file_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
308 #else
309 static int ump_file_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
310 #endif
311 {
312         int err = -ENOTTY;
313         void __user * argument;
314         struct ump_session_data * session_data;
315
316 #ifndef HAVE_UNLOCKED_IOCTL
317         (void)inode; /* inode not used */
318 #endif
319
320         session_data = (struct ump_session_data *)filp->private_data;
321         if (NULL == session_data)
322         {
323                 MSG_ERR(("No session data attached to file object\n"));
324                 return -ENOTTY;
325         }
326
327         /* interpret the argument as a user pointer to something */
328         argument = (void __user *)arg;
329
330         switch (cmd)
331         {
332                 case UMP_IOC_QUERY_API_VERSION:
333                         err = ump_get_api_version_wrapper((u32 __user *)argument, session_data);
334                         break;
335
336                 case UMP_IOC_ALLOCATE :
337                         err = ump_allocate_wrapper((u32 __user *)argument, session_data);
338                         break;
339
340                 case UMP_IOC_RELEASE:
341                         err = ump_release_wrapper((u32 __user *)argument, session_data);
342                         break;
343
344                 case UMP_IOC_SIZE_GET:
345                         err = ump_size_get_wrapper((u32 __user *)argument, session_data);
346                         break;
347
348                 case UMP_IOC_MSYNC:
349                         err = ump_msync_wrapper((u32 __user *)argument, session_data);
350                         break;
351
352                 default:
353                         DBG_MSG(1, ("No handler for IOCTL. cmd: 0x%08x, arg: 0x%08lx\n", cmd, arg));
354                         err = -EFAULT;
355                         break;
356         }
357
358         return err;
359 }
360
361 #ifndef CONFIG_VIDEO_MALI400MP
362 int map_errcode( _mali_osk_errcode_t err )
363 {
364     switch(err)
365     {
366         case _MALI_OSK_ERR_OK : return 0;
367         case _MALI_OSK_ERR_FAULT: return -EFAULT;
368         case _MALI_OSK_ERR_INVALID_FUNC: return -ENOTTY;
369         case _MALI_OSK_ERR_INVALID_ARGS: return -EINVAL;
370         case _MALI_OSK_ERR_NOMEM: return -ENOMEM;
371         case _MALI_OSK_ERR_TIMEOUT: return -ETIMEDOUT;
372         case _MALI_OSK_ERR_RESTARTSYSCALL: return -ERESTARTSYS;
373         case _MALI_OSK_ERR_ITEM_NOT_FOUND: return -ENOENT;
374         default: return -EFAULT;
375     }
376 }
377 #endif
378
379 /*
380  * Handle from OS to map specified virtual memory to specified UMP memory.
381  */
382 static int ump_file_mmap(struct file * filp, struct vm_area_struct * vma)
383 {
384         _ump_uk_map_mem_s args;
385         _mali_osk_errcode_t err;
386         struct ump_session_data * session_data;
387
388         /* Validate the session data */
389         session_data = (struct ump_session_data *)filp->private_data;
390         if (NULL == session_data)
391         {
392                 MSG_ERR(("mmap() called without any session data available\n"));
393                 return -EFAULT;
394         }
395
396         /* Re-pack the arguments that mmap() packed for us */
397         args.ctx = session_data;
398         args.phys_addr = 0;
399         args.size = vma->vm_end - vma->vm_start;
400         args._ukk_private = vma;
401         args.secure_id = vma->vm_pgoff;
402         args.is_cached = 0;
403
404         if (!(vma->vm_flags & VM_SHARED))
405         {
406                 args.is_cached = 1;
407                 vma->vm_flags = vma->vm_flags | VM_SHARED | VM_MAYSHARE  ;
408                 DBG_MSG(3, ("UMP Map function: Forcing the CPU to use cache\n"));
409         }
410
411         DBG_MSG(4, ("UMP vma->flags: %x\n", vma->vm_flags ));
412
413         /* Call the common mmap handler */
414         err = _ump_ukk_map_mem( &args );
415         if ( _MALI_OSK_ERR_OK != err)
416         {
417                 MSG_ERR(("_ump_ukk_map_mem() failed in function ump_file_mmap()"));
418                 return map_errcode( err );
419         }
420
421         return 0; /* success */
422 }
423
424 /* Export UMP kernel space API functions */
425 EXPORT_SYMBOL(ump_dd_secure_id_get);
426 EXPORT_SYMBOL(ump_dd_handle_create_from_secure_id);
427 EXPORT_SYMBOL(ump_dd_phys_block_count_get);
428 EXPORT_SYMBOL(ump_dd_phys_block_get);
429 EXPORT_SYMBOL(ump_dd_phys_blocks_get);
430 EXPORT_SYMBOL(ump_dd_size_get);
431 EXPORT_SYMBOL(ump_dd_reference_add);
432 EXPORT_SYMBOL(ump_dd_reference_release);
433 EXPORT_SYMBOL(ump_dd_meminfo_get);
434 EXPORT_SYMBOL(ump_dd_meminfo_set);
435 EXPORT_SYMBOL(ump_dd_handle_get_from_vaddr);
436
437 /* Export our own extended kernel space allocator */
438 EXPORT_SYMBOL(ump_dd_handle_create_from_phys_blocks);
439
440 /* Setup init and exit functions for this module */
441 //module_init(ump_initialize_module);
442 arch_initcall(ump_initialize_module);
443 module_exit(ump_cleanup_module);
444
445 /* And some module informatio */
446 MODULE_LICENSE(UMP_KERNEL_LINUX_LICENSE);
447 MODULE_AUTHOR("ARM Ltd.");
448 MODULE_VERSION(SVN_REV_STRING);