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