Merge tag 'lsk-v4.4-16.07-android'
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / arm / mali400 / mali / linux / mali_kernel_linux.c
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
12 /**
13  * @file mali_kernel_linux.c
14  * Implementation of the Linux device driver entrypoints
15  */
16 #include "../platform/rk30/custom_log.h"
17 #include "../platform/rk30/rk_ext.h"
18
19 #include <linux/module.h>   /* kernel module definitions */
20 #include <linux/fs.h>       /* file system operations */
21 #include <linux/cdev.h>     /* character device definitions */
22 #include <linux/mm.h>       /* memory manager definitions */
23 #include <linux/mali/mali_utgard_ioctl.h>
24 #include <linux/version.h>
25 #include <linux/device.h>
26 #include "mali_kernel_license.h"
27 #include <linux/platform_device.h>
28 #include <linux/miscdevice.h>
29 #include <linux/bug.h>
30 #include <linux/of.h>
31
32 #include <linux/mali/mali_utgard.h>
33 #include "mali_kernel_common.h"
34 #include "mali_session.h"
35 #include "mali_kernel_core.h"
36 #include "mali_osk.h"
37 #include "mali_kernel_linux.h"
38 #include "mali_ukk.h"
39 #include "mali_ukk_wrappers.h"
40 #include "mali_kernel_sysfs.h"
41 #include "mali_pm.h"
42 #include "mali_kernel_license.h"
43 #include "mali_memory.h"
44 #include "mali_memory_dma_buf.h"
45 #include "mali_memory_manager.h"
46 #include "mali_memory_swap_alloc.h"
47 #if defined(CONFIG_MALI400_INTERNAL_PROFILING)
48 #include "mali_profiling_internal.h"
49 #endif
50 #if defined(CONFIG_MALI400_PROFILING) && defined(CONFIG_MALI_DVFS)
51 #include "mali_osk_profiling.h"
52 #include "mali_dvfs_policy.h"
53
54 static int is_first_resume = 1;
55 /*Store the clk and vol for boot/insmod and mali_resume*/
56 static struct mali_gpu_clk_item mali_gpu_clk[2];
57 #endif
58
59 /* Streamline support for the Mali driver */
60 #if defined(CONFIG_TRACEPOINTS) && defined(CONFIG_MALI400_PROFILING)
61 /* Ask Linux to create the tracepoints */
62 #define CREATE_TRACE_POINTS
63 #include "mali_linux_trace.h"
64
65 EXPORT_TRACEPOINT_SYMBOL_GPL(mali_timeline_event);
66 EXPORT_TRACEPOINT_SYMBOL_GPL(mali_hw_counter);
67 EXPORT_TRACEPOINT_SYMBOL_GPL(mali_sw_counters);
68 #endif /* CONFIG_TRACEPOINTS */
69
70 /* from the __malidrv_build_info.c file that is generated during build */
71 extern const char *__malidrv_build_info(void);
72
73 /* Module parameter to control log level */
74 int mali_debug_level = 2;
75 module_param(mali_debug_level, int, S_IRUSR | S_IWUSR | S_IWGRP | S_IRGRP | S_IROTH); /* rw-rw-r-- */
76 MODULE_PARM_DESC(mali_debug_level, "Higher number, more dmesg output");
77
78 extern int mali_max_job_runtime;
79 module_param(mali_max_job_runtime, int, S_IRUSR | S_IWUSR | S_IWGRP | S_IRGRP | S_IROTH);
80 MODULE_PARM_DESC(mali_max_job_runtime, "Maximum allowed job runtime in msecs.\nJobs will be killed after this no matter what");
81
82 extern int mali_l2_max_reads;
83 module_param(mali_l2_max_reads, int, S_IRUSR | S_IRGRP | S_IROTH);
84 MODULE_PARM_DESC(mali_l2_max_reads, "Maximum reads for Mali L2 cache");
85
86 extern unsigned int mali_dedicated_mem_start;
87 module_param(mali_dedicated_mem_start, uint, S_IRUSR | S_IRGRP | S_IROTH);
88 MODULE_PARM_DESC(mali_dedicated_mem_start, "Physical start address of dedicated Mali GPU memory.");
89
90 extern unsigned int mali_dedicated_mem_size;
91 module_param(mali_dedicated_mem_size, uint, S_IRUSR | S_IRGRP | S_IROTH);
92 MODULE_PARM_DESC(mali_dedicated_mem_size, "Size of dedicated Mali GPU memory.");
93
94 extern unsigned int mali_shared_mem_size;
95 module_param(mali_shared_mem_size, uint, S_IRUSR | S_IRGRP | S_IROTH);
96 MODULE_PARM_DESC(mali_shared_mem_size, "Size of shared Mali GPU memory.");
97
98 #if defined(CONFIG_MALI400_PROFILING)
99 extern int mali_boot_profiling;
100 module_param(mali_boot_profiling, int, S_IRUSR | S_IRGRP | S_IROTH);
101 MODULE_PARM_DESC(mali_boot_profiling, "Start profiling as a part of Mali driver initialization");
102 #endif
103
104 extern int mali_max_pp_cores_group_1;
105 module_param(mali_max_pp_cores_group_1, int, S_IRUSR | S_IRGRP | S_IROTH);
106 MODULE_PARM_DESC(mali_max_pp_cores_group_1, "Limit the number of PP cores to use from first PP group.");
107
108 extern int mali_max_pp_cores_group_2;
109 module_param(mali_max_pp_cores_group_2, int, S_IRUSR | S_IRGRP | S_IROTH);
110 MODULE_PARM_DESC(mali_max_pp_cores_group_2, "Limit the number of PP cores to use from second PP group (Mali-450 only).");
111
112 extern unsigned int mali_mem_swap_out_threshold_value;
113 module_param(mali_mem_swap_out_threshold_value, uint, S_IRUSR | S_IRGRP | S_IROTH);
114 MODULE_PARM_DESC(mali_mem_swap_out_threshold_value, "Threshold value used to limit how much swappable memory cached in Mali driver.");
115
116 #if defined(CONFIG_MALI_DVFS)
117 /** the max fps the same as display vsync default 60, can set by module insert parameter */
118 extern int mali_max_system_fps;
119 module_param(mali_max_system_fps, int, S_IRUSR | S_IWUSR | S_IWGRP | S_IRGRP | S_IROTH);
120 MODULE_PARM_DESC(mali_max_system_fps, "Max system fps the same as display VSYNC.");
121
122 /** a lower limit on their desired FPS default 58, can set by module insert parameter*/
123 extern int mali_desired_fps;
124 module_param(mali_desired_fps, int, S_IRUSR | S_IWUSR | S_IWGRP | S_IRGRP | S_IROTH);
125 MODULE_PARM_DESC(mali_desired_fps, "A bit lower than max_system_fps which user desired fps");
126 #endif
127
128 #if MALI_ENABLE_CPU_CYCLES
129 #include <linux/cpumask.h>
130 #include <linux/timer.h>
131 #include <asm/smp.h>
132 static struct timer_list mali_init_cpu_clock_timers[8];
133 static u32 mali_cpu_clock_last_value[8] = {0,};
134 #endif
135
136 /* Export symbols from common code: mali_user_settings.c */
137 #include "mali_user_settings_db.h"
138 EXPORT_SYMBOL(mali_set_user_setting);
139 EXPORT_SYMBOL(mali_get_user_setting);
140
141 static char mali_dev_name[] = "mali"; /* should be const, but the functions we call requires non-cost */
142
143 /* This driver only supports one Mali device, and this variable stores this single platform device */
144 struct platform_device *mali_platform_device = NULL;
145
146 /* This driver only supports one Mali device, and this variable stores the exposed misc device (/dev/mali) */
147 static struct miscdevice mali_miscdevice = { 0, };
148
149 static int mali_miscdevice_register(struct platform_device *pdev);
150 static void mali_miscdevice_unregister(void);
151
152 static int mali_open(struct inode *inode, struct file *filp);
153 static int mali_release(struct inode *inode, struct file *filp);
154 #ifdef HAVE_UNLOCKED_IOCTL
155 static long mali_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
156 #else
157 static int mali_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg);
158 #endif
159
160 static int mali_probe(struct platform_device *pdev);
161 static int mali_remove(struct platform_device *pdev);
162
163 static int mali_driver_suspend_scheduler(struct device *dev);
164 static int mali_driver_resume_scheduler(struct device *dev);
165
166 #ifdef CONFIG_PM_RUNTIME
167 static int mali_driver_runtime_suspend(struct device *dev);
168 static int mali_driver_runtime_resume(struct device *dev);
169 static int mali_driver_runtime_idle(struct device *dev);
170 #endif
171
172 #if defined(MALI_FAKE_PLATFORM_DEVICE)
173 #if defined(CONFIG_MALI_DT)
174 extern int mali_platform_device_init(struct platform_device *device);
175 extern int mali_platform_device_deinit(struct platform_device *device);
176 #else
177 extern int mali_platform_device_register(void);
178 extern int mali_platform_device_unregister(void);
179 #endif
180 #endif
181
182 /* Linux power management operations provided by the Mali device driver */
183 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29))
184 struct pm_ext_ops mali_dev_ext_pm_ops = {
185         .base =
186         {
187                 .suspend = mali_driver_suspend_scheduler,
188                 .resume = mali_driver_resume_scheduler,
189                 .freeze = mali_driver_suspend_scheduler,
190                 .thaw =   mali_driver_resume_scheduler,
191         },
192 };
193 #else
194 static const struct dev_pm_ops mali_dev_pm_ops = {
195 #ifdef CONFIG_PM_RUNTIME
196         .runtime_suspend = mali_driver_runtime_suspend,
197         .runtime_resume = mali_driver_runtime_resume,
198         .runtime_idle = mali_driver_runtime_idle,
199 #endif
200         .suspend = mali_driver_suspend_scheduler,
201         .resume = mali_driver_resume_scheduler,
202         .freeze = mali_driver_suspend_scheduler,
203         .thaw = mali_driver_resume_scheduler,
204         .poweroff = mali_driver_suspend_scheduler,
205 };
206 #endif
207
208 #ifdef CONFIG_MALI_DT
209 static struct of_device_id base_dt_ids[] = {
210         {.compatible = "arm,mali-300"},
211     /*-------------------------------------------------------*/
212     /* rk_ext : to use dts_for_mali_ko_befor_r5p0-01rel0. */
213         // {.compatible = "arm,mali-400"},
214         {.compatible = "arm,mali400"},
215     /*-------------------------------------------------------*/
216         {.compatible = "arm,mali-450"},
217         {.compatible = "arm,mali-470"},
218         {},
219 };
220
221 MODULE_DEVICE_TABLE(of, base_dt_ids);
222 #endif
223
224 /* The Mali device driver struct */
225 static struct platform_driver mali_platform_driver = {
226         .probe  = mali_probe,
227         .remove = mali_remove,
228 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29))
229         .pm = &mali_dev_ext_pm_ops,
230 #endif
231         .driver =
232         {
233                 .name   = MALI_GPU_NAME_UTGARD,
234                 .owner  = THIS_MODULE,
235                 .bus = &platform_bus_type,
236 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29))
237                 .pm = &mali_dev_pm_ops,
238 #endif
239 #ifdef CONFIG_MALI_DT
240                 .of_match_table = of_match_ptr(base_dt_ids),
241 #endif
242         },
243 };
244
245 /* Linux misc device operations (/dev/mali) */
246 struct file_operations mali_fops = {
247         .owner = THIS_MODULE,
248         .open = mali_open,
249         .release = mali_release,
250 #ifdef HAVE_UNLOCKED_IOCTL
251         .unlocked_ioctl = mali_ioctl,
252 #else
253         .ioctl = mali_ioctl,
254 #endif
255         .compat_ioctl = mali_ioctl,
256         .mmap = mali_mmap
257 };
258
259 #if MALI_ENABLE_CPU_CYCLES
260 void mali_init_cpu_time_counters(int reset, int enable_divide_by_64)
261 {
262         /* The CPU assembly reference used is: ARM Architecture Reference Manual ARMv7-AR C.b */
263         u32 write_value;
264
265         /* See B4.1.116 PMCNTENSET, Performance Monitors Count Enable Set register, VMSA */
266         /* setting p15 c9 c12 1 to 0x8000000f==CPU_CYCLE_ENABLE |EVENT_3_ENABLE|EVENT_2_ENABLE|EVENT_1_ENABLE|EVENT_0_ENABLE */
267         asm volatile("mcr p15, 0, %0, c9, c12, 1" :: "r"(0x8000000f));
268
269
270         /* See B4.1.117 PMCR, Performance Monitors Control Register. Writing to p15, c9, c12, 0 */
271         write_value = 1 << 0; /* Bit 0 set. Enable counters */
272         if (reset) {
273                 write_value |= 1 << 1; /* Reset event counters */
274                 write_value |= 1 << 2; /* Reset cycle counter  */
275         }
276         if (enable_divide_by_64) {
277                 write_value |= 1 << 3; /* Enable the Clock divider by 64 */
278         }
279         write_value |= 1 << 4; /* Export enable. Not needed */
280         asm volatile("MCR p15, 0, %0, c9, c12, 0\t\n" :: "r"(write_value));
281
282         /* PMOVSR Overflow Flag Status Register - Clear Clock and Event overflows */
283         asm volatile("MCR p15, 0, %0, c9, c12, 3\t\n" :: "r"(0x8000000f));
284
285
286         /* See B4.1.124 PMUSERENR - setting p15 c9 c14 to 1" */
287         /* User mode access to the Performance Monitors enabled. */
288         /* Lets User space read cpu clock cycles */
289         asm volatile("mcr p15, 0, %0, c9, c14, 0" :: "r"(1));
290 }
291
292 /** A timer function that configures the cycle clock counter on current CPU.
293  * The function \a mali_init_cpu_time_counters_on_all_cpus sets up this
294  * function to trigger on all Cpus during module load.
295  */
296 static void mali_init_cpu_clock_timer_func(unsigned long data)
297 {
298         int reset_counters, enable_divide_clock_counter_by_64;
299         int current_cpu = raw_smp_processor_id();
300         unsigned int sample0;
301         unsigned int sample1;
302
303         MALI_IGNORE(data);
304
305         reset_counters = 1;
306         enable_divide_clock_counter_by_64 = 0;
307         mali_init_cpu_time_counters(reset_counters, enable_divide_clock_counter_by_64);
308
309         sample0 = mali_get_cpu_cyclecount();
310         sample1 = mali_get_cpu_cyclecount();
311
312         MALI_DEBUG_PRINT(3, ("Init Cpu %d cycle counter- First two samples: %08x %08x \n", current_cpu, sample0, sample1));
313 }
314
315 /** A timer functions for storing current time on all cpus.
316  * Used for checking if the clocks have similar values or if they are drifting.
317  */
318 static void mali_print_cpu_clock_timer_func(unsigned long data)
319 {
320         int current_cpu = raw_smp_processor_id();
321         unsigned int sample0;
322
323         MALI_IGNORE(data);
324         sample0 = mali_get_cpu_cyclecount();
325         if (current_cpu < 8) {
326                 mali_cpu_clock_last_value[current_cpu] = sample0;
327         }
328 }
329
330 /** Init the performance registers on all CPUs to count clock cycles.
331  * For init \a print_only should be 0.
332  * If \a print_only is 1, it will intead print the current clock value of all CPUs.
333  */
334 void mali_init_cpu_time_counters_on_all_cpus(int print_only)
335 {
336         int i = 0;
337         int cpu_number;
338         int jiffies_trigger;
339         int jiffies_wait;
340
341         jiffies_wait = 2;
342         jiffies_trigger = jiffies + jiffies_wait;
343
344         for (i = 0 ; i < 8 ; i++) {
345                 init_timer(&mali_init_cpu_clock_timers[i]);
346                 if (print_only) mali_init_cpu_clock_timers[i].function = mali_print_cpu_clock_timer_func;
347                 else            mali_init_cpu_clock_timers[i].function = mali_init_cpu_clock_timer_func;
348                 mali_init_cpu_clock_timers[i].expires = jiffies_trigger ;
349         }
350         cpu_number = cpumask_first(cpu_online_mask);
351         for (i = 0 ; i < 8 ; i++) {
352                 int next_cpu;
353                 add_timer_on(&mali_init_cpu_clock_timers[i], cpu_number);
354                 next_cpu = cpumask_next(cpu_number, cpu_online_mask);
355                 if (next_cpu >= nr_cpu_ids) break;
356                 cpu_number = next_cpu;
357         }
358
359         while (jiffies_wait) jiffies_wait = schedule_timeout_uninterruptible(jiffies_wait);
360
361         for (i = 0 ; i < 8 ; i++) {
362                 del_timer_sync(&mali_init_cpu_clock_timers[i]);
363         }
364
365         if (print_only) {
366                 if ((0 == mali_cpu_clock_last_value[2]) && (0 == mali_cpu_clock_last_value[3])) {
367                         /* Diff can be printed if we want to check if the clocks are in sync
368                         int diff = mali_cpu_clock_last_value[0] - mali_cpu_clock_last_value[1];*/
369                         MALI_DEBUG_PRINT(2, ("CPU cycle counters readout all: %08x %08x\n", mali_cpu_clock_last_value[0], mali_cpu_clock_last_value[1]));
370                 } else {
371                         MALI_DEBUG_PRINT(2, ("CPU cycle counters readout all: %08x %08x %08x %08x\n", mali_cpu_clock_last_value[0], mali_cpu_clock_last_value[1], mali_cpu_clock_last_value[2], mali_cpu_clock_last_value[3]));
372                 }
373         }
374 }
375 #endif
376
377 int mali_module_init(void)
378 {
379         int err = 0;
380
381         MALI_DEBUG_PRINT(2, ("Inserting Mali v%d device driver. \n", _MALI_API_VERSION));
382         MALI_DEBUG_PRINT(2, ("Compiled: %s, time: %s.\n", __DATE__, __TIME__));
383         MALI_DEBUG_PRINT(2, ("Driver revision: %s\n", SVN_REV_STRING));
384     
385         I("svn_rev_string_from_arm of this mali_ko is '%s', rk_ko_ver is '%d', built at '%s', on '%s'.",
386                 SVN_REV_STRING,
387                 RK_KO_VER,
388                 __TIME__,
389                 __DATE__);
390
391 #if MALI_ENABLE_CPU_CYCLES
392         mali_init_cpu_time_counters_on_all_cpus(0);
393         MALI_DEBUG_PRINT(2, ("CPU cycle counter setup complete\n"));
394         /* Printing the current cpu counters */
395         mali_init_cpu_time_counters_on_all_cpus(1);
396 #endif
397
398         /* Initialize module wide settings */
399 #ifdef MALI_FAKE_PLATFORM_DEVICE
400 #ifndef CONFIG_MALI_DT
401         MALI_DEBUG_PRINT(2, ("mali_module_init() registering device\n"));
402         err = mali_platform_device_register();
403         if (0 != err) {
404                 return err;
405         }
406 #endif
407 #endif
408
409         MALI_DEBUG_PRINT(2, ("mali_module_init() registering driver\n"));
410
411         err = platform_driver_register(&mali_platform_driver);
412
413         if (0 != err) {
414                 MALI_DEBUG_PRINT(2, ("mali_module_init() Failed to register driver (%d)\n", err));
415 #ifdef MALI_FAKE_PLATFORM_DEVICE
416 #ifndef CONFIG_MALI_DT
417                 mali_platform_device_unregister();
418 #endif
419 #endif
420                 mali_platform_device = NULL;
421                 return err;
422         }
423
424 #if defined(CONFIG_MALI400_INTERNAL_PROFILING)
425         err = _mali_internal_profiling_init(mali_boot_profiling ? MALI_TRUE : MALI_FALSE);
426         if (0 != err) {
427                 /* No biggie if we wheren't able to initialize the profiling */
428                 MALI_PRINT_ERROR(("Failed to initialize profiling, feature will be unavailable\n"));
429         }
430 #endif
431
432         /* Tracing the current frequency and voltage from boot/insmod*/
433 #if defined(CONFIG_MALI400_PROFILING) && defined(CONFIG_MALI_DVFS)
434         /* Just call mali_get_current_gpu_clk_item(),to record current clk info.*/
435         mali_get_current_gpu_clk_item(&mali_gpu_clk[0]);
436         _mali_osk_profiling_add_event(MALI_PROFILING_EVENT_TYPE_SINGLE |
437                                       MALI_PROFILING_EVENT_CHANNEL_GPU |
438                                       MALI_PROFILING_EVENT_REASON_SINGLE_GPU_FREQ_VOLT_CHANGE,
439                                       mali_gpu_clk[0].clock,
440                                       mali_gpu_clk[0].vol / 1000,
441                                       0, 0, 0);
442 #endif
443
444         MALI_PRINT(("Mali device driver loaded\n"));
445
446         return 0; /* Success */
447 }
448
449 void mali_module_exit(void)
450 {
451         MALI_DEBUG_PRINT(2, ("Unloading Mali v%d device driver.\n", _MALI_API_VERSION));
452
453         MALI_DEBUG_PRINT(2, ("mali_module_exit() unregistering driver\n"));
454
455         platform_driver_unregister(&mali_platform_driver);
456
457 #if defined(MALI_FAKE_PLATFORM_DEVICE)
458 #ifndef CONFIG_MALI_DT
459         MALI_DEBUG_PRINT(2, ("mali_module_exit() unregistering device\n"));
460         mali_platform_device_unregister();
461 #endif
462 #endif
463
464         /* Tracing the current frequency and voltage from rmmod*/
465         _mali_osk_profiling_add_event(MALI_PROFILING_EVENT_TYPE_SINGLE |
466                                       MALI_PROFILING_EVENT_CHANNEL_GPU |
467                                       MALI_PROFILING_EVENT_REASON_SINGLE_GPU_FREQ_VOLT_CHANGE,
468                                       0,
469                                       0,
470                                       0, 0, 0);
471
472 #if defined(CONFIG_MALI400_INTERNAL_PROFILING)
473         _mali_internal_profiling_term();
474 #endif
475
476         MALI_PRINT(("Mali device driver unloaded\n"));
477 }
478
479 static int mali_probe(struct platform_device *pdev)
480 {
481         int err;
482
483         MALI_DEBUG_PRINT(2, ("mali_probe(): Called for platform device %s\n", pdev->name));
484
485         if (NULL != mali_platform_device) {
486                 /* Already connected to a device, return error */
487                 MALI_PRINT_ERROR(("mali_probe(): The Mali driver is already connected with a Mali device."));
488                 return -EEXIST;
489         }
490
491         mali_platform_device = pdev;
492
493         dev_info(&pdev->dev, "mali_platform_device->num_resources = %d\n",
494                 mali_platform_device->num_resources);
495         
496         {
497                 int i = 0;
498
499                 for(i = 0; i < mali_platform_device->num_resources; i++)
500                         dev_info(&pdev->dev, "mali_platform_device->resource[%d].start = 0x%08x\n",
501                                 i, mali_platform_device->resource[i].start);
502         }
503
504 #ifdef CONFIG_MALI_DT
505         /* If we use DT to initialize our DDK, we have to prepare somethings. */
506         err = mali_platform_device_init(mali_platform_device);
507         if (0 != err) {
508                 MALI_PRINT_ERROR(("mali_probe(): Failed to initialize platform device."));
509                 return -EFAULT;
510         }
511 #endif
512
513         if (_MALI_OSK_ERR_OK == _mali_osk_wq_init()) {
514                 /* Initialize the Mali GPU HW specified by pdev */
515                 if (_MALI_OSK_ERR_OK == mali_initialize_subsystems()) {
516                         /* Register a misc device (so we are accessible from user space) */
517                         err = mali_miscdevice_register(pdev);
518                         if (0 == err) {
519                                 /* Setup sysfs entries */
520                                 err = mali_sysfs_register(mali_dev_name);
521
522                                 if (0 == err) {
523                                         MALI_DEBUG_PRINT(2, ("mali_probe(): Successfully initialized driver for platform device %s\n", pdev->name));
524
525                                         return 0;
526                                 } else {
527                                         MALI_PRINT_ERROR(("mali_probe(): failed to register sysfs entries"));
528                                 }
529                                 mali_miscdevice_unregister();
530                         } else {
531                                 MALI_PRINT_ERROR(("mali_probe(): failed to register Mali misc device."));
532                         }
533                         mali_terminate_subsystems();
534                 } else {
535                         MALI_PRINT_ERROR(("mali_probe(): Failed to initialize Mali device driver."));
536                 }
537                 _mali_osk_wq_term();
538         }
539
540         mali_platform_device = NULL;
541         return -EFAULT;
542 }
543
544 static int mali_remove(struct platform_device *pdev)
545 {
546         MALI_DEBUG_PRINT(2, ("mali_remove() called for platform device %s\n", pdev->name));
547         mali_sysfs_unregister();
548         mali_miscdevice_unregister();
549         mali_terminate_subsystems();
550         _mali_osk_wq_term();
551 #ifdef CONFIG_MALI_DT
552         mali_platform_device_deinit(mali_platform_device);
553 #endif
554         mali_platform_device = NULL;
555         return 0;
556 }
557
558 static int mali_miscdevice_register(struct platform_device *pdev)
559 {
560         int err;
561
562         mali_miscdevice.minor = MISC_DYNAMIC_MINOR;
563         mali_miscdevice.name = mali_dev_name;
564         mali_miscdevice.fops = &mali_fops;
565         mali_miscdevice.parent = get_device(&pdev->dev);
566
567         err = misc_register(&mali_miscdevice);
568         if (0 != err) {
569                 MALI_PRINT_ERROR(("Failed to register misc device, misc_register() returned %d\n", err));
570         }
571
572         return err;
573 }
574
575 static void mali_miscdevice_unregister(void)
576 {
577         misc_deregister(&mali_miscdevice);
578 }
579
580 static int mali_driver_suspend_scheduler(struct device *dev)
581 {
582         mali_pm_os_suspend(MALI_TRUE);
583         /* Tracing the frequency and voltage after mali is suspended */
584         _mali_osk_profiling_add_event(MALI_PROFILING_EVENT_TYPE_SINGLE |
585                                       MALI_PROFILING_EVENT_CHANNEL_GPU |
586                                       MALI_PROFILING_EVENT_REASON_SINGLE_GPU_FREQ_VOLT_CHANGE,
587                                       0,
588                                       0,
589                                       0, 0, 0);
590         return 0;
591 }
592
593 static int mali_driver_resume_scheduler(struct device *dev)
594 {
595         /* Tracing the frequency and voltage after mali is resumed */
596 #if defined(CONFIG_MALI400_PROFILING) && defined(CONFIG_MALI_DVFS)
597         /* Just call mali_get_current_gpu_clk_item() once,to record current clk info.*/
598         if (is_first_resume == 1) {
599                 mali_get_current_gpu_clk_item(&mali_gpu_clk[1]);
600                 is_first_resume = 0;
601         }
602         _mali_osk_profiling_add_event(MALI_PROFILING_EVENT_TYPE_SINGLE |
603                                       MALI_PROFILING_EVENT_CHANNEL_GPU |
604                                       MALI_PROFILING_EVENT_REASON_SINGLE_GPU_FREQ_VOLT_CHANGE,
605                                       mali_gpu_clk[1].clock,
606                                       mali_gpu_clk[1].vol / 1000,
607                                       0, 0, 0);
608 #endif
609         mali_pm_os_resume();
610         return 0;
611 }
612
613 #ifdef CONFIG_PM_RUNTIME
614 static int mali_driver_runtime_suspend(struct device *dev)
615 {
616         if (MALI_TRUE == mali_pm_runtime_suspend()) {
617                 /* Tracing the frequency and voltage after mali is suspended */
618                 _mali_osk_profiling_add_event(MALI_PROFILING_EVENT_TYPE_SINGLE |
619                                               MALI_PROFILING_EVENT_CHANNEL_GPU |
620                                               MALI_PROFILING_EVENT_REASON_SINGLE_GPU_FREQ_VOLT_CHANGE,
621                                               0,
622                                               0,
623                                               0, 0, 0);
624
625                 return 0;
626         } else {
627                 return -EBUSY;
628         }
629 }
630
631 static int mali_driver_runtime_resume(struct device *dev)
632 {
633         /* Tracing the frequency and voltage after mali is resumed */
634 #if defined(CONFIG_MALI400_PROFILING) && defined(CONFIG_MALI_DVFS)
635         /* Just call mali_get_current_gpu_clk_item() once,to record current clk info.*/
636         if (is_first_resume == 1) {
637                 mali_get_current_gpu_clk_item(&mali_gpu_clk[1]);
638                 is_first_resume = 0;
639         }
640         _mali_osk_profiling_add_event(MALI_PROFILING_EVENT_TYPE_SINGLE |
641                                       MALI_PROFILING_EVENT_CHANNEL_GPU |
642                                       MALI_PROFILING_EVENT_REASON_SINGLE_GPU_FREQ_VOLT_CHANGE,
643                                       mali_gpu_clk[1].clock,
644                                       mali_gpu_clk[1].vol / 1000,
645                                       0, 0, 0);
646 #endif
647
648         mali_pm_runtime_resume();
649         return 0;
650 }
651
652 static int mali_driver_runtime_idle(struct device *dev)
653 {
654         /* Nothing to do */
655         return 0;
656 }
657 #endif
658
659 static int mali_open(struct inode *inode, struct file *filp)
660 {
661         struct mali_session_data *session_data;
662         _mali_osk_errcode_t err;
663
664         /* input validation */
665         if (mali_miscdevice.minor != iminor(inode)) {
666                 MALI_PRINT_ERROR(("mali_open() Minor does not match\n"));
667                 return -ENODEV;
668         }
669
670         /* allocated struct to track this session */
671         err = _mali_ukk_open((void **)&session_data);
672         if (_MALI_OSK_ERR_OK != err) return map_errcode(err);
673
674         /* initialize file pointer */
675         filp->f_pos = 0;
676
677         /* link in our session data */
678         filp->private_data = (void *)session_data;
679
680         filp->f_mapping = mali_mem_swap_get_global_swap_file()->f_mapping;
681
682         return 0;
683 }
684
685 static int mali_release(struct inode *inode, struct file *filp)
686 {
687         _mali_osk_errcode_t err;
688
689         /* input validation */
690         if (mali_miscdevice.minor != iminor(inode)) {
691                 MALI_PRINT_ERROR(("mali_release() Minor does not match\n"));
692                 return -ENODEV;
693         }
694
695         err = _mali_ukk_close((void **)&filp->private_data);
696         if (_MALI_OSK_ERR_OK != err) return map_errcode(err);
697
698         return 0;
699 }
700
701 int map_errcode(_mali_osk_errcode_t err)
702 {
703         switch (err) {
704         case _MALI_OSK_ERR_OK :
705                 return 0;
706         case _MALI_OSK_ERR_FAULT:
707                 return -EFAULT;
708         case _MALI_OSK_ERR_INVALID_FUNC:
709                 return -ENOTTY;
710         case _MALI_OSK_ERR_INVALID_ARGS:
711                 return -EINVAL;
712         case _MALI_OSK_ERR_NOMEM:
713                 return -ENOMEM;
714         case _MALI_OSK_ERR_TIMEOUT:
715                 return -ETIMEDOUT;
716         case _MALI_OSK_ERR_RESTARTSYSCALL:
717                 return -ERESTARTSYS;
718         case _MALI_OSK_ERR_ITEM_NOT_FOUND:
719                 return -ENOENT;
720         default:
721                 return -EFAULT;
722         }
723 }
724
725 #ifdef HAVE_UNLOCKED_IOCTL
726 static long mali_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
727 #else
728 static int mali_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
729 #endif
730 {
731         int err;
732         struct mali_session_data *session_data;
733
734 #ifndef HAVE_UNLOCKED_IOCTL
735         /* inode not used */
736         (void)inode;
737 #endif
738
739         MALI_DEBUG_PRINT(7, ("Ioctl received 0x%08X 0x%08lX\n", cmd, arg));
740
741         session_data = (struct mali_session_data *)filp->private_data;
742         if (NULL == session_data) {
743                 MALI_DEBUG_PRINT(7, ("filp->private_data was NULL\n"));
744                 return -ENOTTY;
745         }
746
747         if (NULL == (void *)arg) {
748                 MALI_DEBUG_PRINT(7, ("arg was NULL\n"));
749                 return -ENOTTY;
750         }
751
752         switch (cmd) {
753         case MALI_IOC_WAIT_FOR_NOTIFICATION:
754                 BUILD_BUG_ON(!IS_ALIGNED(sizeof(_mali_uk_wait_for_notification_s), sizeof(u64)));
755                 err = wait_for_notification_wrapper(session_data, (_mali_uk_wait_for_notification_s __user *)arg);
756                 break;
757
758         case MALI_IOC_GET_API_VERSION_V2:
759                 BUILD_BUG_ON(!IS_ALIGNED(sizeof(_mali_uk_get_api_version_v2_s), sizeof(u64)));
760                 err = get_api_version_v2_wrapper(session_data, (_mali_uk_get_api_version_v2_s __user *)arg);
761                 break;
762
763         case MALI_IOC_GET_API_VERSION:
764                 err = get_api_version_wrapper(session_data, (_mali_uk_get_api_version_s __user *)arg);
765                 break;
766
767         case MALI_IOC_POST_NOTIFICATION:
768                 BUILD_BUG_ON(!IS_ALIGNED(sizeof(_mali_uk_post_notification_s), sizeof(u64)));
769                 err = post_notification_wrapper(session_data, (_mali_uk_post_notification_s __user *)arg);
770                 break;
771
772     /* rk_ext : 从对 r5p0-01rel0 集成开始, 不再使用. */
773 #if 0
774         case MALI_IOC_GET_MALI_VERSION_IN_RK30:
775                 err = get_mali_version_in_rk30_wrapper(session_data, (_mali_uk_get_mali_version_in_rk30_s __user *)arg);
776                 break;
777 #else
778     case MALI_IOC_GET_RK_KO_VERSION:
779                 err = get_rk_ko_version_wrapper(session_data, (_mali_rk_ko_version_s __user *)arg);
780                 break;
781 #endif
782         
783         case MALI_IOC_GET_USER_SETTINGS:
784                 BUILD_BUG_ON(!IS_ALIGNED(sizeof(_mali_uk_get_user_settings_s), sizeof(u64)));
785                 err = get_user_settings_wrapper(session_data, (_mali_uk_get_user_settings_s __user *)arg);
786                 break;
787
788         case MALI_IOC_REQUEST_HIGH_PRIORITY:
789                 BUILD_BUG_ON(!IS_ALIGNED(sizeof(_mali_uk_request_high_priority_s), sizeof(u64)));
790                 err = request_high_priority_wrapper(session_data, (_mali_uk_request_high_priority_s __user *)arg);
791                 break;
792
793         case MALI_IOC_PENDING_SUBMIT:
794                 BUILD_BUG_ON(!IS_ALIGNED(sizeof(_mali_uk_pending_submit_s), sizeof(u64)));
795                 err = pending_submit_wrapper(session_data, (_mali_uk_pending_submit_s __user *)arg);
796                 break;
797
798 #if defined(CONFIG_MALI400_PROFILING)
799         case MALI_IOC_PROFILING_ADD_EVENT:
800                 BUILD_BUG_ON(!IS_ALIGNED(sizeof(_mali_uk_profiling_add_event_s), sizeof(u64)));
801                 err = profiling_add_event_wrapper(session_data, (_mali_uk_profiling_add_event_s __user *)arg);
802                 break;
803
804         case MALI_IOC_PROFILING_REPORT_SW_COUNTERS:
805                 BUILD_BUG_ON(!IS_ALIGNED(sizeof(_mali_uk_sw_counters_report_s), sizeof(u64)));
806                 err = profiling_report_sw_counters_wrapper(session_data, (_mali_uk_sw_counters_report_s __user *)arg);
807                 break;
808
809         case MALI_IOC_PROFILING_STREAM_FD_GET:
810                 BUILD_BUG_ON(!IS_ALIGNED(sizeof(_mali_uk_profiling_stream_fd_get_s), sizeof(u64)));
811                 err = profiling_get_stream_fd_wrapper(session_data, (_mali_uk_profiling_stream_fd_get_s __user *)arg);
812                 break;
813
814         case MALI_IOC_PROILING_CONTROL_SET:
815                 BUILD_BUG_ON(!IS_ALIGNED(sizeof(_mali_uk_profiling_control_set_s), sizeof(u64)));
816                 err = profiling_control_set_wrapper(session_data, (_mali_uk_profiling_control_set_s __user *)arg);
817                 break;
818 #else
819
820         case MALI_IOC_PROFILING_ADD_EVENT:          /* FALL-THROUGH */
821         case MALI_IOC_PROFILING_REPORT_SW_COUNTERS: /* FALL-THROUGH */
822                 MALI_DEBUG_PRINT(2, ("Profiling not supported\n"));
823                 err = -ENOTTY;
824                 break;
825 #endif
826
827         case MALI_IOC_PROFILING_MEMORY_USAGE_GET:
828                 BUILD_BUG_ON(!IS_ALIGNED(sizeof(_mali_uk_profiling_memory_usage_get_s), sizeof(u64)));
829                 err = mem_usage_get_wrapper(session_data, (_mali_uk_profiling_memory_usage_get_s __user *)arg);
830                 break;
831
832         case MALI_IOC_MEM_ALLOC:
833                 BUILD_BUG_ON(!IS_ALIGNED(sizeof(_mali_uk_alloc_mem_s), sizeof(u64)));
834                 err = mem_alloc_wrapper(session_data, (_mali_uk_alloc_mem_s __user *)arg);
835                 break;
836
837         case MALI_IOC_MEM_FREE:
838                 BUILD_BUG_ON(!IS_ALIGNED(sizeof(_mali_uk_free_mem_s), sizeof(u64)));
839                 err = mem_free_wrapper(session_data, (_mali_uk_free_mem_s __user *)arg);
840                 break;
841
842         case MALI_IOC_MEM_BIND:
843                 BUILD_BUG_ON(!IS_ALIGNED(sizeof(_mali_uk_bind_mem_s), sizeof(u64)));
844                 err = mem_bind_wrapper(session_data, (_mali_uk_bind_mem_s __user *)arg);
845                 break;
846
847         case MALI_IOC_MEM_UNBIND:
848                 BUILD_BUG_ON(!IS_ALIGNED(sizeof(_mali_uk_unbind_mem_s), sizeof(u64)));
849                 err = mem_unbind_wrapper(session_data, (_mali_uk_unbind_mem_s __user *)arg);
850                 break;
851
852         case MALI_IOC_MEM_COW:
853                 BUILD_BUG_ON(!IS_ALIGNED(sizeof(_mali_uk_cow_mem_s), sizeof(u64)));
854                 err = mem_cow_wrapper(session_data, (_mali_uk_cow_mem_s __user *)arg);
855                 break;
856
857         case MALI_IOC_MEM_COW_MODIFY_RANGE:
858                 BUILD_BUG_ON(!IS_ALIGNED(sizeof(_mali_uk_cow_modify_range_s), sizeof(u64)));
859                 err = mem_cow_modify_range_wrapper(session_data, (_mali_uk_cow_modify_range_s __user *)arg);
860                 break;
861
862         case MALI_IOC_MEM_RESIZE:
863                 BUILD_BUG_ON(!IS_ALIGNED(sizeof(_mali_uk_mem_resize_s), sizeof(u64)));
864                 err = mem_resize_mem_wrapper(session_data, (_mali_uk_mem_resize_s __user *)arg);
865                 break;
866
867         case MALI_IOC_MEM_WRITE_SAFE:
868                 BUILD_BUG_ON(!IS_ALIGNED(sizeof(_mali_uk_mem_write_safe_s), sizeof(u64)));
869                 err = mem_write_safe_wrapper(session_data, (_mali_uk_mem_write_safe_s __user *)arg);
870                 break;
871
872         case MALI_IOC_MEM_QUERY_MMU_PAGE_TABLE_DUMP_SIZE:
873                 BUILD_BUG_ON(!IS_ALIGNED(sizeof(_mali_uk_query_mmu_page_table_dump_size_s), sizeof(u64)));
874                 err = mem_query_mmu_page_table_dump_size_wrapper(session_data, (_mali_uk_query_mmu_page_table_dump_size_s __user *)arg);
875                 break;
876
877         case MALI_IOC_MEM_DUMP_MMU_PAGE_TABLE:
878                 BUILD_BUG_ON(!IS_ALIGNED(sizeof(_mali_uk_dump_mmu_page_table_s), sizeof(u64)));
879                 err = mem_dump_mmu_page_table_wrapper(session_data, (_mali_uk_dump_mmu_page_table_s __user *)arg);
880                 break;
881
882         case MALI_IOC_MEM_DMA_BUF_GET_SIZE:
883 #ifdef CONFIG_DMA_SHARED_BUFFER
884                 BUILD_BUG_ON(!IS_ALIGNED(sizeof(_mali_uk_dma_buf_get_size_s), sizeof(u64)));
885                 err = mali_dma_buf_get_size(session_data, (_mali_uk_dma_buf_get_size_s __user *)arg);
886 #else
887                 MALI_DEBUG_PRINT(2, ("DMA-BUF not supported\n"));
888                 err = -ENOTTY;
889 #endif
890                 break;
891
892         case MALI_IOC_PP_START_JOB:
893                 BUILD_BUG_ON(!IS_ALIGNED(sizeof(_mali_uk_pp_start_job_s), sizeof(u64)));
894                 err = pp_start_job_wrapper(session_data, (_mali_uk_pp_start_job_s __user *)arg);
895                 break;
896
897         case MALI_IOC_PP_AND_GP_START_JOB:
898                 BUILD_BUG_ON(!IS_ALIGNED(sizeof(_mali_uk_pp_and_gp_start_job_s), sizeof(u64)));
899                 err = pp_and_gp_start_job_wrapper(session_data, (_mali_uk_pp_and_gp_start_job_s __user *)arg);
900                 break;
901
902         case MALI_IOC_PP_NUMBER_OF_CORES_GET:
903                 BUILD_BUG_ON(!IS_ALIGNED(sizeof(_mali_uk_get_pp_number_of_cores_s), sizeof(u64)));
904                 err = pp_get_number_of_cores_wrapper(session_data, (_mali_uk_get_pp_number_of_cores_s __user *)arg);
905                 break;
906
907         case MALI_IOC_PP_CORE_VERSION_GET:
908                 BUILD_BUG_ON(!IS_ALIGNED(sizeof(_mali_uk_get_pp_core_version_s), sizeof(u64)));
909                 err = pp_get_core_version_wrapper(session_data, (_mali_uk_get_pp_core_version_s __user *)arg);
910                 break;
911
912         case MALI_IOC_PP_DISABLE_WB:
913                 BUILD_BUG_ON(!IS_ALIGNED(sizeof(_mali_uk_pp_disable_wb_s), sizeof(u64)));
914                 err = pp_disable_wb_wrapper(session_data, (_mali_uk_pp_disable_wb_s __user *)arg);
915                 break;
916
917         case MALI_IOC_GP2_START_JOB:
918                 BUILD_BUG_ON(!IS_ALIGNED(sizeof(_mali_uk_gp_start_job_s), sizeof(u64)));
919                 err = gp_start_job_wrapper(session_data, (_mali_uk_gp_start_job_s __user *)arg);
920                 break;
921
922         case MALI_IOC_GP2_NUMBER_OF_CORES_GET:
923                 BUILD_BUG_ON(!IS_ALIGNED(sizeof(_mali_uk_get_gp_number_of_cores_s), sizeof(u64)));
924                 err = gp_get_number_of_cores_wrapper(session_data, (_mali_uk_get_gp_number_of_cores_s __user *)arg);
925                 break;
926
927         case MALI_IOC_GP2_CORE_VERSION_GET:
928                 BUILD_BUG_ON(!IS_ALIGNED(sizeof(_mali_uk_get_gp_core_version_s), sizeof(u64)));
929                 err = gp_get_core_version_wrapper(session_data, (_mali_uk_get_gp_core_version_s __user *)arg);
930                 break;
931
932         case MALI_IOC_GP2_SUSPEND_RESPONSE:
933                 BUILD_BUG_ON(!IS_ALIGNED(sizeof(_mali_uk_gp_suspend_response_s), sizeof(u64)));
934                 err = gp_suspend_response_wrapper(session_data, (_mali_uk_gp_suspend_response_s __user *)arg);
935                 break;
936
937         case MALI_IOC_VSYNC_EVENT_REPORT:
938                 BUILD_BUG_ON(!IS_ALIGNED(sizeof(_mali_uk_vsync_event_report_s), sizeof(u64)));
939                 err = vsync_event_report_wrapper(session_data, (_mali_uk_vsync_event_report_s __user *)arg);
940                 break;
941
942         case MALI_IOC_TIMELINE_GET_LATEST_POINT:
943                 BUILD_BUG_ON(!IS_ALIGNED(sizeof(_mali_uk_timeline_get_latest_point_s), sizeof(u64)));
944                 err = timeline_get_latest_point_wrapper(session_data, (_mali_uk_timeline_get_latest_point_s __user *)arg);
945                 break;
946         case MALI_IOC_TIMELINE_WAIT:
947                 BUILD_BUG_ON(!IS_ALIGNED(sizeof(_mali_uk_timeline_wait_s), sizeof(u64)));
948                 err = timeline_wait_wrapper(session_data, (_mali_uk_timeline_wait_s __user *)arg);
949                 break;
950         case MALI_IOC_TIMELINE_CREATE_SYNC_FENCE:
951                 BUILD_BUG_ON(!IS_ALIGNED(sizeof(_mali_uk_timeline_create_sync_fence_s), sizeof(u64)));
952                 err = timeline_create_sync_fence_wrapper(session_data, (_mali_uk_timeline_create_sync_fence_s __user *)arg);
953                 break;
954         case MALI_IOC_SOFT_JOB_START:
955                 BUILD_BUG_ON(!IS_ALIGNED(sizeof(_mali_uk_soft_job_start_s), sizeof(u64)));
956                 err = soft_job_start_wrapper(session_data, (_mali_uk_soft_job_start_s __user *)arg);
957                 break;
958         case MALI_IOC_SOFT_JOB_SIGNAL:
959                 BUILD_BUG_ON(!IS_ALIGNED(sizeof(_mali_uk_soft_job_signal_s), sizeof(u64)));
960                 err = soft_job_signal_wrapper(session_data, (_mali_uk_soft_job_signal_s __user *)arg);
961                 break;
962
963         default:
964                 MALI_DEBUG_PRINT(2, ("No handler for ioctl 0x%08X 0x%08lX\n", cmd, arg));
965                 err = -ENOTTY;
966         };
967
968         return err;
969 }
970
971
972 module_init(mali_module_init);
973 module_exit(mali_module_exit);
974
975 MODULE_LICENSE(MALI_KERNEL_LINUX_LICENSE);
976 MODULE_AUTHOR("ARM Ltd.");
977 MODULE_VERSION(SVN_REV_STRING);