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