2 * Copyright (C) 2010-2014, 2016 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 {
65 } 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;
76 struct device *ump_global_mdev = NULL;
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);
89 /* This variable defines the file operations this UMP device driver offer */
90 static struct file_operations ump_fops = {
92 .open = ump_file_open,
93 .release = ump_file_release,
94 #ifdef HAVE_UNLOCKED_IOCTL
95 .unlocked_ioctl = ump_file_ioctl,
97 .ioctl = ump_file_ioctl,
103 /* This function is called by Linux to initialize this module.
104 * All we do is initialize the UMP device driver.
106 static int ump_initialize_module(void)
108 _mali_osk_errcode_t err;
110 DBG_MSG(2, ("Inserting UMP device driver. Compiled: %s, time: %s\n", __DATE__, __TIME__));
112 err = ump_kernel_constructor();
113 if (_MALI_OSK_ERR_OK != err) {
114 MSG_ERR(("UMP device driver init failed\n"));
115 return ump_map_errcode(err);
118 MSG(("UMP device driver %s loaded\n", SVN_REV_STRING));
125 * This function is called by Linux to unload/terminate/exit/cleanup this module.
126 * All we do is terminate the UMP device driver.
128 static void ump_cleanup_module(void)
130 DBG_MSG(2, ("Unloading UMP device driver\n"));
131 ump_kernel_destructor();
132 DBG_MSG(2, ("Module unloaded\n"));
137 static ssize_t ump_memory_used_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
141 u32 mem = _ump_ukk_report_memory_usage();
143 r = snprintf(buf, 64, "%u\n", mem);
144 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
147 static const struct file_operations ump_memory_usage_fops = {
148 .owner = THIS_MODULE,
149 .read = ump_memory_used_read,
153 * Initialize the UMP device driver.
155 int ump_kernel_device_initialize(void)
159 #if UMP_LICENSE_IS_GPL
160 ump_debugfs_dir = debugfs_create_dir(ump_dev_name, NULL);
161 if (ERR_PTR(-ENODEV) == ump_debugfs_dir) {
162 ump_debugfs_dir = NULL;
164 debugfs_create_file("memory_usage", 0400, ump_debugfs_dir, NULL, &ump_memory_usage_fops);
168 if (0 == ump_major) {
169 /* auto select a major */
170 err = alloc_chrdev_region(&dev, 0, 1, ump_dev_name);
171 ump_major = MAJOR(dev);
173 /* use load time defined major number */
174 dev = MKDEV(ump_major, 0);
175 err = register_chrdev_region(dev, 1, ump_dev_name);
179 memset(&ump_device, 0, sizeof(ump_device));
181 /* initialize our char dev data */
182 cdev_init(&ump_device.cdev, &ump_fops);
183 ump_device.cdev.owner = THIS_MODULE;
184 ump_device.cdev.ops = &ump_fops;
186 /* register char dev with the kernel */
187 err = cdev_add(&ump_device.cdev, dev, 1/*count*/);
190 #if UMP_LICENSE_IS_GPL
191 ump_device.ump_class = class_create(THIS_MODULE, ump_dev_name);
192 if (IS_ERR(ump_device.ump_class)) {
193 err = PTR_ERR(ump_device.ump_class);
195 ump_global_mdev = device_create(ump_device.ump_class, NULL, dev, NULL, ump_dev_name);
196 if (!IS_ERR(ump_global_mdev)) {
200 err = PTR_ERR(ump_global_mdev);
202 cdev_del(&ump_device.cdev);
208 unregister_chrdev_region(dev, 1);
217 * Terminate the UMP device driver
219 void ump_kernel_device_terminate(void)
221 dev_t dev = MKDEV(ump_major, 0);
223 #if UMP_LICENSE_IS_GPL
224 device_destroy(ump_device.ump_class, dev);
225 class_destroy(ump_device.ump_class);
228 /* unregister char device */
229 cdev_del(&ump_device.cdev);
232 unregister_chrdev_region(dev, 1);
234 #if UMP_LICENSE_IS_GPL
236 debugfs_remove_recursive(ump_debugfs_dir);
241 * Open a new session. User space has called open() on us.
243 static int ump_file_open(struct inode *inode, struct file *filp)
245 struct ump_session_data *session_data;
246 _mali_osk_errcode_t err;
248 /* input validation */
249 if (0 != MINOR(inode->i_rdev)) {
250 MSG_ERR(("Minor not zero in ump_file_open()\n"));
254 /* Call the OS-Independent UMP Open function */
255 err = _ump_ukk_open((void **) &session_data);
256 if (_MALI_OSK_ERR_OK != err) {
257 MSG_ERR(("Ump failed to open a new session\n"));
258 return ump_map_errcode(err);
261 filp->private_data = (void *)session_data;
264 return 0; /* success */
270 * Close a session. User space has called close() or crashed/terminated.
272 static int ump_file_release(struct inode *inode, struct file *filp)
274 _mali_osk_errcode_t err;
276 err = _ump_ukk_close((void **) &filp->private_data);
277 if (_MALI_OSK_ERR_OK != err) {
278 return ump_map_errcode(err);
281 return 0; /* success */
287 * Handle IOCTL requests.
289 #ifdef HAVE_UNLOCKED_IOCTL
290 static long ump_file_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
292 static int ump_file_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
296 void __user *argument;
297 struct ump_session_data *session_data;
299 #ifndef HAVE_UNLOCKED_IOCTL
300 (void)inode; /* inode not used */
303 session_data = (struct ump_session_data *)filp->private_data;
304 if (NULL == session_data) {
305 MSG_ERR(("No session data attached to file object\n"));
309 /* interpret the argument as a user pointer to something */
310 argument = (void __user *)arg;
313 case UMP_IOC_QUERY_API_VERSION:
314 err = ump_get_api_version_wrapper((u32 __user *)argument, session_data);
317 case UMP_IOC_ALLOCATE :
318 err = ump_allocate_wrapper((u32 __user *)argument, session_data);
321 case UMP_IOC_RELEASE:
322 err = ump_release_wrapper((u32 __user *)argument, session_data);
325 case UMP_IOC_SIZE_GET:
326 err = ump_size_get_wrapper((u32 __user *)argument, session_data);
330 err = ump_msync_wrapper((u32 __user *)argument, session_data);
333 case UMP_IOC_CACHE_OPERATIONS_CONTROL:
334 err = ump_cache_operations_control_wrapper((u32 __user *)argument, session_data);
337 case UMP_IOC_SWITCH_HW_USAGE:
338 err = ump_switch_hw_usage_wrapper((u32 __user *)argument, session_data);
342 err = ump_lock_wrapper((u32 __user *)argument, session_data);
346 err = ump_unlock_wrapper((u32 __user *)argument, session_data);
349 case UMP_IOC_DMABUF_IMPORT:
350 #ifdef CONFIG_DMA_SHARED_BUFFER
351 err = ump_dmabuf_import_wrapper((u32 __user *)argument, session_data);
354 DBG_MSG(1, ("User space use dmabuf API, but kernel don't support DMA BUF\n"));
359 DBG_MSG(1, ("No handler for IOCTL. cmd: 0x%08x, arg: 0x%08lx\n", cmd, arg));
367 int ump_map_errcode(_mali_osk_errcode_t err)
370 case _MALI_OSK_ERR_OK :
372 case _MALI_OSK_ERR_FAULT:
374 case _MALI_OSK_ERR_INVALID_FUNC:
376 case _MALI_OSK_ERR_INVALID_ARGS:
378 case _MALI_OSK_ERR_NOMEM:
380 case _MALI_OSK_ERR_TIMEOUT:
382 case _MALI_OSK_ERR_RESTARTSYSCALL:
384 case _MALI_OSK_ERR_ITEM_NOT_FOUND:
392 * Handle from OS to map specified virtual memory to specified UMP memory.
394 static int ump_file_mmap(struct file *filp, struct vm_area_struct *vma)
396 _ump_uk_map_mem_s args;
397 _mali_osk_errcode_t err;
398 struct ump_session_data *session_data;
400 /* Validate the session data */
401 session_data = (struct ump_session_data *)filp->private_data;
402 if (NULL == session_data) {
403 MSG_ERR(("mmap() called without any session data available\n"));
407 /* Re-pack the arguments that mmap() packed for us */
408 args.ctx = session_data;
410 args.size = vma->vm_end - vma->vm_start;
411 args._ukk_private = vma;
412 args.secure_id = vma->vm_pgoff;
415 if (!(vma->vm_flags & VM_SHARED)) {
417 vma->vm_flags = vma->vm_flags | VM_SHARED | VM_MAYSHARE ;
418 DBG_MSG(3, ("UMP Map function: Forcing the CPU to use cache\n"));
420 /* By setting this flag, during a process fork; the child process will not have the parent UMP mappings */
421 vma->vm_flags |= VM_DONTCOPY;
423 DBG_MSG(4, ("UMP vma->flags: %x\n", vma->vm_flags));
425 /* Call the common mmap handler */
426 err = _ump_ukk_map_mem(&args);
427 if (_MALI_OSK_ERR_OK != err) {
428 MSG_ERR(("_ump_ukk_map_mem() failed in function ump_file_mmap()"));
429 return ump_map_errcode(err);
432 return 0; /* success */
435 /* Export UMP kernel space API functions */
436 EXPORT_SYMBOL(ump_dd_secure_id_get);
437 EXPORT_SYMBOL(ump_dd_handle_create_from_secure_id);
438 EXPORT_SYMBOL(ump_dd_phys_block_count_get);
439 EXPORT_SYMBOL(ump_dd_phys_block_get);
440 EXPORT_SYMBOL(ump_dd_phys_blocks_get);
441 EXPORT_SYMBOL(ump_dd_size_get);
442 EXPORT_SYMBOL(ump_dd_reference_add);
443 EXPORT_SYMBOL(ump_dd_reference_release);
445 /* Export our own extended kernel space allocator */
446 EXPORT_SYMBOL(ump_dd_handle_create_from_phys_blocks);
448 /* Setup init and exit functions for this module */
449 module_init(ump_initialize_module);
450 module_exit(ump_cleanup_module);
452 /* And some module informatio */
453 MODULE_LICENSE(UMP_KERNEL_LINUX_LICENSE);
454 MODULE_AUTHOR("ARM Ltd.");
455 MODULE_VERSION(SVN_REV_STRING);