2 * Copyright (C) 2011-2012 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 = 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");
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 #if UMP_LICENSE_IS_GPL
54 static struct dentry *ump_debugfs_dir = NULL;
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.
62 typedef struct ump_vma_usage_tracker
66 } ump_vma_usage_tracker;
71 #if UMP_LICENSE_IS_GPL
72 struct class * ump_class;
76 /* The global variable containing the global device data */
77 static struct ump_device ump_device;
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);
86 static int ump_file_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg);
88 static int ump_file_mmap(struct file * filp, struct vm_area_struct * vma);
91 /* This variable defines the file operations this UMP device driver offer */
92 static struct file_operations ump_fops =
95 .open = ump_file_open,
96 .release = ump_file_release,
97 #ifdef HAVE_UNLOCKED_IOCTL
98 .unlocked_ioctl = ump_file_ioctl,
100 .ioctl = ump_file_ioctl,
102 .mmap = ump_file_mmap
106 /* This function is called by Linux to initialize this module.
107 * All we do is initialize the UMP device driver.
109 static int ump_initialize_module(void)
111 _mali_osk_errcode_t err;
113 DBG_MSG(2, ("Inserting UMP device driver. Compiled: %s, time: %s\n", __DATE__, __TIME__));
115 err = ump_kernel_constructor();
116 if (_MALI_OSK_ERR_OK != err)
118 MSG_ERR(("UMP device driver init failed\n"));
119 return map_errcode(err);
122 MSG(("UMP device driver %s loaded\n", SVN_REV_STRING));
129 * This function is called by Linux to unload/terminate/exit/cleanup this module.
130 * All we do is terminate the UMP device driver.
132 static void ump_cleanup_module(void)
134 DBG_MSG(2, ("Unloading UMP device driver\n"));
135 ump_kernel_destructor();
136 DBG_MSG(2, ("Module unloaded\n"));
141 static ssize_t ump_memory_used_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
145 u32 mem = _ump_ukk_report_memory_usage();
147 r = snprintf(buf, 64, "%u\n", mem);
148 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
151 static const struct file_operations ump_memory_usage_fops = {
152 .owner = THIS_MODULE,
153 .read = ump_memory_used_read,
157 * Initialize the UMP device driver.
159 int ump_kernel_device_initialize(void)
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)
167 ump_debugfs_dir = NULL;
171 debugfs_create_file("memory_usage", 0400, ump_debugfs_dir, NULL, &ump_memory_usage_fops);
177 /* auto select a major */
178 err = alloc_chrdev_region(&dev, 0, 1, ump_dev_name);
179 ump_major = MAJOR(dev);
183 /* use load time defined major number */
184 dev = MKDEV(ump_major, 0);
185 err = register_chrdev_region(dev, 1, ump_dev_name);
190 memset(&ump_device, 0, sizeof(ump_device));
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;
197 /* register char dev with the kernel */
198 err = cdev_add(&ump_device.cdev, dev, 1/*count*/);
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))
206 err = PTR_ERR(ump_device.ump_class);
210 struct device * mdev;
211 mdev = device_create(ump_device.ump_class, NULL, dev, NULL, ump_dev_name);
219 cdev_del(&ump_device.cdev);
225 unregister_chrdev_region(dev, 1);
234 * Terminate the UMP device driver
236 void ump_kernel_device_terminate(void)
238 dev_t dev = MKDEV(ump_major, 0);
240 #if UMP_LICENSE_IS_GPL
241 device_destroy(ump_device.ump_class, dev);
242 class_destroy(ump_device.ump_class);
245 /* unregister char device */
246 cdev_del(&ump_device.cdev);
249 unregister_chrdev_region(dev, 1);
251 #if UMP_LICENSE_IS_GPL
253 debugfs_remove_recursive(ump_debugfs_dir);
258 * Open a new session. User space has called open() on us.
260 static int ump_file_open(struct inode *inode, struct file *filp)
262 struct ump_session_data * session_data;
263 _mali_osk_errcode_t err;
265 /* input validation */
266 if (0 != MINOR(inode->i_rdev))
268 MSG_ERR(("Minor not zero in ump_file_open()\n"));
272 /* Call the OS-Independent UMP Open function */
273 err = _ump_ukk_open((void**) &session_data );
274 if( _MALI_OSK_ERR_OK != err )
276 MSG_ERR(("Ump failed to open a new session\n"));
277 return map_errcode( err );
280 filp->private_data = (void*)session_data;
283 return 0; /* success */
289 * Close a session. User space has called close() or crashed/terminated.
291 static int ump_file_release(struct inode *inode, struct file *filp)
293 _mali_osk_errcode_t err;
295 err = _ump_ukk_close((void**) &filp->private_data );
296 if( _MALI_OSK_ERR_OK != err )
298 return map_errcode( err );
301 return 0; /* success */
307 * Handle IOCTL requests.
309 #ifdef HAVE_UNLOCKED_IOCTL
310 static long ump_file_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
312 static int ump_file_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
316 void __user * argument;
317 struct ump_session_data * session_data;
319 #ifndef HAVE_UNLOCKED_IOCTL
320 (void)inode; /* inode not used */
323 session_data = (struct ump_session_data *)filp->private_data;
324 if (NULL == session_data)
326 MSG_ERR(("No session data attached to file object\n"));
330 /* interpret the argument as a user pointer to something */
331 argument = (void __user *)arg;
335 case UMP_IOC_QUERY_API_VERSION:
336 err = ump_get_api_version_wrapper((u32 __user *)argument, session_data);
339 case UMP_IOC_ALLOCATE :
340 err = ump_allocate_wrapper((u32 __user *)argument, session_data);
343 #ifdef CONFIG_DMA_SHARED_BUFFER
344 case UMP_IOC_DMABUF_IMPORT:
345 err = ump_dmabuf_import_wrapper((u32 __user *)argument,
350 case UMP_IOC_RELEASE:
351 err = ump_release_wrapper((u32 __user *)argument, session_data);
354 case UMP_IOC_SIZE_GET:
355 err = ump_size_get_wrapper((u32 __user *)argument, session_data);
359 err = ump_msync_wrapper((u32 __user *)argument, session_data);
362 case UMP_IOC_CACHE_OPERATIONS_CONTROL:
363 err = ump_cache_operations_control_wrapper((u32 __user *)argument, session_data);
366 case UMP_IOC_SWITCH_HW_USAGE:
367 err = ump_switch_hw_usage_wrapper((u32 __user *)argument, session_data);
371 err = ump_lock_wrapper((u32 __user *)argument, session_data);
375 err = ump_unlock_wrapper((u32 __user *)argument, session_data);
379 DBG_MSG(1, ("No handler for IOCTL. cmd: 0x%08x, arg: 0x%08lx\n", cmd, arg));
388 * Handle from OS to map specified virtual memory to specified UMP memory.
390 static int ump_file_mmap(struct file * filp, struct vm_area_struct * vma)
392 _ump_uk_map_mem_s args;
393 _mali_osk_errcode_t err;
394 struct ump_session_data * session_data;
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 */
400 MSG_ERR(("mmap() called without any session data available\n"));
404 /* Re-pack the arguments that mmap() packed for us */
405 args.ctx = session_data;
407 args.size = vma->vm_end - vma->vm_start;
408 args._ukk_private = vma;
409 args.secure_id = vma->vm_pgoff;
412 if (!(vma->vm_flags & VM_SHARED))
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"));
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;
421 DBG_MSG(4, ("UMP vma->flags: %x\n", vma->vm_flags ));
423 /* Call the common mmap handler */
424 err = _ump_ukk_map_mem( &args );
425 if ( _MALI_OSK_ERR_OK != err)
427 MSG_ERR(("_ump_ukk_map_mem() failed in function ump_file_mmap()"));
428 return map_errcode( err );
431 return 0; /* success */
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);
444 /* Export our own extended kernel space allocator */
445 EXPORT_SYMBOL(ump_dd_handle_create_from_phys_blocks);
447 /* Setup init and exit functions for this module */
448 module_init(ump_initialize_module);
449 module_exit(ump_cleanup_module);
451 /* And some module informatio */
452 MODULE_LICENSE(UMP_KERNEL_LINUX_LICENSE);
453 MODULE_AUTHOR("ARM Ltd.");
454 MODULE_VERSION(SVN_REV_STRING);