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