ump_kernel_linux.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. * Copyright (C) 2010-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. #include <linux/module.h> /* kernel module definitions */
  11. #include <linux/fs.h> /* file system operations */
  12. #include <linux/cdev.h> /* character device definitions */
  13. #include <linux/ioport.h> /* request_mem_region */
  14. #include <linux/mm.h> /* memory management functions and types */
  15. #include <asm/uaccess.h> /* user space access */
  16. #include <asm/atomic.h>
  17. #include <linux/device.h>
  18. #include <linux/debugfs.h>
  19. #include "arch/config.h" /* Configuration for current platform. The symlinc for arch is set by Makefile */
  20. #include "ump_ioctl.h"
  21. #include "ump_kernel_common.h"
  22. #include "ump_kernel_interface.h"
  23. #include "ump_kernel_interface_ref_drv.h"
  24. #include "ump_kernel_descriptor_mapping.h"
  25. #include "ump_kernel_memory_backend.h"
  26. #include "ump_kernel_memory_backend_os.h"
  27. #include "ump_kernel_memory_backend_dedicated.h"
  28. #include "ump_kernel_license.h"
  29. #include "ump_osk.h"
  30. #include "ump_ukk.h"
  31. #include "ump_uk_types.h"
  32. #include "ump_ukk_wrappers.h"
  33. #include "ump_ukk_ref_wrappers.h"
  34. /* Module parameter to control log level */
  35. int ump_debug_level = 2;
  36. module_param(ump_debug_level, int, S_IRUSR | S_IWUSR | S_IWGRP | S_IRGRP | S_IROTH); /* rw-rw-r-- */
  37. MODULE_PARM_DESC(ump_debug_level, "Higher number, more dmesg output");
  38. /* By default the module uses any available major, but it's possible to set it at load time to a specific number */
  39. int ump_major = 0;
  40. module_param(ump_major, int, S_IRUGO); /* r--r--r-- */
  41. MODULE_PARM_DESC(ump_major, "Device major number");
  42. /* Name of the UMP device driver */
  43. static char ump_dev_name[] = "ump"; /* should be const, but the functions we call requires non-cost */
  44. #if UMP_LICENSE_IS_GPL
  45. static struct dentry *ump_debugfs_dir = NULL;
  46. #endif
  47. /*
  48. * The data which we attached to each virtual memory mapping request we get.
  49. * Each memory mapping has a reference to the UMP memory it maps.
  50. * We release this reference when the last memory mapping is unmapped.
  51. */
  52. typedef struct ump_vma_usage_tracker
  53. {
  54. int references;
  55. ump_dd_handle handle;
  56. } ump_vma_usage_tracker;
  57. struct ump_device
  58. {
  59. struct cdev cdev;
  60. #if UMP_LICENSE_IS_GPL
  61. struct class * ump_class;
  62. #endif
  63. };
  64. /* The global variable containing the global device data */
  65. static struct ump_device ump_device;
  66. /* Forward declare static functions */
  67. static int ump_file_open(struct inode *inode, struct file *filp);
  68. static int ump_file_release(struct inode *inode, struct file *filp);
  69. #ifdef HAVE_UNLOCKED_IOCTL
  70. static long ump_file_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
  71. #else
  72. static int ump_file_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg);
  73. #endif
  74. static int ump_file_mmap(struct file * filp, struct vm_area_struct * vma);
  75. /* This variable defines the file operations this UMP device driver offer */
  76. static struct file_operations ump_fops =
  77. {
  78. .owner = THIS_MODULE,
  79. .open = ump_file_open,
  80. .release = ump_file_release,
  81. #ifdef HAVE_UNLOCKED_IOCTL
  82. .unlocked_ioctl = ump_file_ioctl,
  83. #else
  84. .ioctl = ump_file_ioctl,
  85. #endif
  86. .mmap = ump_file_mmap
  87. };
  88. /* This function is called by Linux to initialize this module.
  89. * All we do is initialize the UMP device driver.
  90. */
  91. static int ump_initialize_module(void)
  92. {
  93. _mali_osk_errcode_t err;
  94. DBG_MSG(2, ("Inserting UMP device driver. Compiled: %s, time: %s\n", __DATE__, __TIME__));
  95. err = ump_kernel_constructor();
  96. if (_MALI_OSK_ERR_OK != err)
  97. {
  98. MSG_ERR(("UMP device driver init failed\n"));
  99. return map_errcode(err);
  100. }
  101. MSG(("UMP device driver %s loaded\n", SVN_REV_STRING));
  102. return 0;
  103. }
  104. /*
  105. * This function is called by Linux to unload/terminate/exit/cleanup this module.
  106. * All we do is terminate the UMP device driver.
  107. */
  108. static void ump_cleanup_module(void)
  109. {
  110. DBG_MSG(2, ("Unloading UMP device driver\n"));
  111. ump_kernel_destructor();
  112. DBG_MSG(2, ("Module unloaded\n"));
  113. }
  114. static ssize_t ump_memory_used_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
  115. {
  116. char buf[64];
  117. size_t r;
  118. u32 mem = _ump_ukk_report_memory_usage();
  119. r = snprintf(buf, 64, "%u\n", mem);
  120. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  121. }
  122. static const struct file_operations ump_memory_usage_fops = {
  123. .owner = THIS_MODULE,
  124. .read = ump_memory_used_read,
  125. };
  126. /*
  127. * Initialize the UMP device driver.
  128. */
  129. int ump_kernel_device_initialize(void)
  130. {
  131. int err;
  132. dev_t dev = 0;
  133. #if UMP_LICENSE_IS_GPL
  134. ump_debugfs_dir = debugfs_create_dir(ump_dev_name, NULL);
  135. if (ERR_PTR(-ENODEV) == ump_debugfs_dir)
  136. {
  137. ump_debugfs_dir = NULL;
  138. }
  139. else
  140. {
  141. debugfs_create_file("memory_usage", 0400, ump_debugfs_dir, NULL, &ump_memory_usage_fops);
  142. }
  143. #endif
  144. if (0 == ump_major)
  145. {
  146. /* auto select a major */
  147. err = alloc_chrdev_region(&dev, 0, 1, ump_dev_name);
  148. ump_major = MAJOR(dev);
  149. }
  150. else
  151. {
  152. /* use load time defined major number */
  153. dev = MKDEV(ump_major, 0);
  154. err = register_chrdev_region(dev, 1, ump_dev_name);
  155. }
  156. if (0 == err)
  157. {
  158. memset(&ump_device, 0, sizeof(ump_device));
  159. /* initialize our char dev data */
  160. cdev_init(&ump_device.cdev, &ump_fops);
  161. ump_device.cdev.owner = THIS_MODULE;
  162. ump_device.cdev.ops = &ump_fops;
  163. /* register char dev with the kernel */
  164. err = cdev_add(&ump_device.cdev, dev, 1/*count*/);
  165. if (0 == err)
  166. {
  167. #if UMP_LICENSE_IS_GPL
  168. ump_device.ump_class = class_create(THIS_MODULE, ump_dev_name);
  169. if (IS_ERR(ump_device.ump_class))
  170. {
  171. err = PTR_ERR(ump_device.ump_class);
  172. }
  173. else
  174. {
  175. struct device * mdev;
  176. mdev = device_create(ump_device.ump_class, NULL, dev, NULL, ump_dev_name);
  177. if (!IS_ERR(mdev))
  178. {
  179. return 0;
  180. }
  181. err = PTR_ERR(mdev);
  182. }
  183. cdev_del(&ump_device.cdev);
  184. #else
  185. return 0;
  186. #endif
  187. }
  188. unregister_chrdev_region(dev, 1);
  189. }
  190. return err;
  191. }
  192. /*
  193. * Terminate the UMP device driver
  194. */
  195. void ump_kernel_device_terminate(void)
  196. {
  197. dev_t dev = MKDEV(ump_major, 0);
  198. #if UMP_LICENSE_IS_GPL
  199. device_destroy(ump_device.ump_class, dev);
  200. class_destroy(ump_device.ump_class);
  201. #endif
  202. /* unregister char device */
  203. cdev_del(&ump_device.cdev);
  204. /* free major */
  205. unregister_chrdev_region(dev, 1);
  206. #if UMP_LICENSE_IS_GPL
  207. if(ump_debugfs_dir)
  208. debugfs_remove_recursive(ump_debugfs_dir);
  209. #endif
  210. }
  211. /*
  212. * Open a new session. User space has called open() on us.
  213. */
  214. static int ump_file_open(struct inode *inode, struct file *filp)
  215. {
  216. struct ump_session_data * session_data;
  217. _mali_osk_errcode_t err;
  218. /* input validation */
  219. if (0 != MINOR(inode->i_rdev))
  220. {
  221. MSG_ERR(("Minor not zero in ump_file_open()\n"));
  222. return -ENODEV;
  223. }
  224. /* Call the OS-Independent UMP Open function */
  225. err = _ump_ukk_open((void**) &session_data );
  226. if( _MALI_OSK_ERR_OK != err )
  227. {
  228. MSG_ERR(("Ump failed to open a new session\n"));
  229. return map_errcode( err );
  230. }
  231. filp->private_data = (void*)session_data;
  232. filp->f_pos = 0;
  233. return 0; /* success */
  234. }
  235. /*
  236. * Close a session. User space has called close() or crashed/terminated.
  237. */
  238. static int ump_file_release(struct inode *inode, struct file *filp)
  239. {
  240. _mali_osk_errcode_t err;
  241. err = _ump_ukk_close((void**) &filp->private_data );
  242. if( _MALI_OSK_ERR_OK != err )
  243. {
  244. return map_errcode( err );
  245. }
  246. return 0; /* success */
  247. }
  248. /*
  249. * Handle IOCTL requests.
  250. */
  251. #ifdef HAVE_UNLOCKED_IOCTL
  252. static long ump_file_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  253. #else
  254. static int ump_file_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
  255. #endif
  256. {
  257. int err = -ENOTTY;
  258. void __user * argument;
  259. struct ump_session_data * session_data;
  260. #ifndef HAVE_UNLOCKED_IOCTL
  261. (void)inode; /* inode not used */
  262. #endif
  263. session_data = (struct ump_session_data *)filp->private_data;
  264. if (NULL == session_data)
  265. {
  266. MSG_ERR(("No session data attached to file object\n"));
  267. return -ENOTTY;
  268. }
  269. /* interpret the argument as a user pointer to something */
  270. argument = (void __user *)arg;
  271. switch (cmd)
  272. {
  273. case UMP_IOC_QUERY_API_VERSION:
  274. err = ump_get_api_version_wrapper((u32 __user *)argument, session_data);
  275. break;
  276. case UMP_IOC_ALLOCATE :
  277. err = ump_allocate_wrapper((u32 __user *)argument, session_data);
  278. break;
  279. case UMP_IOC_RELEASE:
  280. err = ump_release_wrapper((u32 __user *)argument, session_data);
  281. break;
  282. case UMP_IOC_SIZE_GET:
  283. err = ump_size_get_wrapper((u32 __user *)argument, session_data);
  284. break;
  285. case UMP_IOC_MSYNC:
  286. err = ump_msync_wrapper((u32 __user *)argument, session_data);
  287. break;
  288. case UMP_IOC_CACHE_OPERATIONS_CONTROL:
  289. err = ump_cache_operations_control_wrapper((u32 __user *)argument, session_data);
  290. break;
  291. case UMP_IOC_SWITCH_HW_USAGE:
  292. err = ump_switch_hw_usage_wrapper((u32 __user *)argument, session_data);
  293. break;
  294. case UMP_IOC_LOCK:
  295. err = ump_lock_wrapper((u32 __user *)argument, session_data);
  296. break;
  297. case UMP_IOC_UNLOCK:
  298. err = ump_unlock_wrapper((u32 __user *)argument, session_data);
  299. break;
  300. default:
  301. DBG_MSG(1, ("No handler for IOCTL. cmd: 0x%08x, arg: 0x%08lx\n", cmd, arg));
  302. err = -EFAULT;
  303. break;
  304. }
  305. return err;
  306. }
  307. int map_errcode( _mali_osk_errcode_t err )
  308. {
  309. switch(err)
  310. {
  311. case _MALI_OSK_ERR_OK : return 0;
  312. case _MALI_OSK_ERR_FAULT: return -EFAULT;
  313. case _MALI_OSK_ERR_INVALID_FUNC: return -ENOTTY;
  314. case _MALI_OSK_ERR_INVALID_ARGS: return -EINVAL;
  315. case _MALI_OSK_ERR_NOMEM: return -ENOMEM;
  316. case _MALI_OSK_ERR_TIMEOUT: return -ETIMEDOUT;
  317. case _MALI_OSK_ERR_RESTARTSYSCALL: return -ERESTARTSYS;
  318. case _MALI_OSK_ERR_ITEM_NOT_FOUND: return -ENOENT;
  319. default: return -EFAULT;
  320. }
  321. }
  322. /*
  323. * Handle from OS to map specified virtual memory to specified UMP memory.
  324. */
  325. static int ump_file_mmap(struct file * filp, struct vm_area_struct * vma)
  326. {
  327. _ump_uk_map_mem_s args;
  328. _mali_osk_errcode_t err;
  329. struct ump_session_data * session_data;
  330. /* Validate the session data */
  331. session_data = (struct ump_session_data *)filp->private_data;
  332. if (NULL == session_data)
  333. {
  334. MSG_ERR(("mmap() called without any session data available\n"));
  335. return -EFAULT;
  336. }
  337. /* Re-pack the arguments that mmap() packed for us */
  338. args.ctx = session_data;
  339. args.phys_addr = 0;
  340. args.size = vma->vm_end - vma->vm_start;
  341. args._ukk_private = vma;
  342. args.secure_id = vma->vm_pgoff;
  343. args.is_cached = 0;
  344. if (!(vma->vm_flags & VM_SHARED))
  345. {
  346. args.is_cached = 1;
  347. vma->vm_flags = vma->vm_flags | VM_SHARED | VM_MAYSHARE ;
  348. DBG_MSG(3, ("UMP Map function: Forcing the CPU to use cache\n"));
  349. }
  350. /* By setting this flag, during a process fork; the child process will not have the parent UMP mappings */
  351. vma->vm_flags |= VM_DONTCOPY;
  352. DBG_MSG(4, ("UMP vma->flags: %x\n", vma->vm_flags ));
  353. /* Call the common mmap handler */
  354. err = _ump_ukk_map_mem( &args );
  355. if ( _MALI_OSK_ERR_OK != err)
  356. {
  357. MSG_ERR(("_ump_ukk_map_mem() failed in function ump_file_mmap()"));
  358. return map_errcode( err );
  359. }
  360. return 0; /* success */
  361. }
  362. /* Export UMP kernel space API functions */
  363. EXPORT_SYMBOL(ump_dd_secure_id_get);
  364. EXPORT_SYMBOL(ump_dd_handle_create_from_secure_id);
  365. EXPORT_SYMBOL(ump_dd_phys_block_count_get);
  366. EXPORT_SYMBOL(ump_dd_phys_block_get);
  367. EXPORT_SYMBOL(ump_dd_phys_blocks_get);
  368. EXPORT_SYMBOL(ump_dd_size_get);
  369. EXPORT_SYMBOL(ump_dd_reference_add);
  370. EXPORT_SYMBOL(ump_dd_reference_release);
  371. /* Export our own extended kernel space allocator */
  372. EXPORT_SYMBOL(ump_dd_handle_create_from_phys_blocks);
  373. /* Setup init and exit functions for this module */
  374. module_init(ump_initialize_module);
  375. module_exit(ump_cleanup_module);
  376. /* And some module informatio */
  377. MODULE_LICENSE(UMP_KERNEL_LINUX_LICENSE);
  378. MODULE_AUTHOR("ARM Ltd.");
  379. MODULE_VERSION(SVN_REV_STRING);