2 * Copyright (C) 2010 ARM Limited. All rights reserved.
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.
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.
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>
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"
34 #include "ump_uk_types.h"
35 #include "ump_ukk_wrappers.h"
36 #include "ump_ukk_ref_wrappers.h"
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");
44 /* By default the module uses any available major, but it's possible to set it at load time to a specific number */
46 module_param(ump_major, int, S_IRUGO); /* r--r--r-- */
47 MODULE_PARM_DESC(ump_major, "Device major number");
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 */
53 static struct dentry *ump_debugfs_dir = NULL;
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.
60 typedef struct ump_vma_usage_tracker
64 } ump_vma_usage_tracker;
69 #if UMP_LICENSE_IS_GPL
70 struct class * ump_class;
74 /* The global variable containing the global device data */
75 static struct ump_device ump_device;
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);
84 static int ump_file_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg);
86 static int ump_file_mmap(struct file * filp, struct vm_area_struct * vma);
88 #ifdef CONFIG_VIDEO_MALI400MP
89 extern int map_errcode( _mali_osk_errcode_t err );
92 /* This variable defines the file operations this UMP device driver offer */
93 static struct file_operations ump_fops =
96 .open = ump_file_open,
97 .release = ump_file_release,
98 #ifdef HAVE_UNLOCKED_IOCTL
99 .unlocked_ioctl = ump_file_ioctl,
101 .ioctl = ump_file_ioctl,
103 .mmap = ump_file_mmap
107 /* This function is called by Linux to initialize this module.
108 * All we do is initialize the UMP device driver.
110 static int ump_initialize_module(void)
112 _mali_osk_errcode_t err;
114 DBG_MSG(2, ("Inserting UMP device driver. Compiled: %s, time: %s\n", __DATE__, __TIME__));
116 err = ump_kernel_constructor();
117 if (_MALI_OSK_ERR_OK != err)
119 MSG_ERR(("UMP device driver init failed\n"));
120 return map_errcode(err);
123 MSG(("UMP device driver %s loaded\n", SVN_REV_STRING));
130 * This function is called by Linux to unload/terminate/exit/cleanup this module.
131 * All we do is terminate the UMP device driver.
133 static void ump_cleanup_module(void)
135 DBG_MSG(2, ("Unloading UMP device driver\n"));
136 ump_kernel_destructor();
137 DBG_MSG(2, ("Module unloaded\n"));
142 static ssize_t ump_memory_used_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
146 u32 mem = _ump_ukk_report_memory_usage();
148 r = snprintf(buf, 64, "%u\n", mem);
149 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
152 static const struct file_operations ump_memory_usage_fops = {
153 .owner = THIS_MODULE,
154 .read = ump_memory_used_read,
158 * Initialize the UMP device driver.
160 int ump_kernel_device_initialize(void)
164 ump_debugfs_dir = debugfs_create_dir(ump_dev_name, NULL);
165 if (ERR_PTR(-ENODEV) == ump_debugfs_dir)
167 ump_debugfs_dir = NULL;
171 debugfs_create_file("memory_usage", 0400, ump_debugfs_dir, NULL, &ump_memory_usage_fops);
176 /* auto select a major */
177 err = alloc_chrdev_region(&dev, 0, 1, ump_dev_name);
178 ump_major = MAJOR(dev);
182 /* use load time defined major number */
183 dev = MKDEV(ump_major, 0);
184 err = register_chrdev_region(dev, 1, ump_dev_name);
189 memset(&ump_device, 0, sizeof(ump_device));
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;
196 /* register char dev with the kernel */
197 err = cdev_add(&ump_device.cdev, dev, 1/*count*/);
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))
205 err = PTR_ERR(ump_device.ump_class);
209 struct device * mdev;
210 mdev = device_create(ump_device.ump_class, NULL, dev, NULL, ump_dev_name);
218 cdev_del(&ump_device.cdev);
224 unregister_chrdev_region(dev, 1);
233 * Terminate the UMP device driver
235 void ump_kernel_device_terminate(void)
237 dev_t dev = MKDEV(ump_major, 0);
239 #if UMP_LICENSE_IS_GPL
240 device_destroy(ump_device.ump_class, dev);
241 class_destroy(ump_device.ump_class);
244 /* unregister char device */
245 cdev_del(&ump_device.cdev);
248 unregister_chrdev_region(dev, 1);
251 debugfs_remove_recursive(ump_debugfs_dir);
255 * Open a new session. User space has called open() on us.
257 static int ump_file_open(struct inode *inode, struct file *filp)
259 struct ump_session_data * session_data;
260 _mali_osk_errcode_t err;
262 /* input validation */
263 if (0 != MINOR(inode->i_rdev))
265 MSG_ERR(("Minor not zero in ump_file_open()\n"));
269 /* Call the OS-Independent UMP Open function */
270 err = _ump_ukk_open((void**) &session_data );
271 if( _MALI_OSK_ERR_OK != err )
273 MSG_ERR(("Ump failed to open a new session\n"));
274 return map_errcode( err );
277 filp->private_data = (void*)session_data;
280 return 0; /* success */
286 * Close a session. User space has called close() or crashed/terminated.
288 static int ump_file_release(struct inode *inode, struct file *filp)
290 _mali_osk_errcode_t err;
292 err = _ump_ukk_close((void**) &filp->private_data );
293 if( _MALI_OSK_ERR_OK != err )
295 return map_errcode( err );
298 return 0; /* success */
304 * Handle IOCTL requests.
306 #ifdef HAVE_UNLOCKED_IOCTL
307 static long ump_file_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
309 static int ump_file_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
313 void __user * argument;
314 struct ump_session_data * session_data;
316 #ifndef HAVE_UNLOCKED_IOCTL
317 (void)inode; /* inode not used */
320 session_data = (struct ump_session_data *)filp->private_data;
321 if (NULL == session_data)
323 MSG_ERR(("No session data attached to file object\n"));
327 /* interpret the argument as a user pointer to something */
328 argument = (void __user *)arg;
332 case UMP_IOC_QUERY_API_VERSION:
333 err = ump_get_api_version_wrapper((u32 __user *)argument, session_data);
336 case UMP_IOC_ALLOCATE :
337 err = ump_allocate_wrapper((u32 __user *)argument, session_data);
340 case UMP_IOC_RELEASE:
341 err = ump_release_wrapper((u32 __user *)argument, session_data);
344 case UMP_IOC_SIZE_GET:
345 err = ump_size_get_wrapper((u32 __user *)argument, session_data);
349 err = ump_msync_wrapper((u32 __user *)argument, session_data);
353 DBG_MSG(1, ("No handler for IOCTL. cmd: 0x%08x, arg: 0x%08lx\n", cmd, arg));
361 #ifndef CONFIG_VIDEO_MALI400MP
362 int map_errcode( _mali_osk_errcode_t err )
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;
380 * Handle from OS to map specified virtual memory to specified UMP memory.
382 static int ump_file_mmap(struct file * filp, struct vm_area_struct * vma)
384 _ump_uk_map_mem_s args;
385 _mali_osk_errcode_t err;
386 struct ump_session_data * session_data;
388 /* Validate the session data */
389 session_data = (struct ump_session_data *)filp->private_data;
390 if (NULL == session_data)
392 MSG_ERR(("mmap() called without any session data available\n"));
396 /* Re-pack the arguments that mmap() packed for us */
397 args.ctx = session_data;
399 args.size = vma->vm_end - vma->vm_start;
400 args._ukk_private = vma;
401 args.secure_id = vma->vm_pgoff;
404 if (!(vma->vm_flags & VM_SHARED))
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"));
411 DBG_MSG(4, ("UMP vma->flags: %x\n", vma->vm_flags ));
413 /* Call the common mmap handler */
414 err = _ump_ukk_map_mem( &args );
415 if ( _MALI_OSK_ERR_OK != err)
417 MSG_ERR(("_ump_ukk_map_mem() failed in function ump_file_mmap()"));
418 return map_errcode( err );
421 return 0; /* success */
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);
437 /* Export our own extended kernel space allocator */
438 EXPORT_SYMBOL(ump_dd_handle_create_from_phys_blocks);
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);
445 /* And some module informatio */
446 MODULE_LICENSE(UMP_KERNEL_LINUX_LICENSE);
447 MODULE_AUTHOR("ARM Ltd.");
448 MODULE_VERSION(SVN_REV_STRING);