1 /*
2 * Copyright (C) 2010-2014, 2016-2017 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 <linux/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 */
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");
43
44 /* By default the module uses any available major, but it's possible to set it at load time to a specific number */
45 int ump_major = 0;
46 module_param(ump_major, int, S_IRUGO); /* r--r--r-- */
47 MODULE_PARM_DESC(ump_major, "Device major number");
48
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 */
51
52
53 #if UMP_LICENSE_IS_GPL
54 static 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 */
62 typedef struct ump_vma_usage_tracker {
63 int references;
64 ump_dd_handle handle;
65 } ump_vma_usage_tracker;
66
67 struct 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 */
75 static struct ump_device ump_device;
76 struct device *ump_global_mdev = NULL;
77
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);
83 #else
84 static int ump_file_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg);
85 #endif
86 static 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 */
90 static 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 */
ump_initialize_module(void)106 static 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 ump_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 */
ump_cleanup_module(void)128 static 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
ump_memory_used_read(struct file * filp,char __user * ubuf,size_t cnt,loff_t * ppos)137 static 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
147 static 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 */
ump_kernel_device_initialize(void)155 int 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 ump_global_mdev = device_create(ump_device.ump_class, NULL, dev, NULL, ump_dev_name);
196 if (!IS_ERR(ump_global_mdev)) {
197 return 0;
198 }
199
200 err = PTR_ERR(ump_global_mdev);
201 }
202 cdev_del(&ump_device.cdev);
203 #else
204 return 0;
205 #endif
206 }
207
208 unregister_chrdev_region(dev, 1);
209 }
210
211 return err;
212 }
213
214
215
216 /*
217 * Terminate the UMP device driver
218 */
ump_kernel_device_terminate(void)219 void ump_kernel_device_terminate(void)
220 {
221 dev_t dev = MKDEV(ump_major, 0);
222
223 #if UMP_LICENSE_IS_GPL
224 device_destroy(ump_device.ump_class, dev);
225 class_destroy(ump_device.ump_class);
226 #endif
227
228 /* unregister char device */
229 cdev_del(&ump_device.cdev);
230
231 /* free major */
232 unregister_chrdev_region(dev, 1);
233
234 #if UMP_LICENSE_IS_GPL
235 if (ump_debugfs_dir)
236 debugfs_remove_recursive(ump_debugfs_dir);
237 #endif
238 }
239
240 /*
241 * Open a new session. User space has called open() on us.
242 */
ump_file_open(struct inode * inode,struct file * filp)243 static int ump_file_open(struct inode *inode, struct file *filp)
244 {
245 struct ump_session_data *session_data;
246 _mali_osk_errcode_t err;
247
248 /* input validation */
249 if (0 != MINOR(inode->i_rdev)) {
250 MSG_ERR(("Minor not zero in ump_file_open()\n"));
251 return -ENODEV;
252 }
253
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);
259 }
260
261 filp->private_data = (void *)session_data;
262 filp->f_pos = 0;
263
264 return 0; /* success */
265 }
266
267
268
269 /*
270 * Close a session. User space has called close() or crashed/terminated.
271 */
ump_file_release(struct inode * inode,struct file * filp)272 static int ump_file_release(struct inode *inode, struct file *filp)
273 {
274 _mali_osk_errcode_t err;
275
276 err = _ump_ukk_close((void **) &filp->private_data);
277 if (_MALI_OSK_ERR_OK != err) {
278 return ump_map_errcode(err);
279 }
280
281 return 0; /* success */
282 }
283
284
285
286 /*
287 * Handle IOCTL requests.
288 */
289 #ifdef HAVE_UNLOCKED_IOCTL
ump_file_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)290 static long ump_file_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
291 #else
292 static int ump_file_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
293 #endif
294 {
295 int err = -ENOTTY;
296 void __user *argument;
297 struct ump_session_data *session_data;
298
299 #ifndef HAVE_UNLOCKED_IOCTL
300 (void)inode; /* inode not used */
301 #endif
302
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"));
306 return -ENOTTY;
307 }
308
309 /* interpret the argument as a user pointer to something */
310 argument = (void __user *)arg;
311
312 switch (cmd) {
313 case UMP_IOC_QUERY_API_VERSION:
314 err = ump_get_api_version_wrapper((u32 __user *)argument, session_data);
315 break;
316
317 case UMP_IOC_ALLOCATE :
318 err = ump_allocate_wrapper((u32 __user *)argument, session_data);
319 break;
320
321 case UMP_IOC_RELEASE:
322 err = ump_release_wrapper((u32 __user *)argument, session_data);
323 break;
324
325 case UMP_IOC_SIZE_GET:
326 err = ump_size_get_wrapper((u32 __user *)argument, session_data);
327 break;
328
329 case UMP_IOC_MSYNC:
330 err = ump_msync_wrapper((u32 __user *)argument, session_data);
331 break;
332
333 case UMP_IOC_CACHE_OPERATIONS_CONTROL:
334 err = ump_cache_operations_control_wrapper((u32 __user *)argument, session_data);
335 break;
336
337 case UMP_IOC_SWITCH_HW_USAGE:
338 err = ump_switch_hw_usage_wrapper((u32 __user *)argument, session_data);
339 break;
340
341 case UMP_IOC_LOCK:
342 err = ump_lock_wrapper((u32 __user *)argument, session_data);
343 break;
344
345 case UMP_IOC_UNLOCK:
346 err = ump_unlock_wrapper((u32 __user *)argument, session_data);
347 break;
348
349 case UMP_IOC_DMABUF_IMPORT:
350 #ifdef CONFIG_DMA_SHARED_BUFFER
351 err = ump_dmabuf_import_wrapper((u32 __user *)argument, session_data);
352 #else
353 err = -EFAULT;
354 DBG_MSG(1, ("User space use dmabuf API, but kernel don't support DMA BUF\n"));
355 #endif
356 break;
357
358 default:
359 DBG_MSG(1, ("No handler for IOCTL. cmd: 0x%08x, arg: 0x%08lx\n", cmd, arg));
360 err = -EFAULT;
361 break;
362 }
363
364 return err;
365 }
366
ump_map_errcode(_mali_osk_errcode_t err)367 int ump_map_errcode(_mali_osk_errcode_t err)
368 {
369 switch (err) {
370 case _MALI_OSK_ERR_OK :
371 return 0;
372 case _MALI_OSK_ERR_FAULT:
373 return -EFAULT;
374 case _MALI_OSK_ERR_INVALID_FUNC:
375 return -ENOTTY;
376 case _MALI_OSK_ERR_INVALID_ARGS:
377 return -EINVAL;
378 case _MALI_OSK_ERR_NOMEM:
379 return -ENOMEM;
380 case _MALI_OSK_ERR_TIMEOUT:
381 return -ETIMEDOUT;
382 case _MALI_OSK_ERR_RESTARTSYSCALL:
383 return -ERESTARTSYS;
384 case _MALI_OSK_ERR_ITEM_NOT_FOUND:
385 return -ENOENT;
386 default:
387 return -EFAULT;
388 }
389 }
390
391 /*
392 * Handle from OS to map specified virtual memory to specified UMP memory.
393 */
ump_file_mmap(struct file * filp,struct vm_area_struct * vma)394 static int ump_file_mmap(struct file *filp, struct vm_area_struct *vma)
395 {
396 _ump_uk_map_mem_s args;
397 _mali_osk_errcode_t err;
398 struct ump_session_data *session_data;
399
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"));
404 return -EFAULT;
405 }
406
407 /* Re-pack the arguments that mmap() packed for us */
408 args.ctx = session_data;
409 args.phys_addr = 0;
410 args.size = vma->vm_end - vma->vm_start;
411 args._ukk_private = vma;
412 args.secure_id = vma->vm_pgoff;
413
414 /* By setting this flag, during a process fork; the child process will not have the parent UMP mappings */
415 vma->vm_flags |= VM_DONTCOPY;
416
417 DBG_MSG(4, ("UMP vma->flags: %x\n", vma->vm_flags));
418
419 /* Call the common mmap handler */
420 err = _ump_ukk_map_mem(&args);
421 if (_MALI_OSK_ERR_OK != err) {
422 MSG_ERR(("_ump_ukk_map_mem() failed in function ump_file_mmap()"));
423 return ump_map_errcode(err);
424 }
425
426 return 0; /* success */
427 }
428
429 /* Export UMP kernel space API functions */
430 EXPORT_SYMBOL(ump_dd_secure_id_get);
431 EXPORT_SYMBOL(ump_dd_handle_create_from_secure_id);
432 EXPORT_SYMBOL(ump_dd_phys_block_count_get);
433 EXPORT_SYMBOL(ump_dd_phys_block_get);
434 EXPORT_SYMBOL(ump_dd_phys_blocks_get);
435 EXPORT_SYMBOL(ump_dd_size_get);
436 EXPORT_SYMBOL(ump_dd_reference_add);
437 EXPORT_SYMBOL(ump_dd_reference_release);
438
439 /* Export our own extended kernel space allocator */
440 EXPORT_SYMBOL(ump_dd_handle_create_from_phys_blocks);
441
442 /* Setup init and exit functions for this module */
443 module_init(ump_initialize_module);
444 module_exit(ump_cleanup_module);
445
446 /* And some module informatio */
447 MODULE_LICENSE(UMP_KERNEL_LINUX_LICENSE);
448 MODULE_AUTHOR("ARM Ltd.");
449 MODULE_VERSION(SVN_REV_STRING);
450