tizen 2.4 release
[kernel/linux-3.0.git] / drivers / gpu / arm / mali400 / ump / linux / ump_kernel_linux.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 #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 = 2;
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 = 0;
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 #if UMP_LICENSE_IS_GPL
54 static struct dentry *ump_debugfs_dir = NULL;
55 #endif
56
57 /*
58  * The data which we attached to each virtual memory mapping request we get.
59  * Each memory mapping has a reference to the UMP memory it maps.
60  * We release this reference when the last memory mapping is unmapped.
61  */
62 typedef struct ump_vma_usage_tracker
63 {
64         int references;
65         ump_dd_handle handle;
66 } ump_vma_usage_tracker;
67
68 struct ump_device
69 {
70         struct cdev cdev;
71 #if UMP_LICENSE_IS_GPL
72         struct class * ump_class;
73 #endif
74 };
75
76 /* The global variable containing the global device data */
77 static struct ump_device ump_device;
78
79
80 /* Forward declare static functions */
81 static int ump_file_open(struct inode *inode, struct file *filp);
82 static int ump_file_release(struct inode *inode, struct file *filp);
83 #ifdef HAVE_UNLOCKED_IOCTL
84 static long ump_file_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
85 #else
86 static int ump_file_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg);
87 #endif
88 static int ump_file_mmap(struct file * filp, struct vm_area_struct * vma);
89
90
91 /* This variable defines the file operations this UMP device driver offer */
92 static struct file_operations ump_fops =
93 {
94         .owner   = THIS_MODULE,
95         .open    = ump_file_open,
96         .release = ump_file_release,
97 #ifdef HAVE_UNLOCKED_IOCTL
98         .unlocked_ioctl   = ump_file_ioctl,
99 #else
100         .ioctl   = ump_file_ioctl,
101 #endif
102         .mmap    = ump_file_mmap
103 };
104
105
106 /* This function is called by Linux to initialize this module.
107  * All we do is initialize the UMP device driver.
108  */
109 static int ump_initialize_module(void)
110 {
111         _mali_osk_errcode_t err;
112
113         DBG_MSG(2, ("Inserting UMP device driver. Compiled: %s, time: %s\n", __DATE__, __TIME__));
114
115         err = ump_kernel_constructor();
116         if (_MALI_OSK_ERR_OK != err)
117         {
118                 MSG_ERR(("UMP device driver init failed\n"));
119                 return map_errcode(err);
120         }
121
122         MSG(("UMP device driver %s loaded\n", SVN_REV_STRING));
123         return 0;
124 }
125
126
127
128 /*
129  * This function is called by Linux to unload/terminate/exit/cleanup this module.
130  * All we do is terminate the UMP device driver.
131  */
132 static void ump_cleanup_module(void)
133 {
134         DBG_MSG(2, ("Unloading UMP device driver\n"));
135         ump_kernel_destructor();
136         DBG_MSG(2, ("Module unloaded\n"));
137 }
138
139
140
141 static ssize_t ump_memory_used_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
142 {
143         char buf[64];
144         size_t r;
145         u32 mem = _ump_ukk_report_memory_usage();
146
147         r = snprintf(buf, 64, "%u\n", mem);
148         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
149 }
150
151 static const struct file_operations ump_memory_usage_fops = {
152         .owner = THIS_MODULE,
153         .read = ump_memory_used_read,
154 };
155
156 /*
157  * Initialize the UMP device driver.
158  */
159 int ump_kernel_device_initialize(void)
160 {
161         int err;
162         dev_t dev = 0;
163 #if UMP_LICENSE_IS_GPL
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 #endif
174
175         if (0 == ump_major)
176         {
177                 /* auto select a major */
178                 err = alloc_chrdev_region(&dev, 0, 1, ump_dev_name);
179                 ump_major = MAJOR(dev);
180         }
181         else
182         {
183                 /* use load time defined major number */
184                 dev = MKDEV(ump_major, 0);
185                 err = register_chrdev_region(dev, 1, ump_dev_name);
186         }
187
188         if (0 == err)
189         {
190                 memset(&ump_device, 0, sizeof(ump_device));
191
192                 /* initialize our char dev data */
193                 cdev_init(&ump_device.cdev, &ump_fops);
194                 ump_device.cdev.owner = THIS_MODULE;
195                 ump_device.cdev.ops = &ump_fops;
196
197                 /* register char dev with the kernel */
198                 err = cdev_add(&ump_device.cdev, dev, 1/*count*/);
199                 if (0 == err)
200                 {
201
202 #if UMP_LICENSE_IS_GPL
203                         ump_device.ump_class = class_create(THIS_MODULE, ump_dev_name);
204                         if (IS_ERR(ump_device.ump_class))
205                         {
206                                 err = PTR_ERR(ump_device.ump_class);
207                         }
208                         else
209                         {
210                                 struct device * mdev;
211                                 mdev = device_create(ump_device.ump_class, NULL, dev, NULL, ump_dev_name);
212                                 if (!IS_ERR(mdev))
213                                 {
214                                         return 0;
215                                 }
216
217                                 err = PTR_ERR(mdev);
218                         }
219                         cdev_del(&ump_device.cdev);
220 #else
221                         return 0;
222 #endif
223                 }
224
225                 unregister_chrdev_region(dev, 1);
226         }
227
228         return err;
229 }
230
231
232
233 /*
234  * Terminate the UMP device driver
235  */
236 void ump_kernel_device_terminate(void)
237 {
238         dev_t dev = MKDEV(ump_major, 0);
239
240 #if UMP_LICENSE_IS_GPL
241         device_destroy(ump_device.ump_class, dev);
242         class_destroy(ump_device.ump_class);
243 #endif
244
245         /* unregister char device */
246         cdev_del(&ump_device.cdev);
247
248         /* free major */
249         unregister_chrdev_region(dev, 1);
250
251 #if UMP_LICENSE_IS_GPL
252         if(ump_debugfs_dir)
253                 debugfs_remove_recursive(ump_debugfs_dir);
254 #endif
255 }
256
257 /*
258  * Open a new session. User space has called open() on us.
259  */
260 static int ump_file_open(struct inode *inode, struct file *filp)
261 {
262         struct ump_session_data * session_data;
263         _mali_osk_errcode_t err;
264
265         /* input validation */
266         if (0 != MINOR(inode->i_rdev))
267         {
268                 MSG_ERR(("Minor not zero in ump_file_open()\n"));
269                 return -ENODEV;
270         }
271
272         /* Call the OS-Independent UMP Open function */
273         err = _ump_ukk_open((void**) &session_data );
274         if( _MALI_OSK_ERR_OK != err )
275         {
276                 MSG_ERR(("Ump failed to open a new session\n"));
277                 return map_errcode( err );
278         }
279
280         filp->private_data = (void*)session_data;
281         filp->f_pos = 0;
282
283         return 0; /* success */
284 }
285
286
287
288 /*
289  * Close a session. User space has called close() or crashed/terminated.
290  */
291 static int ump_file_release(struct inode *inode, struct file *filp)
292 {
293         _mali_osk_errcode_t err;
294
295         err = _ump_ukk_close((void**) &filp->private_data );
296         if( _MALI_OSK_ERR_OK != err )
297         {
298                 return map_errcode( err );
299         }
300
301         return 0;  /* success */
302 }
303
304
305
306 /*
307  * Handle IOCTL requests.
308  */
309 #ifdef HAVE_UNLOCKED_IOCTL
310 static long ump_file_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
311 #else
312 static int ump_file_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
313 #endif
314 {
315         int err = -ENOTTY;
316         void __user * argument;
317         struct ump_session_data * session_data;
318
319 #ifndef HAVE_UNLOCKED_IOCTL
320         (void)inode; /* inode not used */
321 #endif
322
323         session_data = (struct ump_session_data *)filp->private_data;
324         if (NULL == session_data)
325         {
326                 MSG_ERR(("No session data attached to file object\n"));
327                 return -ENOTTY;
328         }
329
330         /* interpret the argument as a user pointer to something */
331         argument = (void __user *)arg;
332
333         switch (cmd)
334         {
335                 case UMP_IOC_QUERY_API_VERSION:
336                         err = ump_get_api_version_wrapper((u32 __user *)argument, session_data);
337                         break;
338
339                 case UMP_IOC_ALLOCATE :
340                         err = ump_allocate_wrapper((u32 __user *)argument, session_data);
341                         break;
342 /* MALI_SEC */
343 #ifdef CONFIG_DMA_SHARED_BUFFER
344                 case UMP_IOC_DMABUF_IMPORT:
345                         err = ump_dmabuf_import_wrapper((u32 __user *)argument,
346                                                         session_data);
347                         break;
348 #endif
349
350                 case UMP_IOC_RELEASE:
351                         err = ump_release_wrapper((u32 __user *)argument, session_data);
352                         break;
353
354                 case UMP_IOC_SIZE_GET:
355                         err = ump_size_get_wrapper((u32 __user *)argument, session_data);
356                         break;
357
358                 case UMP_IOC_MSYNC:
359                         err = ump_msync_wrapper((u32 __user *)argument, session_data);
360                         break;
361
362                 case UMP_IOC_CACHE_OPERATIONS_CONTROL:
363                         err = ump_cache_operations_control_wrapper((u32 __user *)argument, session_data);
364                         break;
365
366                 case UMP_IOC_SWITCH_HW_USAGE:
367                         err = ump_switch_hw_usage_wrapper((u32 __user *)argument, session_data);
368                         break;
369
370                 case UMP_IOC_LOCK:
371                         err = ump_lock_wrapper((u32 __user *)argument, session_data);
372                         break;
373
374                 case UMP_IOC_UNLOCK:
375                         err = ump_unlock_wrapper((u32 __user *)argument, session_data);
376                         break;
377
378                 default:
379                         DBG_MSG(1, ("No handler for IOCTL. cmd: 0x%08x, arg: 0x%08lx\n", cmd, arg));
380                         err = -EFAULT;
381                         break;
382         }
383
384         return err;
385 }
386
387 /*
388  * Handle from OS to map specified virtual memory to specified UMP memory.
389  */
390 static int ump_file_mmap(struct file * filp, struct vm_area_struct * vma)
391 {
392         _ump_uk_map_mem_s args;
393         _mali_osk_errcode_t err;
394         struct ump_session_data * session_data;
395
396         /* Validate the session data */
397         session_data = (struct ump_session_data *)filp->private_data;
398         if (NULL == session_data || NULL == session_data->cookies_map->table->mappings) /* MALI_SEC */
399         {
400                 MSG_ERR(("mmap() called without any session data available\n"));
401                 return -EFAULT;
402         }
403
404         /* Re-pack the arguments that mmap() packed for us */
405         args.ctx = session_data;
406         args.phys_addr = 0;
407         args.size = vma->vm_end - vma->vm_start;
408         args._ukk_private = vma;
409         args.secure_id = vma->vm_pgoff;
410         args.is_cached = 0;
411
412         if (!(vma->vm_flags & VM_SHARED))
413         {
414                 args.is_cached = 1;
415                 vma->vm_flags = vma->vm_flags | VM_SHARED | VM_MAYSHARE  ;
416                 DBG_MSG(3, ("UMP Map function: Forcing the CPU to use cache\n"));
417         }
418         /* By setting this flag, during a process fork; the child process will not have the parent UMP mappings */
419         vma->vm_flags |= VM_DONTCOPY;
420
421         DBG_MSG(4, ("UMP vma->flags: %x\n", vma->vm_flags ));
422
423         /* Call the common mmap handler */
424         err = _ump_ukk_map_mem( &args );
425         if ( _MALI_OSK_ERR_OK != err)
426         {
427                 MSG_ERR(("_ump_ukk_map_mem() failed in function ump_file_mmap()"));
428                 return map_errcode( err );
429         }
430
431         return 0; /* success */
432 }
433
434 /* Export UMP kernel space API functions */
435 EXPORT_SYMBOL(ump_dd_secure_id_get);
436 EXPORT_SYMBOL(ump_dd_handle_create_from_secure_id);
437 EXPORT_SYMBOL(ump_dd_phys_block_count_get);
438 EXPORT_SYMBOL(ump_dd_phys_block_get);
439 EXPORT_SYMBOL(ump_dd_phys_blocks_get);
440 EXPORT_SYMBOL(ump_dd_size_get);
441 EXPORT_SYMBOL(ump_dd_reference_add);
442 EXPORT_SYMBOL(ump_dd_reference_release);
443
444 /* Export our own extended kernel space allocator */
445 EXPORT_SYMBOL(ump_dd_handle_create_from_phys_blocks);
446
447 /* Setup init and exit functions for this module */
448 module_init(ump_initialize_module);
449 module_exit(ump_cleanup_module);
450
451 /* And some module informatio */
452 MODULE_LICENSE(UMP_KERNEL_LINUX_LICENSE);
453 MODULE_AUTHOR("ARM Ltd.");
454 MODULE_VERSION(SVN_REV_STRING);