Merge branch 'linux-linaro-lsk-v4.4-android' of git://git.linaro.org/kernel/linux...
[firefly-linux-kernel-4.4.55.git] / drivers / video / rockchip / rga2 / rga2_drv.c
1 /*
2  * Copyright (C) 2012 ROCKCHIP, Inc.
3  *
4  * This software is licensed under the terms of the GNU General Public
5  * License version 2, as published by the Free Software Foundation, and
6  * may be copied, distributed, and modified under those terms.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  */
14
15 #define pr_fmt(fmt) "rga: " fmt
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/sched.h>
21 #include <linux/mutex.h>
22 #include <linux/err.h>
23 #include <linux/clk.h>
24 #include <asm/delay.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/delay.h>
27 #include <asm/io.h>
28 #include <linux/irq.h>
29 #include <linux/interrupt.h>
30 #include <linux/fs.h>
31 #include <asm/uaccess.h>
32 #include <linux/miscdevice.h>
33 #include <linux/poll.h>
34 #include <linux/delay.h>
35 #include <linux/wait.h>
36 #include <linux/syscalls.h>
37 #include <linux/timer.h>
38 #include <linux/time.h>
39 #include <asm/cacheflush.h>
40 #include <linux/slab.h>
41 #include <linux/fb.h>
42 #include <linux/wakelock.h>
43 #include <linux/scatterlist.h>
44 #include <linux/rockchip_ion.h>
45 #include <linux/version.h>
46 #include <linux/pm_runtime.h>
47
48 #include "rga2.h"
49 #include "rga2_reg_info.h"
50 #include "rga2_mmu_info.h"
51 #include "RGA2_API.h"
52 #include "rga2_rop.h"
53
54 #if defined(CONFIG_RK_IOMMU) && defined(CONFIG_ION_ROCKCHIP)
55 #define CONFIG_RGA_IOMMU
56 #endif
57
58 #define RGA2_TEST_FLUSH_TIME 0
59 #define RGA2_INFO_BUS_ERROR 1
60 #define RGA2_POWER_OFF_DELAY    4*HZ /* 4s */
61 #define RGA2_TIMEOUT_DELAY      2*HZ /* 2s */
62 #define RGA2_MAJOR              255
63 #define RGA2_RESET_TIMEOUT      1000
64
65 /* Driver information */
66 #define DRIVER_DESC             "RGA2 Device Driver"
67 #define DRIVER_NAME             "rga2"
68 #define RGA2_VERSION   "2.000"
69
70 ktime_t rga2_start;
71 ktime_t rga2_end;
72 int rga2_flag;
73 int first_RGA2_proc;
74
75 rga2_session rga2_session_global;
76 long (*rga_ioctl_kernel_p)(struct rga_req *);
77
78 struct rga2_drvdata_t {
79         struct miscdevice miscdev;
80         struct device *dev;
81         void *rga_base;
82         int irq;
83
84         struct delayed_work power_off_work;
85         struct wake_lock wake_lock;
86         void (*rga_irq_callback)(int rga_retval);
87
88         struct clk *aclk_rga2;
89         struct clk *hclk_rga2;
90         struct clk *rga2;
91
92         struct ion_client * ion_client;
93 };
94
95 struct rga2_drvdata_t *rga2_drvdata;
96 struct rga2_service_info rga2_service;
97 struct rga2_mmu_buf_t rga2_mmu_buf;
98
99 static int rga2_blit_async(rga2_session *session, struct rga2_req *req);
100 static void rga2_del_running_list(void);
101 static void rga2_del_running_list_timeout(void);
102 static void rga2_try_set_reg(void);
103
104
105 /* Logging */
106 #define RGA_DEBUG 0
107 #if RGA_DEBUG
108 #define DBG(format, args...) printk(KERN_DEBUG "%s: " format, DRIVER_NAME, ## args)
109 #define ERR(format, args...) printk(KERN_ERR "%s: " format, DRIVER_NAME, ## args)
110 #define WARNING(format, args...) printk(KERN_WARN "%s: " format, DRIVER_NAME, ## args)
111 #define INFO(format, args...) printk(KERN_INFO "%s: " format, DRIVER_NAME, ## args)
112 #else
113 #define DBG(format, args...)
114 #define ERR(format, args...)
115 #define WARNING(format, args...)
116 #define INFO(format, args...)
117 #endif
118
119 #if RGA2_TEST_MSG
120 static void print_info(struct rga2_req *req)
121 {
122         printk("render_mode=%d bitblt_mode=%d rotate_mode=%.8x\n",
123                 req->render_mode, req->bitblt_mode, req->rotate_mode);
124         printk("src : y=%.lx uv=%.lx v=%.lx format=%d aw=%d ah=%d vw=%d vh=%d xoff=%d yoff=%d \n",
125                 req->src.yrgb_addr, req->src.uv_addr, req->src.v_addr, req->src.format,
126                 req->src.act_w, req->src.act_h, req->src.vir_w, req->src.vir_h,
127                 req->src.x_offset, req->src.y_offset);
128         printk("dst : y=%lx uv=%lx v=%lx format=%d aw=%d ah=%d vw=%d vh=%d xoff=%d yoff=%d \n",
129                 req->dst.yrgb_addr, req->dst.uv_addr, req->dst.v_addr, req->dst.format,
130                 req->dst.act_w, req->dst.act_h, req->dst.vir_w, req->dst.vir_h,
131                 req->dst.x_offset, req->dst.y_offset);
132         printk("mmu : src=%.2x src1=%.2x dst=%.2x els=%.2x\n",
133                 req->mmu_info.src0_mmu_flag, req->mmu_info.src1_mmu_flag,
134                 req->mmu_info.dst_mmu_flag,  req->mmu_info.els_mmu_flag);
135         printk("alpha : flag %.8x mode0=%.8x mode1=%.8x\n",
136                 req->alpha_rop_flag, req->alpha_mode_0, req->alpha_mode_1);
137 }
138 #endif
139
140 static inline void rga2_write(u32 b, u32 r)
141 {
142         *((volatile unsigned int *)(rga2_drvdata->rga_base + r)) = b;
143 }
144
145 static inline u32 rga2_read(u32 r)
146 {
147         return *((volatile unsigned int *)(rga2_drvdata->rga_base + r));
148 }
149
150 static void rga2_soft_reset(void)
151 {
152         u32 i;
153         u32 reg;
154
155         rga2_write((1 << 3) | (1 << 4), RGA2_SYS_CTRL); //RGA_SYS_CTRL
156
157         for(i = 0; i < RGA2_RESET_TIMEOUT; i++)
158         {
159                 reg = rga2_read(RGA2_SYS_CTRL) & 1; //RGA_SYS_CTRL
160
161                 if(reg == 0)
162                         break;
163
164                 udelay(1);
165         }
166
167         if(i == RGA2_RESET_TIMEOUT)
168                 ERR("soft reset timeout.\n");
169 }
170
171 static void rga2_dump(void)
172 {
173         int running;
174         struct rga2_reg *reg, *reg_tmp;
175         rga2_session *session, *session_tmp;
176
177         running = atomic_read(&rga2_service.total_running);
178         printk("rga total_running %d\n", running);
179         list_for_each_entry_safe(session, session_tmp, &rga2_service.session,
180                 list_session)
181         {
182                 printk("session pid %d:\n", session->pid);
183                 running = atomic_read(&session->task_running);
184                 printk("task_running %d\n", running);
185                 list_for_each_entry_safe(reg, reg_tmp, &session->waiting, session_link)
186                 {
187                         printk("waiting register set 0x%.lu\n", (unsigned long)reg);
188                 }
189                 list_for_each_entry_safe(reg, reg_tmp, &session->running, session_link)
190                 {
191                         printk("running register set 0x%.lu\n", (unsigned long)reg);
192                 }
193         }
194 }
195
196 static inline void rga2_queue_power_off_work(void)
197 {
198         queue_delayed_work(system_wq, &rga2_drvdata->power_off_work,
199                 RGA2_POWER_OFF_DELAY);
200 }
201
202 /* Caller must hold rga_service.lock */
203 static void rga2_power_on(void)
204 {
205         static ktime_t last;
206         ktime_t now = ktime_get();
207
208 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0))
209         pm_runtime_get_sync(rga2_drvdata->dev);
210 #endif
211
212         if (ktime_to_ns(ktime_sub(now, last)) > NSEC_PER_SEC) {
213                 cancel_delayed_work_sync(&rga2_drvdata->power_off_work);
214                 rga2_queue_power_off_work();
215                 last = now;
216         }
217
218         if (rga2_service.enable)
219                 return;
220
221         clk_prepare_enable(rga2_drvdata->rga2);
222         clk_prepare_enable(rga2_drvdata->aclk_rga2);
223         clk_prepare_enable(rga2_drvdata->hclk_rga2);
224         wake_lock(&rga2_drvdata->wake_lock);
225         rga2_service.enable = true;
226 }
227
228 /* Caller must hold rga_service.lock */
229 static void rga2_power_off(void)
230 {
231         int total_running;
232
233         if (!rga2_service.enable) {
234                 return;
235         }
236
237         total_running = atomic_read(&rga2_service.total_running);
238         if (total_running) {
239                 pr_err("power off when %d task running!!\n", total_running);
240                 mdelay(50);
241                 pr_err("delay 50 ms for running task\n");
242                 rga2_dump();
243         }
244
245         clk_disable_unprepare(rga2_drvdata->rga2);
246         clk_disable_unprepare(rga2_drvdata->aclk_rga2);
247         clk_disable_unprepare(rga2_drvdata->hclk_rga2);
248
249 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0))
250         pm_runtime_put(rga2_drvdata->dev);
251 #endif
252
253         wake_unlock(&rga2_drvdata->wake_lock);
254     first_RGA2_proc = 0;
255         rga2_service.enable = false;
256 }
257
258 static void rga2_power_off_work(struct work_struct *work)
259 {
260         if (mutex_trylock(&rga2_service.lock)) {
261                 rga2_power_off();
262                 mutex_unlock(&rga2_service.lock);
263         } else {
264                 /* Come back later if the device is busy... */
265                 rga2_queue_power_off_work();
266         }
267 }
268
269 static int rga2_flush(rga2_session *session, unsigned long arg)
270 {
271     int ret = 0;
272     int ret_timeout;
273
274     #if RGA2_TEST_FLUSH_TIME
275     ktime_t start;
276     ktime_t end;
277     start = ktime_get();
278     #endif
279
280     ret_timeout = wait_event_timeout(session->wait, atomic_read(&session->done), RGA2_TIMEOUT_DELAY);
281
282         if (unlikely(ret_timeout < 0)) {
283                 //pr_err("flush pid %d wait task ret %d\n", session->pid, ret);
284         mutex_lock(&rga2_service.lock);
285         rga2_del_running_list();
286         mutex_unlock(&rga2_service.lock);
287         ret = ret_timeout;
288         } else if (0 == ret_timeout) {
289                 //pr_err("flush pid %d wait %d task done timeout\n", session->pid, atomic_read(&session->task_running));
290         //printk("bus  = %.8x\n", rga_read(RGA_INT));
291         mutex_lock(&rga2_service.lock);
292         rga2_del_running_list_timeout();
293         rga2_try_set_reg();
294         mutex_unlock(&rga2_service.lock);
295                 ret = -ETIMEDOUT;
296         }
297
298     #if RGA2_TEST_FLUSH_TIME
299     end = ktime_get();
300     end = ktime_sub(end, start);
301     printk("one flush wait time %d\n", (int)ktime_to_us(end));
302     #endif
303
304         return ret;
305 }
306
307
308 static int rga2_get_result(rga2_session *session, unsigned long arg)
309 {
310         int ret = 0;
311         int num_done;
312
313         num_done = atomic_read(&session->num_done);
314         if (unlikely(copy_to_user((void __user *)arg, &num_done, sizeof(int)))) {
315             printk("copy_to_user failed\n");
316             ret =  -EFAULT;
317         }
318         return ret;
319 }
320
321
322 static int rga2_check_param(const struct rga2_req *req)
323 {
324         if(!((req->render_mode == color_fill_mode)))
325         {
326             if (unlikely((req->src.act_w <= 0) || (req->src.act_w > 8191) || (req->src.act_h <= 0) || (req->src.act_h > 8191)))
327             {
328                 printk("invalid source resolution act_w = %d, act_h = %d\n", req->src.act_w, req->src.act_h);
329                 return -EINVAL;
330             }
331         }
332
333         if(!((req->render_mode == color_fill_mode)))
334         {
335             if (unlikely((req->src.vir_w <= 0) || (req->src.vir_w > 8191) || (req->src.vir_h <= 0) || (req->src.vir_h > 8191)))
336             {
337                 printk("invalid source resolution vir_w = %d, vir_h = %d\n", req->src.vir_w, req->src.vir_h);
338                 return -EINVAL;
339             }
340         }
341
342         //check dst width and height
343         if (unlikely((req->dst.act_w <= 0) || (req->dst.act_w > 4096) || (req->dst.act_h <= 0) || (req->dst.act_h > 4096)))
344         {
345             printk("invalid destination resolution act_w = %d, act_h = %d\n", req->dst.act_w, req->dst.act_h);
346             return -EINVAL;
347         }
348
349         if (unlikely((req->dst.vir_w <= 0) || (req->dst.vir_w > 4096) || (req->dst.vir_h <= 0) || (req->dst.vir_h > 4096)))
350         {
351             printk("invalid destination resolution vir_w = %d, vir_h = %d\n", req->dst.vir_w, req->dst.vir_h);
352             return -EINVAL;
353         }
354
355         //check src_vir_w
356         if(unlikely(req->src.vir_w < req->src.act_w)){
357             printk("invalid src_vir_w act_w = %d, vir_w = %d\n", req->src.act_w, req->src.vir_w);
358             return -EINVAL;
359         }
360
361         //check dst_vir_w
362         if(unlikely(req->dst.vir_w < req->dst.act_w)){
363             if(req->rotate_mode != 1)
364             {
365                 printk("invalid dst_vir_w act_h = %d, vir_h = %d\n", req->dst.act_w, req->dst.vir_w);
366                 return -EINVAL;
367             }
368         }
369
370         return 0;
371 }
372
373 static void rga2_copy_reg(struct rga2_reg *reg, uint32_t offset)
374 {
375     uint32_t i;
376     uint32_t *cmd_buf;
377     uint32_t *reg_p;
378
379     if(atomic_read(&reg->session->task_running) != 0)
380         printk(KERN_ERR "task_running is no zero\n");
381
382     atomic_add(1, &rga2_service.cmd_num);
383         atomic_add(1, &reg->session->task_running);
384
385     cmd_buf = (uint32_t *)rga2_service.cmd_buff + offset*32;
386     reg_p = (uint32_t *)reg->cmd_reg;
387
388     for(i=0; i<32; i++)
389         cmd_buf[i] = reg_p[i];
390 }
391
392
393 static struct rga2_reg * rga2_reg_init(rga2_session *session, struct rga2_req *req)
394 {
395     int32_t ret;
396         struct rga2_reg *reg = kzalloc(sizeof(struct rga2_reg), GFP_KERNEL);
397         if (NULL == reg) {
398                 pr_err("kmalloc fail in rga_reg_init\n");
399                 return NULL;
400         }
401
402     reg->session = session;
403         INIT_LIST_HEAD(&reg->session_link);
404         INIT_LIST_HEAD(&reg->status_link);
405
406     reg->MMU_base = NULL;
407
408     if ((req->mmu_info.src0_mmu_flag & 1) || (req->mmu_info.src1_mmu_flag & 1)
409         || (req->mmu_info.dst_mmu_flag & 1) || (req->mmu_info.els_mmu_flag & 1))
410     {
411         ret = rga2_set_mmu_info(reg, req);
412         if(ret < 0) {
413             printk("%s, [%d] set mmu info error \n", __FUNCTION__, __LINE__);
414             if(reg != NULL)
415                 kfree(reg);
416
417             return NULL;
418         }
419     }
420
421     if(RGA2_gen_reg_info((uint8_t *)reg->cmd_reg, req) == -1) {
422         printk("gen reg info error\n");
423         if(reg != NULL)
424             kfree(reg);
425
426         return NULL;
427     }
428
429     mutex_lock(&rga2_service.lock);
430         list_add_tail(&reg->status_link, &rga2_service.waiting);
431         list_add_tail(&reg->session_link, &session->waiting);
432         mutex_unlock(&rga2_service.lock);
433
434     return reg;
435 }
436
437
438 /* Caller must hold rga_service.lock */
439 static void rga2_reg_deinit(struct rga2_reg *reg)
440 {
441         list_del_init(&reg->session_link);
442         list_del_init(&reg->status_link);
443         kfree(reg);
444 }
445
446 /* Caller must hold rga_service.lock */
447 static void rga2_reg_from_wait_to_run(struct rga2_reg *reg)
448 {
449         list_del_init(&reg->status_link);
450         list_add_tail(&reg->status_link, &rga2_service.running);
451
452         list_del_init(&reg->session_link);
453         list_add_tail(&reg->session_link, &reg->session->running);
454 }
455
456 /* Caller must hold rga_service.lock */
457 static void rga2_service_session_clear(rga2_session *session)
458 {
459         struct rga2_reg *reg, *n;
460
461         list_for_each_entry_safe(reg, n, &session->waiting, session_link)
462         {
463                 rga2_reg_deinit(reg);
464         }
465
466         list_for_each_entry_safe(reg, n, &session->running, session_link)
467         {
468                 rga2_reg_deinit(reg);
469         }
470 }
471
472 /* Caller must hold rga_service.lock */
473 static void rga2_try_set_reg(void)
474 {
475         struct rga2_reg *reg ;
476
477         if (list_empty(&rga2_service.running))
478         {
479                 if (!list_empty(&rga2_service.waiting))
480                 {
481                         /* RGA is idle */
482                         reg = list_entry(rga2_service.waiting.next, struct rga2_reg, status_link);
483
484                         rga2_power_on();
485                         udelay(1);
486
487                         rga2_copy_reg(reg, 0);
488                         rga2_reg_from_wait_to_run(reg);
489
490 #ifdef CONFIG_ARM
491                         dmac_flush_range(&rga2_service.cmd_buff[0], &rga2_service.cmd_buff[32]);
492                         outer_flush_range(virt_to_phys(&rga2_service.cmd_buff[0]),virt_to_phys(&rga2_service.cmd_buff[32]));
493 #elif defined(CONFIG_ARM64)
494                         __dma_flush_range(&rga2_service.cmd_buff[0], &rga2_service.cmd_buff[32]);
495 #endif
496
497                         //rga2_soft_reset();
498
499                         rga2_write(0x0, RGA2_SYS_CTRL);
500
501                         /* CMD buff */
502                         rga2_write(virt_to_phys(rga2_service.cmd_buff), RGA2_CMD_BASE);
503
504 #if RGA2_TEST
505                         if(rga2_flag) {
506                                 int32_t i, *p;
507                                 p = rga2_service.cmd_buff;
508                                 printk("CMD_REG\n");
509                                 for (i=0; i<8; i++)
510                                         printk("%.8x %.8x %.8x %.8x\n", p[0 + i*4], p[1+i*4], p[2 + i*4], p[3 + i*4]);
511                         }
512 #endif
513
514                         /* master mode */
515                         rga2_write((0x1<<1)|(0x1<<2)|(0x1<<5)|(0x1<<6), RGA2_SYS_CTRL);
516
517                         /* All CMD finish int */
518                         rga2_write(rga2_read(RGA2_INT)|(0x1<<10)|(0x1<<9)|(0x1<<8), RGA2_INT);
519
520 #if RGA2_TEST_TIME
521                         rga2_start = ktime_get();
522 #endif
523
524                         /* Start proc */
525                         atomic_set(&reg->session->done, 0);
526                         rga2_write(0x1, RGA2_CMD_CTRL);
527 #if RGA2_TEST
528                         if(rga2_flag)
529                         {
530                                 uint32_t i;
531                                 printk("CMD_READ_BACK_REG\n");
532                                 for (i=0; i<8; i++)
533                                         printk("%.8x %.8x %.8x %.8x\n", rga2_read(0x100 + i*16 + 0),
534                                                         rga2_read(0x100 + i*16 + 4), rga2_read(0x100 + i*16 + 8), rga2_read(0x100 + i*16 + 12));
535                         }
536 #endif
537                 }
538         }
539 }
540
541 static void rga2_del_running_list(void)
542 {
543         struct rga2_mmu_buf_t *tbuf = &rga2_mmu_buf;
544         struct rga2_reg *reg;
545
546         while (!list_empty(&rga2_service.running)) {
547                 reg = list_entry(rga2_service.running.next, struct rga2_reg,
548                                  status_link);
549                 if (reg->MMU_len && tbuf) {
550                         if (tbuf->back + reg->MMU_len > 2 * tbuf->size)
551                                 tbuf->back = reg->MMU_len + tbuf->size;
552                         else
553                                 tbuf->back += reg->MMU_len;
554                 }
555                 atomic_sub(1, &reg->session->task_running);
556                 atomic_sub(1, &rga2_service.total_running);
557
558                 if(list_empty(&reg->session->waiting))
559                 {
560                         atomic_set(&reg->session->done, 1);
561                         wake_up(&reg->session->wait);
562                 }
563
564                 rga2_reg_deinit(reg);
565         }
566 }
567
568 static void rga2_del_running_list_timeout(void)
569 {
570         struct rga2_mmu_buf_t *tbuf = &rga2_mmu_buf;
571         struct rga2_reg *reg;
572
573         while (!list_empty(&rga2_service.running)) {
574                 reg = list_entry(rga2_service.running.next, struct rga2_reg,
575                                  status_link);
576                 kfree(reg->MMU_base);
577                 if (reg->MMU_len && tbuf) {
578                         if (tbuf->back + reg->MMU_len > 2 * tbuf->size)
579                                 tbuf->back = reg->MMU_len + tbuf->size;
580                         else
581                                 tbuf->back += reg->MMU_len;
582                 }
583                 atomic_sub(1, &reg->session->task_running);
584                 atomic_sub(1, &rga2_service.total_running);
585                 rga2_soft_reset();
586                 if (list_empty(&reg->session->waiting)) {
587                         atomic_set(&reg->session->done, 1);
588                         wake_up(&reg->session->wait);
589                 }
590                 rga2_reg_deinit(reg);
591         }
592         return;
593 }
594
595 static int rga2_convert_dma_buf(struct rga2_req *req)
596 {
597         struct ion_handle *hdl;
598         ion_phys_addr_t phy_addr;
599         size_t len;
600         int ret;
601         uint32_t src_vir_w, dst_vir_w;
602
603         src_vir_w = req->src.vir_w;
604         dst_vir_w = req->dst.vir_w;
605
606         req->sg_src0 = NULL;
607         req->sg_src1 = NULL;
608         req->sg_dst  = NULL;
609         req->sg_els  = NULL;
610
611         if ((int)req->src.yrgb_addr > 0) {
612                 hdl = ion_import_dma_buf(rga2_drvdata->ion_client,
613                                          req->src.yrgb_addr);
614                 if (IS_ERR(hdl)) {
615                         ret = PTR_ERR(hdl);
616                         printk("RGA2 SRC ERROR ion buf handle\n");
617                         return ret;
618                 }
619                 if (req->mmu_info.src0_mmu_flag) {
620                         req->sg_src0 = ion_sg_table(rga2_drvdata->ion_client,
621                                                     hdl);
622                         req->src.yrgb_addr = req->src.uv_addr;
623                         req->src.uv_addr = req->src.yrgb_addr
624                                         + (src_vir_w * req->src.vir_h);
625                         req->src.v_addr = req->src.uv_addr
626                                         + (src_vir_w * req->src.vir_h) / 4;
627                 } else {
628                         ion_phys(rga2_drvdata->ion_client, hdl, &phy_addr,
629                                  &len);
630                         req->src.yrgb_addr = phy_addr;
631                         req->src.uv_addr = req->src.yrgb_addr
632                                         + (src_vir_w * req->src.vir_h);
633                         req->src.v_addr = req->src.uv_addr
634                                         + (src_vir_w * req->src.vir_h) / 4;
635                 }
636                 ion_free(rga2_drvdata->ion_client, hdl);
637         } else {
638                 req->src.yrgb_addr = req->src.uv_addr;
639                 req->src.uv_addr = req->src.yrgb_addr
640                                         + (src_vir_w * req->src.vir_h);
641                 req->src.v_addr = req->src.uv_addr
642                                         + (src_vir_w * req->src.vir_h) / 4;
643         }
644
645         if ((int)req->dst.yrgb_addr > 0) {
646                 hdl = ion_import_dma_buf(rga2_drvdata->ion_client,
647                                          req->dst.yrgb_addr);
648                 if (IS_ERR(hdl)) {
649                         ret = PTR_ERR(hdl);
650                         printk("RGA2 DST ERROR ion buf handle\n");
651                         return ret;
652                 }
653                 if (req->mmu_info.dst_mmu_flag) {
654                         req->sg_dst = ion_sg_table(rga2_drvdata->ion_client,
655                                                    hdl);
656                         req->dst.yrgb_addr = req->dst.uv_addr;
657                         req->dst.uv_addr = req->dst.yrgb_addr
658                                         + (dst_vir_w * req->dst.vir_h);
659                         req->dst.v_addr = req->dst.uv_addr
660                                         + (dst_vir_w * req->dst.vir_h) / 4;
661                 } else {
662                         ion_phys(rga2_drvdata->ion_client, hdl, &phy_addr,
663                                  &len);
664                         req->dst.yrgb_addr = phy_addr;
665                         req->dst.uv_addr = req->dst.yrgb_addr
666                                         + (dst_vir_w * req->dst.vir_h);
667                         req->dst.v_addr = req->dst.uv_addr
668                                         + (dst_vir_w * req->dst.vir_h) / 4;
669                 }
670                 ion_free(rga2_drvdata->ion_client, hdl);
671         } else {
672                 req->dst.yrgb_addr = req->dst.uv_addr;
673                 req->dst.uv_addr = req->dst.yrgb_addr
674                                         + (dst_vir_w * req->dst.vir_h);
675                 req->dst.v_addr = req->dst.uv_addr
676                                         + (dst_vir_w * req->dst.vir_h) / 4;
677         }
678
679         if ((int)req->src1.yrgb_addr > 0) {
680                 hdl = ion_import_dma_buf(rga2_drvdata->ion_client,
681                                          req->src1.yrgb_addr);
682                 if (IS_ERR(hdl)) {
683                         ret = PTR_ERR(hdl);
684                         printk("RGA2 ERROR ion buf handle\n");
685                         return ret;
686                 }
687                 if (req->mmu_info.dst_mmu_flag) {
688                         req->sg_src1 = ion_sg_table(rga2_drvdata->ion_client,
689                                                     hdl);
690                         req->src1.yrgb_addr = req->src1.uv_addr;
691                         req->src1.uv_addr = req->src1.yrgb_addr
692                                         + (req->src1.vir_w * req->src1.vir_h);
693                         req->src1.v_addr = req->src1.uv_addr
694                                 + (req->src1.vir_w * req->src1.vir_h) / 4;
695                 } else {
696                         ion_phys(rga2_drvdata->ion_client, hdl, &phy_addr,
697                                  &len);
698                         req->src1.yrgb_addr = phy_addr;
699                         req->src1.uv_addr = req->src1.yrgb_addr
700                                         + (req->src1.vir_w * req->src1.vir_h);
701                         req->src1.v_addr = req->src1.uv_addr
702                                 + (req->src1.vir_w * req->src1.vir_h) / 4;
703                 }
704                 ion_free(rga2_drvdata->ion_client, hdl);
705         } else {
706                 req->src1.yrgb_addr = req->src1.uv_addr;
707                 req->src1.uv_addr = req->src1.yrgb_addr
708                                         + (req->src1.vir_w * req->src1.vir_h);
709                 req->src1.v_addr = req->src1.uv_addr
710                                 + (req->src1.vir_w * req->src1.vir_h) / 4;
711         }
712
713         return 0;
714 }
715
716 static int rga2_blit(rga2_session *session, struct rga2_req *req)
717 {
718         int ret = -1;
719         int num = 0;
720         struct rga2_reg *reg;
721
722         if(rga2_convert_dma_buf(req)) {
723                 printk("RGA2 : DMA buf copy error\n");
724                 return -EFAULT;
725         }
726
727         do {
728                 /* check value if legal */
729                 ret = rga2_check_param(req);
730                 if(ret == -EINVAL) {
731                         printk("req argument is inval\n");
732                         break;
733                 }
734
735                 reg = rga2_reg_init(session, req);
736                 if(reg == NULL) {
737                         break;
738                 }
739                 num = 1;
740
741                 mutex_lock(&rga2_service.lock);
742                 atomic_add(num, &rga2_service.total_running);
743                 rga2_try_set_reg();
744                 mutex_unlock(&rga2_service.lock);
745
746                 return 0;
747         }
748         while(0);
749
750         return -EFAULT;
751 }
752
753 static int rga2_blit_async(rga2_session *session, struct rga2_req *req)
754 {
755         int ret = -1;
756
757 #if RGA2_TEST_MSG
758         if (1) {//req->src.format >= 0x10) {
759                 print_info(req);
760                 rga2_flag = 1;
761                 printk("*** rga_blit_async proc ***\n");
762         }
763         else
764                 rga2_flag = 0;
765 #endif
766         atomic_set(&session->done, 0);
767         ret = rga2_blit(session, req);
768
769         return ret;
770         }
771
772 static int rga2_blit_sync(rga2_session *session, struct rga2_req *req)
773 {
774         int ret = -1;
775         int ret_timeout = 0;
776
777 #if RGA2_TEST_MSG
778         if (1) {//req->bitblt_mode == 0x2) {
779                 print_info(req);
780                 rga2_flag = 1;
781                 printk("*** rga2_blit_sync proc ***\n");
782         }
783         else
784                 rga2_flag = 0;
785 #endif
786
787         atomic_set(&session->done, 0);
788
789         ret = rga2_blit(session, req);
790         if(ret < 0)
791                 return ret;
792
793         ret_timeout = wait_event_timeout(session->wait, atomic_read(&session->done), RGA2_TIMEOUT_DELAY);
794
795         if (unlikely(ret_timeout< 0))
796         {
797                 //pr_err("sync pid %d wait task ret %d\n", session->pid, ret_timeout);
798                 mutex_lock(&rga2_service.lock);
799                 rga2_del_running_list();
800                 mutex_unlock(&rga2_service.lock);
801                 ret = ret_timeout;
802         }
803         else if (0 == ret_timeout)
804         {
805                 //pr_err("sync pid %d wait %d task done timeout\n", session->pid, atomic_read(&session->task_running));
806                 mutex_lock(&rga2_service.lock);
807                 rga2_del_running_list_timeout();
808                 rga2_try_set_reg();
809                 mutex_unlock(&rga2_service.lock);
810                 ret = -ETIMEDOUT;
811         }
812
813 #if RGA2_TEST_TIME
814         rga2_end = ktime_get();
815         rga2_end = ktime_sub(rga2_end, rga2_start);
816         printk("sync one cmd end time %d\n", (int)ktime_to_us(rga2_end));
817 #endif
818
819         return ret;
820         }
821
822 static long rga_ioctl(struct file *file, uint32_t cmd, unsigned long arg)
823 {
824         struct rga2_req req, req_first;
825         struct rga_req req_rga;
826         int ret = 0;
827         rga2_session *session;
828
829         memset(&req, 0x0, sizeof(req));
830
831         mutex_lock(&rga2_service.mutex);
832
833         session = (rga2_session *)file->private_data;
834
835         if (NULL == session)
836         {
837                 printk("%s [%d] rga thread session is null\n",__FUNCTION__,__LINE__);
838                 mutex_unlock(&rga2_service.mutex);
839                 return -EINVAL;
840         }
841
842         memset(&req, 0x0, sizeof(req));
843
844         switch (cmd)
845         {
846                 case RGA_BLIT_SYNC:
847                         if (unlikely(copy_from_user(&req_rga, (struct rga_req*)arg, sizeof(struct rga_req))))
848                         {
849                                 ERR("copy_from_user failed\n");
850                                 ret = -EFAULT;
851                                 break;
852                         }
853                         RGA_MSG_2_RGA2_MSG(&req_rga, &req);
854
855                         if (first_RGA2_proc == 0 && req.bitblt_mode == bitblt_mode && rga2_service.dev_mode == 1) {
856                                 memcpy(&req_first, &req, sizeof(struct rga2_req));
857                                 if ((req_first.src.act_w != req_first.dst.act_w)
858                                                 || (req_first.src.act_h != req_first.dst.act_h)) {
859                                         req_first.src.act_w = MIN(320, MIN(req_first.src.act_w, req_first.dst.act_w));
860                                         req_first.src.act_h = MIN(240, MIN(req_first.src.act_h, req_first.dst.act_h));
861                                         req_first.dst.act_w = req_first.src.act_w;
862                                         req_first.dst.act_h = req_first.src.act_h;
863                                         ret = rga2_blit_async(session, &req_first);
864                                 }
865                                 ret = rga2_blit_sync(session, &req);
866                                 first_RGA2_proc = 1;
867                         }
868                         else {
869                                 ret = rga2_blit_sync(session, &req);
870                         }
871                         break;
872                 case RGA_BLIT_ASYNC:
873                         if (unlikely(copy_from_user(&req_rga, (struct rga_req*)arg, sizeof(struct rga_req))))
874                         {
875                                 ERR("copy_from_user failed\n");
876                                 ret = -EFAULT;
877                                 break;
878                         }
879
880                         RGA_MSG_2_RGA2_MSG(&req_rga, &req);
881
882                         if (first_RGA2_proc == 0 && req.bitblt_mode == bitblt_mode && rga2_service.dev_mode == 1) {
883                                 memcpy(&req_first, &req, sizeof(struct rga2_req));
884                                 if ((req_first.src.act_w != req_first.dst.act_w)
885                                                 || (req_first.src.act_h != req_first.dst.act_h)) {
886                                         req_first.src.act_w = MIN(320, MIN(req_first.src.act_w, req_first.dst.act_w));
887                                         req_first.src.act_h = MIN(240, MIN(req_first.src.act_h, req_first.dst.act_h));
888                                         req_first.dst.act_w = req_first.src.act_w;
889                                         req_first.dst.act_h = req_first.src.act_h;
890                                         ret = rga2_blit_async(session, &req_first);
891                                 }
892                                 ret = rga2_blit_async(session, &req);
893                                 first_RGA2_proc = 1;
894                         }
895                         else {
896                                 ret = rga2_blit_async(session, &req);
897                         }
898                         break;
899                 case RGA2_BLIT_SYNC:
900                         if (unlikely(copy_from_user(&req, (struct rga2_req*)arg, sizeof(struct rga2_req))))
901                         {
902                                 ERR("copy_from_user failed\n");
903                                 ret = -EFAULT;
904                                 break;
905                         }
906                         ret = rga2_blit_sync(session, &req);
907                         break;
908                 case RGA2_BLIT_ASYNC:
909                         if (unlikely(copy_from_user(&req, (struct rga2_req*)arg, sizeof(struct rga2_req))))
910                         {
911                                 ERR("copy_from_user failed\n");
912                                 ret = -EFAULT;
913                                 break;
914                         }
915
916                         if((atomic_read(&rga2_service.total_running) > 16))
917                         {
918                                 ret = rga2_blit_sync(session, &req);
919                         }
920                         else
921                         {
922                                 ret = rga2_blit_async(session, &req);
923                         }
924                         break;
925                 case RGA_FLUSH:
926                 case RGA2_FLUSH:
927                         ret = rga2_flush(session, arg);
928                         break;
929                 case RGA_GET_RESULT:
930                 case RGA2_GET_RESULT:
931                         ret = rga2_get_result(session, arg);
932                         break;
933                 case RGA_GET_VERSION:
934                 case RGA2_GET_VERSION:
935                         ret = copy_to_user((void *)arg, RGA2_VERSION, sizeof(RGA2_VERSION));
936                         //ret = 0;
937                         break;
938                 default:
939                         ERR("unknown ioctl cmd!\n");
940                         ret = -EINVAL;
941                         break;
942         }
943
944         mutex_unlock(&rga2_service.mutex);
945
946         return ret;
947 }
948
949 #ifdef CONFIG_COMPAT
950 static long compat_rga_ioctl(struct file *file, uint32_t cmd, unsigned long arg)
951 {
952         struct rga2_req req, req_first;
953         struct rga_req_32 req_rga;
954         int ret = 0;
955         rga2_session *session;
956
957         memset(&req, 0x0, sizeof(req));
958
959         mutex_lock(&rga2_service.mutex);
960
961         session = (rga2_session *)file->private_data;
962
963 #if RGA2_TEST_MSG
964         printk("use compat_rga_ioctl\n");
965 #endif
966
967         if (NULL == session) {
968                 printk("%s [%d] rga thread session is null\n",__FUNCTION__,__LINE__);
969                 mutex_unlock(&rga2_service.mutex);
970                 return -EINVAL;
971         }
972
973         memset(&req, 0x0, sizeof(req));
974
975         switch (cmd) {
976                 case RGA_BLIT_SYNC:
977                         if (unlikely(copy_from_user(&req_rga, compat_ptr((compat_uptr_t)arg), sizeof(struct rga_req_32))))
978                         {
979                                 ERR("copy_from_user failed\n");
980                                 ret = -EFAULT;
981                                 break;
982                         }
983
984                         RGA_MSG_2_RGA2_MSG_32(&req_rga, &req);
985
986                         if (first_RGA2_proc == 0 && req.bitblt_mode == bitblt_mode && rga2_service.dev_mode == 1) {
987                                 memcpy(&req_first, &req, sizeof(struct rga2_req));
988                                 if ((req_first.src.act_w != req_first.dst.act_w)
989                                                 || (req_first.src.act_h != req_first.dst.act_h)) {
990                                         req_first.src.act_w = MIN(320, MIN(req_first.src.act_w, req_first.dst.act_w));
991                                         req_first.src.act_h = MIN(240, MIN(req_first.src.act_h, req_first.dst.act_h));
992                                         req_first.dst.act_w = req_first.src.act_w;
993                                         req_first.dst.act_h = req_first.src.act_h;
994                                         ret = rga2_blit_async(session, &req_first);
995                                 }
996                                 ret = rga2_blit_sync(session, &req);
997                                 first_RGA2_proc = 1;
998                         }
999                         else {
1000                                 ret = rga2_blit_sync(session, &req);
1001                         }
1002                         break;
1003                 case RGA_BLIT_ASYNC:
1004                         if (unlikely(copy_from_user(&req_rga, compat_ptr((compat_uptr_t)arg), sizeof(struct rga_req_32))))
1005                         {
1006                                 ERR("copy_from_user failed\n");
1007                                 ret = -EFAULT;
1008                                 break;
1009                         }
1010                         RGA_MSG_2_RGA2_MSG_32(&req_rga, &req);
1011
1012                         if (first_RGA2_proc == 0 && req.bitblt_mode == bitblt_mode && rga2_service.dev_mode == 1) {
1013                                 memcpy(&req_first, &req, sizeof(struct rga2_req));
1014                                 if ((req_first.src.act_w != req_first.dst.act_w)
1015                                                 || (req_first.src.act_h != req_first.dst.act_h)) {
1016                                         req_first.src.act_w = MIN(320, MIN(req_first.src.act_w, req_first.dst.act_w));
1017                                         req_first.src.act_h = MIN(240, MIN(req_first.src.act_h, req_first.dst.act_h));
1018                                         req_first.dst.act_w = req_first.src.act_w;
1019                                         req_first.dst.act_h = req_first.src.act_h;
1020                                         ret = rga2_blit_async(session, &req_first);
1021                                 }
1022                                 ret = rga2_blit_sync(session, &req);
1023                                 first_RGA2_proc = 1;
1024                         }
1025                         else {
1026                                 ret = rga2_blit_sync(session, &req);
1027                         }
1028
1029                         //if((atomic_read(&rga2_service.total_running) > 8))
1030                         //    ret = rga2_blit_sync(session, &req);
1031                         //else
1032                         //    ret = rga2_blit_async(session, &req);
1033
1034                         break;
1035                 case RGA2_BLIT_SYNC:
1036                         if (unlikely(copy_from_user(&req, compat_ptr((compat_uptr_t)arg), sizeof(struct rga2_req))))
1037                         {
1038                                 ERR("copy_from_user failed\n");
1039                                 ret = -EFAULT;
1040                                 break;
1041                         }
1042                         ret = rga2_blit_sync(session, &req);
1043                         break;
1044                 case RGA2_BLIT_ASYNC:
1045                         if (unlikely(copy_from_user(&req, compat_ptr((compat_uptr_t)arg), sizeof(struct rga2_req))))
1046                         {
1047                                 ERR("copy_from_user failed\n");
1048                                 ret = -EFAULT;
1049                                 break;
1050                         }
1051
1052                         if((atomic_read(&rga2_service.total_running) > 16))
1053                                 ret = rga2_blit_sync(session, &req);
1054                         else
1055                                 ret = rga2_blit_async(session, &req);
1056
1057                         break;
1058                 case RGA_FLUSH:
1059                 case RGA2_FLUSH:
1060                         ret = rga2_flush(session, arg);
1061                         break;
1062                 case RGA_GET_RESULT:
1063                 case RGA2_GET_RESULT:
1064                         ret = rga2_get_result(session, arg);
1065                         break;
1066                 case RGA_GET_VERSION:
1067                 case RGA2_GET_VERSION:
1068                         ret = copy_to_user((void *)arg, RGA2_VERSION, sizeof(RGA2_VERSION));
1069                         //ret = 0;
1070                         break;
1071                 default:
1072                         ERR("unknown ioctl cmd!\n");
1073                         ret = -EINVAL;
1074                         break;
1075         }
1076
1077         mutex_unlock(&rga2_service.mutex);
1078
1079         return ret;
1080 }
1081 #endif
1082
1083
1084 long rga2_ioctl_kernel(struct rga_req *req_rga)
1085 {
1086         int ret = 0;
1087         rga2_session *session;
1088         struct rga2_req req;
1089
1090         memset(&req, 0x0, sizeof(req));
1091         mutex_lock(&rga2_service.mutex);
1092         session = &rga2_session_global;
1093         if (NULL == session)
1094         {
1095                 printk("%s [%d] rga thread session is null\n",__FUNCTION__,__LINE__);
1096                 mutex_unlock(&rga2_service.mutex);
1097                 return -EINVAL;
1098         }
1099
1100         RGA_MSG_2_RGA2_MSG(req_rga, &req);
1101         ret = rga2_blit_sync(session, &req);
1102         mutex_unlock(&rga2_service.mutex);
1103
1104         return ret;
1105 }
1106
1107
1108 static int rga2_open(struct inode *inode, struct file *file)
1109 {
1110         rga2_session *session = kzalloc(sizeof(rga2_session), GFP_KERNEL);
1111
1112         if (NULL == session) {
1113                 pr_err("unable to allocate memory for rga_session.");
1114                 return -ENOMEM;
1115         }
1116
1117         session->pid = current->pid;
1118         INIT_LIST_HEAD(&session->waiting);
1119         INIT_LIST_HEAD(&session->running);
1120         INIT_LIST_HEAD(&session->list_session);
1121         init_waitqueue_head(&session->wait);
1122         mutex_lock(&rga2_service.lock);
1123         list_add_tail(&session->list_session, &rga2_service.session);
1124         mutex_unlock(&rga2_service.lock);
1125         atomic_set(&session->task_running, 0);
1126         atomic_set(&session->num_done, 0);
1127         file->private_data = (void *)session;
1128
1129         return nonseekable_open(inode, file);
1130 }
1131
1132 static int rga2_release(struct inode *inode, struct file *file)
1133 {
1134         int task_running;
1135         rga2_session *session = (rga2_session *)file->private_data;
1136
1137         if (NULL == session)
1138                 return -EINVAL;
1139
1140         task_running = atomic_read(&session->task_running);
1141         if (task_running)
1142         {
1143                 pr_err("rga2_service session %d still has %d task running when closing\n", session->pid, task_running);
1144                 msleep(100);
1145         }
1146
1147         wake_up(&session->wait);
1148         mutex_lock(&rga2_service.lock);
1149         list_del(&session->list_session);
1150         rga2_service_session_clear(session);
1151         kfree(session);
1152         mutex_unlock(&rga2_service.lock);
1153
1154         return 0;
1155 }
1156
1157 static irqreturn_t rga2_irq_thread(int irq, void *dev_id)
1158 {
1159         mutex_lock(&rga2_service.lock);
1160         if (rga2_service.enable) {
1161                 rga2_del_running_list();
1162                 rga2_try_set_reg();
1163         }
1164         mutex_unlock(&rga2_service.lock);
1165
1166         return IRQ_HANDLED;
1167 }
1168
1169 static irqreturn_t rga2_irq(int irq,  void *dev_id)
1170 {
1171         /*clear INT */
1172         rga2_write(rga2_read(RGA2_INT) | (0x1<<4) | (0x1<<5) | (0x1<<6) | (0x1<<7), RGA2_INT);
1173
1174         return IRQ_WAKE_THREAD;
1175 }
1176
1177 struct file_operations rga2_fops = {
1178         .owner          = THIS_MODULE,
1179         .open           = rga2_open,
1180         .release        = rga2_release,
1181         .unlocked_ioctl         = rga_ioctl,
1182 #ifdef CONFIG_COMPAT
1183         .compat_ioctl           = compat_rga_ioctl,
1184 #endif
1185 };
1186
1187 static struct miscdevice rga2_dev ={
1188         .minor = RGA2_MAJOR,
1189         .name  = "rga",
1190         .fops  = &rga2_fops,
1191 };
1192
1193 static const struct of_device_id rockchip_rga_dt_ids[] = {
1194         { .compatible = "rockchip,rga2", },
1195         {},
1196 };
1197
1198 static int rga2_drv_probe(struct platform_device *pdev)
1199 {
1200         struct rga2_drvdata_t *data;
1201         struct resource *res;
1202         int ret = 0;
1203         struct device_node *np = pdev->dev.of_node;
1204
1205         mutex_init(&rga2_service.lock);
1206         mutex_init(&rga2_service.mutex);
1207         atomic_set(&rga2_service.total_running, 0);
1208         atomic_set(&rga2_service.src_format_swt, 0);
1209         rga2_service.last_prc_src_format = 1; /* default is yuv first*/
1210         rga2_service.enable = false;
1211
1212         rga_ioctl_kernel_p = rga2_ioctl_kernel;
1213
1214         data = devm_kzalloc(&pdev->dev, sizeof(struct rga2_drvdata_t), GFP_KERNEL);
1215         if(NULL == data)
1216         {
1217                 ERR("failed to allocate driver data.\n");
1218                 return -ENOMEM;
1219         }
1220
1221         INIT_DELAYED_WORK(&data->power_off_work, rga2_power_off_work);
1222         wake_lock_init(&data->wake_lock, WAKE_LOCK_SUSPEND, "rga");
1223
1224         data->rga2 = devm_clk_get(&pdev->dev, "clk_rga");
1225         data->aclk_rga2 = devm_clk_get(&pdev->dev, "aclk_rga");
1226         data->hclk_rga2 = devm_clk_get(&pdev->dev, "hclk_rga");
1227
1228         /* map the registers */
1229         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1230         data->rga_base = devm_ioremap_resource(&pdev->dev, res);
1231         if (!data->rga_base) {
1232                 ERR("rga ioremap failed\n");
1233                 ret = -ENOENT;
1234                 goto err_ioremap;
1235         }
1236
1237         /* get the IRQ */
1238         data->irq = platform_get_irq(pdev, 0);
1239         if (data->irq <= 0) {
1240                 ERR("failed to get rga irq resource (%d).\n", data->irq);
1241                 ret = data->irq;
1242                 goto err_irq;
1243         }
1244
1245         /* request the IRQ */
1246         ret = devm_request_threaded_irq(&pdev->dev, data->irq, rga2_irq, rga2_irq_thread, 0, "rga", pdev);
1247         if (ret)
1248         {
1249                 ERR("rga request_irq failed (%d).\n", ret);
1250                 goto err_irq;
1251         }
1252
1253         platform_set_drvdata(pdev, data);
1254         data->dev = &pdev->dev;
1255         rga2_drvdata = data;
1256         of_property_read_u32(np, "dev_mode", &rga2_service.dev_mode);
1257
1258 #if defined(CONFIG_ION_ROCKCHIP)
1259         data->ion_client = rockchip_ion_client_create("rga");
1260         if (IS_ERR(data->ion_client)) {
1261                 dev_err(&pdev->dev, "failed to create ion client for rga");
1262                 return PTR_ERR(data->ion_client);
1263         } else {
1264                 dev_info(&pdev->dev, "rga ion client create success!\n");
1265         }
1266 #endif
1267
1268         ret = misc_register(&rga2_dev);
1269         if(ret)
1270         {
1271                 ERR("cannot register miscdev (%d)\n", ret);
1272                 goto err_misc_register;
1273         }
1274
1275 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0))
1276         pm_runtime_enable(&pdev->dev);
1277 #endif
1278
1279         pr_info("Driver loaded succesfully\n");
1280
1281         return 0;
1282
1283 err_misc_register:
1284         free_irq(data->irq, pdev);
1285 err_irq:
1286         iounmap(data->rga_base);
1287 err_ioremap:
1288         wake_lock_destroy(&data->wake_lock);
1289         //kfree(data);
1290
1291         return ret;
1292 }
1293
1294 static int rga2_drv_remove(struct platform_device *pdev)
1295 {
1296         struct rga2_drvdata_t *data = platform_get_drvdata(pdev);
1297         DBG("%s [%d]\n",__FUNCTION__,__LINE__);
1298
1299         wake_lock_destroy(&data->wake_lock);
1300         misc_deregister(&(data->miscdev));
1301         free_irq(data->irq, &data->miscdev);
1302         iounmap((void __iomem *)(data->rga_base));
1303
1304         devm_clk_put(&pdev->dev, data->rga2);
1305         devm_clk_put(&pdev->dev, data->aclk_rga2);
1306         devm_clk_put(&pdev->dev, data->hclk_rga2);
1307
1308 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0))
1309         pm_runtime_disable(&pdev->dev);
1310 #endif
1311
1312         kfree(data);
1313         return 0;
1314 }
1315
1316 static struct platform_driver rga2_driver = {
1317         .probe          = rga2_drv_probe,
1318         .remove         = rga2_drv_remove,
1319         .driver         = {
1320                 .owner  = THIS_MODULE,
1321                 .name   = "rga2",
1322                 .of_match_table = of_match_ptr(rockchip_rga_dt_ids),
1323         },
1324 };
1325
1326
1327 void rga2_test_0(void);
1328
1329 static int __init rga2_init(void)
1330 {
1331         int ret;
1332         uint32_t *buf_p;
1333
1334         /* malloc pre scale mid buf mmu table */
1335         buf_p = kmalloc(1024*256, GFP_KERNEL);
1336         rga2_mmu_buf.buf_virtual = buf_p;
1337         rga2_mmu_buf.buf = (uint32_t *)virt_to_phys((void *)((unsigned long)buf_p));
1338         rga2_mmu_buf.front = 0;
1339         rga2_mmu_buf.back = 64*1024;
1340         rga2_mmu_buf.size = 64*1024;
1341
1342         rga2_mmu_buf.pages = kmalloc(32768 * sizeof(struct page *), GFP_KERNEL);
1343
1344         ret = platform_driver_register(&rga2_driver);
1345         if (ret != 0) {
1346                 printk(KERN_ERR "Platform device register failed (%d).\n", ret);
1347                 return ret;
1348         }
1349
1350         rga2_session_global.pid = 0x0000ffff;
1351         INIT_LIST_HEAD(&rga2_session_global.waiting);
1352         INIT_LIST_HEAD(&rga2_session_global.running);
1353         INIT_LIST_HEAD(&rga2_session_global.list_session);
1354
1355         INIT_LIST_HEAD(&rga2_service.waiting);
1356         INIT_LIST_HEAD(&rga2_service.running);
1357         INIT_LIST_HEAD(&rga2_service.done);
1358         INIT_LIST_HEAD(&rga2_service.session);
1359         init_waitqueue_head(&rga2_session_global.wait);
1360         //mutex_lock(&rga_service.lock);
1361         list_add_tail(&rga2_session_global.list_session, &rga2_service.session);
1362         //mutex_unlock(&rga_service.lock);
1363         atomic_set(&rga2_session_global.task_running, 0);
1364         atomic_set(&rga2_session_global.num_done, 0);
1365
1366 #if RGA2_TEST_CASE
1367         rga2_test_0();
1368 #endif
1369
1370         INFO("Module initialized.\n");
1371
1372         return 0;
1373 }
1374
1375 static void __exit rga2_exit(void)
1376 {
1377         rga2_power_off();
1378
1379         if (rga2_mmu_buf.buf_virtual)
1380                 kfree(rga2_mmu_buf.buf_virtual);
1381
1382         platform_driver_unregister(&rga2_driver);
1383 }
1384
1385
1386 #if RGA2_TEST_CASE
1387
1388 void rga2_test_0(void)
1389 {
1390         struct rga2_req req;
1391         rga2_session session;
1392         unsigned int *src, *dst;
1393
1394         session.pid     = current->pid;
1395         INIT_LIST_HEAD(&session.waiting);
1396         INIT_LIST_HEAD(&session.running);
1397         INIT_LIST_HEAD(&session.list_session);
1398         init_waitqueue_head(&session.wait);
1399         /* no need to protect */
1400         list_add_tail(&session.list_session, &rga2_service.session);
1401         atomic_set(&session.task_running, 0);
1402         atomic_set(&session.num_done, 0);
1403
1404         memset(&req, 0, sizeof(struct rga2_req));
1405         src = kmalloc(800*480*4, GFP_KERNEL);
1406         dst = kmalloc(800*480*4, GFP_KERNEL);
1407
1408         printk("\n********************************\n");
1409         printk("************ RGA2_TEST ************\n");
1410         printk("********************************\n\n");
1411
1412 #if 1
1413         memset(src, 0x80, 800 * 480 * 4);
1414         memset(dst, 0xcc, 800 * 480 * 4);
1415 #endif
1416 #if 0
1417         dmac_flush_range(src, &src[800 * 480]);
1418         outer_flush_range(virt_to_phys(src), virt_to_phys(&src[800 * 480]));
1419
1420         dmac_flush_range(dst, &dst[800 * 480]);
1421         outer_flush_range(virt_to_phys(dst), virt_to_phys(&dst[800 * 480]));
1422 #endif
1423
1424 #if 0
1425         req.pat.act_w = 16;
1426         req.pat.act_h = 16;
1427         req.pat.vir_w = 16;
1428         req.pat.vir_h = 16;
1429         req.pat.yrgb_addr = virt_to_phys(src);
1430         req.render_mode = 0;
1431         rga2_blit_sync(&session, &req);
1432 #endif
1433         {
1434                 uint32_t i, j;
1435                 uint8_t *sp;
1436
1437                 sp = (uint8_t *)src;
1438                 for (j = 0; j < 240; j++) {
1439                         sp = (uint8_t *)src + j * 320 * 10 / 8;
1440                         for (i = 0; i < 320; i++) {
1441                                 if ((i & 3) == 0) {
1442                                         sp[i * 5 / 4] = 0;
1443                                         sp[i * 5 / 4+1] = 0x1;
1444                                 } else if ((i & 3) == 1) {
1445                                         sp[i * 5 / 4+1] = 0x4;
1446                                 } else if ((i & 3) == 2) {
1447                                         sp[i * 5 / 4+1] = 0x10;
1448                                 } else if ((i & 3) == 3) {
1449                                         sp[i * 5 / 4+1] = 0x40;
1450                             }
1451                         }
1452                 }
1453                 sp = (uint8_t *)src;
1454                 for (j = 0; j < 100; j++)
1455                         printk("src %.2x\n", sp[j]);
1456         }
1457         req.src.act_w = 320;
1458         req.src.act_h = 240;
1459
1460         req.src.vir_w = 320;
1461         req.src.vir_h = 240;
1462         req.src.yrgb_addr = 0;//(uint32_t)virt_to_phys(src);
1463         req.src.uv_addr = (unsigned long)virt_to_phys(src);
1464         req.src.v_addr = 0;
1465         req.src.format = RGA2_FORMAT_YCbCr_420_SP_10B;
1466
1467         req.dst.act_w  = 320;
1468         req.dst.act_h = 240;
1469         req.dst.x_offset = 0;
1470         req.dst.y_offset = 0;
1471
1472         req.dst.vir_w = 320;
1473         req.dst.vir_h = 240;
1474
1475         req.dst.yrgb_addr = 0;//((uint32_t)virt_to_phys(dst));
1476         req.dst.uv_addr = (unsigned long)virt_to_phys(dst);
1477         req.dst.format = RGA2_FORMAT_YCbCr_420_SP;
1478
1479         //dst = dst0;
1480
1481         //req.render_mode = color_fill_mode;
1482         //req.fg_color = 0x80ffffff;
1483
1484         req.rotate_mode = 0;
1485         req.scale_bicu_mode = 2;
1486
1487 #if 0
1488         //req.alpha_rop_flag = 0;
1489         //req.alpha_rop_mode = 0x19;
1490         //req.PD_mode = 3;
1491
1492         //req.mmu_info.mmu_flag = 0x21;
1493         //req.mmu_info.mmu_en = 1;
1494
1495         //printk("src = %.8x\n", req.src.yrgb_addr);
1496         //printk("src = %.8x\n", req.src.uv_addr);
1497         //printk("dst = %.8x\n", req.dst.yrgb_addr);
1498 #endif
1499
1500         rga2_blit_sync(&session, &req);
1501
1502 #if 0
1503         uint32_t j;
1504         for (j = 0; j < 320 * 240 * 10 / 8; j++) {
1505         if (src[j] != dst[j])
1506                 printk("error value dst not equal src j %d, s %.2x d %.2x\n",
1507                         j, src[j], dst[j]);
1508         }
1509 #endif
1510
1511 #if 1
1512         {
1513                 uint32_t j;
1514                 uint8_t *dp = (uint8_t *)dst;
1515
1516                 for (j = 0; j < 100; j++)
1517                         printk("%d %.2x\n", j, dp[j]);
1518         }
1519 #endif
1520
1521         if(src)
1522                 kfree(src);
1523         if(dst)
1524                 kfree(dst);
1525 }
1526 #endif
1527
1528 module_init(rga2_init);
1529 module_exit(rga2_exit);
1530
1531 /* Module information */
1532 MODULE_AUTHOR("zsq@rock-chips.com");
1533 MODULE_DESCRIPTION("Driver for rga device");
1534 MODULE_LICENSE("GPL");