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