rk29: vpu_service: fix bug when isr timeout and taskrunning count error
[firefly-linux-kernel-4.4.55.git] / arch / arm / mach-rk29 / vpu_service.c
1 /* arch/arm/mach-rk29/vpu.c
2  *
3  * Copyright (C) 2010 ROCKCHIP, Inc.
4  *
5  * This software is licensed under the terms of the GNU General Public
6  * License version 2, as published by the Free Software Foundation, and
7  * may be copied, distributed, and modified under those terms.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  */
15
16 #ifdef CONFIG_RK29_VPU_DEBUG
17 #define DEBUG
18 #define pr_fmt(fmt) "VPU_SERVICE: %s: " fmt, __func__
19 #else
20 #define pr_fmt(fmt) "VPU_SERVICE: " fmt
21 #endif
22
23
24 #include <linux/clk.h>
25 #include <linux/delay.h>
26 #include <linux/init.h>
27 #include <linux/interrupt.h>
28 #include <linux/io.h>
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/fs.h>
32 #include <linux/ioport.h>
33 #include <linux/miscdevice.h>
34 #include <linux/mm.h>
35 #include <linux/poll.h>
36 #include <linux/platform_device.h>
37 #include <linux/timer.h>
38
39 #include <asm/uaccess.h>
40
41 #include <mach/irqs.h>
42 #include <mach/vpu_service.h>
43 #include <mach/rk29_iomap.h>
44 #include <mach/pmu.h>
45 #include <mach/cru.h>
46
47
48 #define DEC_INTERRUPT_REGISTER                  1
49 #define PP_INTERRUPT_REGISTER                   60
50 #define ENC_INTERRUPT_REGISTER                  1
51
52 #define DEC_INTERRUPT_BIT                        0x100
53 #define PP_INTERRUPT_BIT                         0x100
54 #define ENC_INTERRUPT_BIT                        0x1
55
56 #define REG_NUM_DEC                             (60)
57 #define REG_NUM_PP                              (41)
58 #define REG_NUM_ENC                             (96)
59 #define REG_NUM_DEC_PP                          (REG_NUM_DEC+REG_NUM_PP)
60 #define SIZE_REG(reg)                           ((reg)*4)
61
62 #define DEC_IO_SIZE                             ((100 + 1) * 4) /* bytes */
63 #define ENC_IO_SIZE                             (96 * 4)        /* bytes */
64 static const u16 dec_hw_ids[] = { 0x8190, 0x8170, 0x9170, 0x9190, 0x6731 };
65 static const u16 enc_hw_ids[] = { 0x6280, 0x7280, 0x8270 };
66
67 #define VPU_REG_EN_ENC                          14
68 #define VPU_REG_ENC_GATE                        2
69 #define VPU_REG_ENC_GATE_BIT                    (1<<4)
70
71 #define VPU_REG_EN_DEC                          1
72 #define VPU_REG_DEC_GATE                        2
73 #define VPU_REG_DEC_GATE_BIT                    (1<<10)
74 #define VPU_REG_EN_PP                           0
75 #define VPU_REG_PP_GATE                         1
76 #define VPU_REG_PP_GATE_BIT                     (1<<8)
77 #define VPU_REG_EN_DEC_PP                       1
78 #define VPU_REG_DEC_PP_GATE                     61
79 #define VPU_REG_DEC_PP_GATE_BIT                 (1<<8)
80
81
82 /**
83  * struct for process session which connect to vpu
84  *
85  * @author ChenHengming (2011-5-3)
86  */
87 typedef struct vpu_session {
88         VPU_CLIENT_TYPE         type;
89         /* a linked list of data so we can access them for debugging */
90         struct list_head        list_session;
91         /* a linked list of register data waiting for process */
92         struct list_head        waiting;
93         /* a linked list of register data in processing */
94         struct list_head        running;
95         /* a linked list of register data processed */
96         struct list_head        done;
97         wait_queue_head_t       wait;
98         pid_t                   pid;
99 } vpu_session;
100
101 /**
102  * struct for process register set
103  *
104  * @author ChenHengming (2011-5-4)
105  */
106 typedef struct vpu_reg {
107         VPU_CLIENT_TYPE         type;
108         vpu_session             *session;
109         struct list_head        session_link;           /* link to vpu service session */
110         struct list_head        status_link;            /* link to register set list */
111         unsigned long           size;
112         unsigned long           reg[VPU_REG_NUM_DEC_PP];
113 } vpu_reg;
114
115 typedef struct vpu_device {
116         unsigned long           iobaseaddr;
117         unsigned int            iosize;
118         volatile u32            *hwregs;
119 } vpu_device;
120
121 typedef struct vpu_service_info {
122         spinlock_t              lock;
123         struct timer_list       timer;                  /* timer for power off */
124         struct list_head        waiting;                /* link to link_reg in struct vpu_reg */
125         struct list_head        running;                /* link to link_reg in struct vpu_reg */
126         struct list_head        done;                   /* link to link_reg in struct vpu_reg */
127         struct list_head        session;                /* link to list_session in struct vpu_session */
128         atomic_t                task_running;
129         bool                    enabled;
130         vpu_reg                 *reg_codec;
131         vpu_reg                 *reg_pproc;
132         vpu_reg                 *reg_resev;
133         VPUHwDecConfig_t        dec_config;
134         VPUHwEncConfig_t        enc_config;
135 } vpu_service_info;
136
137 typedef struct vpu_request
138 {
139         unsigned long   *req;
140         unsigned long   size;
141 } vpu_request;
142
143 static struct clk *aclk_vepu;
144 static struct clk *hclk_vepu;
145 static struct clk *aclk_ddr_vepu;
146 static struct clk *hclk_cpu_vcodec;
147 static vpu_service_info service;
148 static vpu_device       dec_dev;
149 static vpu_device       enc_dev;
150
151 #define POWER_OFF_DELAY 4*HZ /* 4s */
152 #define TIMEOUT_DELAY   2*HZ /* 2s */
153
154 static void vpu_get_clk(void)
155 {
156         aclk_vepu       = clk_get(NULL, "aclk_vepu");
157         hclk_vepu       = clk_get(NULL, "hclk_vepu");
158         aclk_ddr_vepu   = clk_get(NULL, "aclk_ddr_vepu");
159         hclk_cpu_vcodec = clk_get(NULL, "hclk_cpu_vcodec");
160 }
161
162 static void vpu_put_clk(void)
163 {
164         clk_put(aclk_vepu);
165         clk_put(hclk_vepu);
166         clk_put(aclk_ddr_vepu);
167         clk_put(hclk_cpu_vcodec);
168 }
169
170 static void vpu_service_power_off(void)
171 {
172         if (!service.enabled)
173                 return;
174
175         service.enabled = false;
176         printk("vpu: power off\n");
177
178         while(atomic_read(&service.task_running)) {
179                 pr_alert("power off when task running!!\n");
180                 udelay(10);
181         }
182
183         pmu_set_power_domain(PD_VCODEC, false);
184         udelay(10);
185         clk_disable(hclk_cpu_vcodec);
186         clk_disable(aclk_ddr_vepu);
187         clk_disable(hclk_vepu);
188         clk_disable(aclk_vepu);
189 }
190
191 static void vpu_service_power_off_work_func(unsigned long data)
192 {
193         printk("vpu: delayed power off work\n");
194         vpu_service_power_off();
195 }
196
197 static void vpu_service_power_on(void)
198 {
199         if (service.enabled) {
200                 mod_timer(&service.timer, jiffies + POWER_OFF_DELAY);
201                 return;
202         }
203         service.enabled = true;
204         printk("vpu: power on\n");
205
206         clk_enable(aclk_vepu);
207         clk_enable(hclk_vepu);
208         clk_enable(hclk_cpu_vcodec);
209         udelay(10);
210         pmu_set_power_domain(PD_VCODEC, true);
211         udelay(10);
212         clk_enable(aclk_ddr_vepu);
213         init_timer(&service.timer);
214         service.timer.expires = jiffies + POWER_OFF_DELAY;
215         service.timer.function = vpu_service_power_off_work_func;
216         add_timer(&service.timer);
217 }
218
219 static vpu_reg *reg_init(vpu_session *session, void __user *src, unsigned long size)
220 {
221         unsigned long flag;
222         vpu_reg *reg = kmalloc(sizeof(vpu_reg), GFP_KERNEL);
223         if (NULL == reg) {
224                 pr_err("kmalloc fail in reg_init\n");
225                 return NULL;
226         }
227
228         reg->session = session;
229         reg->type = session->type;
230         reg->size = size;
231         INIT_LIST_HEAD(&reg->session_link);
232         INIT_LIST_HEAD(&reg->status_link);
233
234         if (copy_from_user(&reg->reg[0], (void __user *)src, size)) {
235                 pr_err("copy_from_user failed in reg_init\n");
236                 kfree(reg);
237                 return NULL;
238         }
239
240         spin_lock_irqsave(&service.lock, flag);
241         list_add_tail(&reg->status_link, &service.waiting);
242         list_add_tail(&reg->session_link, &session->waiting);
243         spin_unlock_irqrestore(&service.lock, flag);
244
245         return reg;
246 }
247
248 static void reg_deinit(vpu_reg *reg)
249 {
250         list_del_init(&reg->session_link);
251         list_del_init(&reg->status_link);
252         kfree(reg);
253         if (reg == service.reg_codec) service.reg_codec = NULL;
254         if (reg == service.reg_pproc) service.reg_pproc = NULL;
255 }
256
257 static void reg_from_wait_to_run(vpu_reg *reg)
258 {
259         list_del_init(&reg->status_link);
260         list_add_tail(&reg->status_link, &service.running);
261
262         list_del_init(&reg->session_link);
263         list_add_tail(&reg->session_link, &reg->session->running);
264 }
265
266 static void reg_copy_from_hw(vpu_reg *reg, volatile u32 *src, u32 count)
267 {
268         int i;
269         u32 *dst = (u32 *)&reg->reg[0];
270         for (i = 0; i < count; i++)
271                 *dst++ = *src++;
272 }
273
274 static void reg_from_run_to_done(vpu_reg *reg)
275 {
276         spin_lock(&service.lock);
277         list_del_init(&reg->status_link);
278         list_add_tail(&reg->status_link, &service.done);
279
280         list_del_init(&reg->session_link);
281         list_add_tail(&reg->session_link, &reg->session->done);
282
283         switch (reg->type) {
284         case VPU_ENC : {
285                 service.reg_codec = NULL;
286                 reg_copy_from_hw(reg, enc_dev.hwregs, REG_NUM_ENC);
287                 break;
288         }
289         case VPU_DEC : {
290                 service.reg_codec = NULL;
291                 reg_copy_from_hw(reg, dec_dev.hwregs, REG_NUM_DEC);
292                 break;
293         }
294         case VPU_PP : {
295                 service.reg_pproc = NULL;
296                 reg_copy_from_hw(reg, dec_dev.hwregs + PP_INTERRUPT_REGISTER, REG_NUM_PP);
297                 dec_dev.hwregs[PP_INTERRUPT_REGISTER] = 0;
298                 break;
299         }
300         case VPU_DEC_PP : {
301                 service.reg_codec = NULL;
302                 service.reg_pproc = NULL;
303                 reg_copy_from_hw(reg, dec_dev.hwregs, REG_NUM_DEC_PP);
304                 dec_dev.hwregs[PP_INTERRUPT_REGISTER] = 0;
305                 break;
306         }
307         default : {
308                 pr_err("copy reg from hw with unknown type %d\n", reg->type);
309                 break;
310         }
311         }
312         wake_up_interruptible_sync(&reg->session->wait);
313         spin_unlock(&service.lock);
314 }
315
316 void reg_copy_to_hw(vpu_reg *reg)
317 {
318         int i;
319         u32 *src = (u32 *)&reg->reg[0];
320         atomic_add(1, &service.task_running);
321         switch (reg->type) {
322         case VPU_ENC : {
323                 u32 *dst = (u32 *)enc_dev.hwregs;
324                 service.reg_codec = reg;
325
326                 dst[VPU_REG_EN_ENC] = src[VPU_REG_EN_ENC] & 0x6;
327
328                 for (i = 0; i < VPU_REG_EN_ENC; i++)
329                         dst[i] = src[i];
330
331                 for (i = VPU_REG_EN_ENC + 1; i < REG_NUM_ENC; i++)
332                         dst[i] = src[i];
333
334                 dsb();
335
336                 dst[VPU_REG_ENC_GATE] = src[VPU_REG_ENC_GATE] | VPU_REG_ENC_GATE_BIT;
337                 dst[VPU_REG_EN_ENC]   = src[VPU_REG_EN_ENC];
338         } break;
339         case VPU_DEC : {
340                 u32 *dst = (u32 *)dec_dev.hwregs;
341                 service.reg_codec = reg;
342
343                 for (i = REG_NUM_DEC - 1; i > VPU_REG_DEC_GATE; i--)
344                         dst[i] = src[i];
345
346                 dsb();
347
348                 dst[VPU_REG_DEC_GATE] = src[VPU_REG_DEC_GATE] | VPU_REG_DEC_GATE_BIT;
349                 dst[VPU_REG_EN_DEC]   = src[VPU_REG_EN_DEC];
350         } break;
351         case VPU_PP : {
352                 u32 *dst = (u32 *)dec_dev.hwregs + PP_INTERRUPT_REGISTER;
353                 service.reg_pproc = reg;
354
355                 dst[VPU_REG_PP_GATE] = src[VPU_REG_PP_GATE] | VPU_REG_PP_GATE_BIT;
356
357                 for (i = VPU_REG_PP_GATE + 1; i < REG_NUM_PP; i++)
358                         dst[i] = src[i];
359
360                 dsb();
361
362                 dst[VPU_REG_EN_PP] = src[VPU_REG_EN_PP];
363         } break;
364         case VPU_DEC_PP : {
365                 u32 *dst = (u32 *)dec_dev.hwregs;
366                 service.reg_codec = reg;
367                 service.reg_pproc = reg;
368
369                 for (i = VPU_REG_EN_DEC_PP + 1; i < REG_NUM_DEC_PP; i++)
370                         dst[i] = src[i];
371
372                 dst[VPU_REG_EN_DEC_PP]   = src[VPU_REG_EN_DEC_PP] | 0x2;
373                 dsb();
374
375                 dst[VPU_REG_DEC_PP_GATE] = src[VPU_REG_DEC_PP_GATE] | VPU_REG_PP_GATE_BIT;
376                 dst[VPU_REG_DEC_GATE]    = src[VPU_REG_DEC_GATE]    | VPU_REG_DEC_GATE_BIT;
377                 dst[VPU_REG_EN_DEC]      = src[VPU_REG_EN_DEC];
378         } break;
379         default : {
380                 pr_err("unsupport session type %d", reg->type);
381                 atomic_sub(1, &service.task_running);
382                 break;
383         }
384         }
385 }
386
387 static void try_set_reg(void)
388 {
389         unsigned long flag;
390         // first get reg from reg list
391         spin_lock_irqsave(&service.lock, flag);
392         if (!list_empty(&service.waiting)) {
393                 vpu_reg *reg = list_entry(service.waiting.next, vpu_reg, status_link);
394
395                 if (((VPU_DEC_PP == reg->type) && (NULL == service.reg_codec) && (NULL == service.reg_pproc)) ||
396                     ((VPU_DEC == reg->type) && (NULL == service.reg_codec)) ||
397                     ((VPU_PP  == reg->type) && (NULL == service.reg_pproc)) ||
398                     ((VPU_ENC == reg->type) && (NULL == service.reg_codec))) {
399                         reg_from_wait_to_run(reg);
400                         vpu_service_power_on();
401                         reg_copy_to_hw(reg);
402                 }
403         }
404         spin_unlock_irqrestore(&service.lock, flag);
405 }
406
407 static int return_reg(vpu_reg *reg, u32 __user *dst)
408 {
409         int ret = 0;
410         switch (reg->type) {
411         case VPU_ENC : {
412                 if (copy_to_user(dst, &reg->reg[0], SIZE_REG(REG_NUM_ENC)))
413                         ret = -EFAULT;
414                 break;
415         }
416         case VPU_DEC : {
417                 if (copy_to_user(dst, &reg->reg[0], SIZE_REG(REG_NUM_DEC)))
418                         ret = -EFAULT;
419                 break;
420         }
421         case VPU_PP : {
422                 if (copy_to_user(dst, &reg->reg[0], SIZE_REG(REG_NUM_PP)))
423                         ret = -EFAULT;
424                 break;
425         }
426         case VPU_DEC_PP : {
427                 if (copy_to_user(dst, &reg->reg[0], SIZE_REG(REG_NUM_DEC_PP)))
428                         ret = -EFAULT;
429                 break;
430         }
431         default : {
432                 ret = -EFAULT;
433                 pr_err("copy reg to user with unknown type %d\n", reg->type);
434                 break;
435         }
436         }
437         reg_deinit(reg);
438         return ret;
439 }
440
441 static long vpu_service_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
442 {
443         vpu_session *session = (vpu_session *)filp->private_data;
444         if (NULL == session) {
445                 return -EINVAL;
446         }
447
448         switch (cmd) {
449         case VPU_IOC_SET_CLIENT_TYPE : {
450         session->type = (VPU_CLIENT_TYPE)arg;
451                 break;
452         }
453         case VPU_IOC_GET_HW_FUSE_STATUS : {
454                 vpu_request req;
455                 if (copy_from_user(&req, (void __user *)arg, sizeof(vpu_request))) {
456                         pr_err("VPU_IOC_GET_HW_FUSE_STATUS copy_from_user failed\n");
457                         return -EFAULT;
458                 } else {
459                         if (VPU_ENC != session->type) {
460                                 if (copy_to_user((void __user *)req.req, &service.dec_config, sizeof(VPUHwDecConfig_t))) {
461                                         pr_err("VPU_IOC_GET_HW_FUSE_STATUS copy_to_user failed type %d\n", session->type);
462                                         return -EFAULT;
463                                 }
464                         } else {
465                                 if (copy_to_user((void __user *)req.req, &service.enc_config, sizeof(VPUHwEncConfig_t))) {
466                                         pr_err("VPU_IOC_GET_HW_FUSE_STATUS copy_to_user failed type %d\n", session->type);
467                                         return -EFAULT;
468                                 }
469                         }
470                 }
471
472                 break;
473         }
474         case VPU_IOC_SET_REG : {
475                 vpu_request req;
476                 vpu_reg *reg;
477                 if (copy_from_user(&req, (void __user *)arg, sizeof(vpu_request))) {
478                         pr_err("VPU_IOC_SET_REG copy_from_user failed\n");
479                         return -EFAULT;
480                 }
481
482                 reg = reg_init(session, (void __user *)req.req, req.size);
483                 if (NULL == reg) {
484                         return -EFAULT;
485                 } else {
486                         try_set_reg();
487                 }
488
489                 break;
490         }
491         case VPU_IOC_GET_REG : {
492                 vpu_request req;
493                 vpu_reg *reg;
494                 if (copy_from_user(&req, (void __user *)arg, sizeof(vpu_request))) {
495                         pr_err("VPU_IOC_GET_REG copy_from_user failed\n");
496                         return -EFAULT;
497                 } else {
498                         int ret = wait_event_interruptible_timeout(session->wait, !list_empty(&session->done), TIMEOUT_DELAY);
499                         if (unlikely(ret < 0)) {
500                                 pr_err("pid %d wait task ret %d\n", session->pid, ret);
501                                 return ret;
502                         } else if (0 == ret) {
503                                 pr_err("pid %d wait task done timeout\n", session->pid);
504                                 return -ETIMEDOUT;
505                         }
506                 }
507                 {
508                         unsigned long flag;
509                         spin_lock_irqsave(&service.lock, flag);
510                         reg = list_entry(session->done.next, vpu_reg, session_link);
511                         return_reg(reg, (u32 __user *)req.req);
512                         spin_unlock_irqrestore(&service.lock, flag);
513                 }
514                 break;
515         }
516         default : {
517                 pr_err("unknow vpu service ioctl cmd %x\n", cmd);
518                 break;
519         }
520         }
521
522         return 0;
523 }
524
525 static int vpu_service_check_hw_id(struct vpu_device * dev, const u16 *hwids, size_t num)
526 {
527         u32 hwid = readl(dev->hwregs);
528         pr_info("HW ID = 0x%08x\n", hwid);
529
530         hwid = (hwid >> 16) & 0xFFFF;   /* product version only */
531
532         while (num--) {
533                 if (hwid == hwids[num]) {
534                         pr_info("Compatible HW found at 0x%08lx\n", dev->iobaseaddr);
535                         return 1;
536                 }
537         }
538
539         pr_info("No Compatible HW found at 0x%08lx\n", dev->iobaseaddr);
540         return 0;
541 }
542
543 static void vpu_service_release_io(void)
544 {
545         if (dec_dev.hwregs)
546                 iounmap((void *)dec_dev.hwregs);
547         release_mem_region(dec_dev.iobaseaddr, dec_dev.iosize);
548
549         if (enc_dev.hwregs)
550                 iounmap((void *)enc_dev.hwregs);
551         release_mem_region(enc_dev.iobaseaddr, enc_dev.iosize);
552 }
553
554 static int vpu_service_reserve_io(void)
555 {
556         unsigned long iobaseaddr;
557         unsigned long iosize;
558
559         iobaseaddr      = dec_dev.iobaseaddr;
560         iosize          = dec_dev.iosize;
561
562         if (!request_mem_region(iobaseaddr, iosize, "vdpu_io")) {
563                 pr_info("failed to reserve dec HW regs\n");
564                 return -EBUSY;
565         }
566
567         dec_dev.hwregs = (volatile u32 *)ioremap_nocache(iobaseaddr, iosize);
568
569         if (dec_dev.hwregs == NULL) {
570                 pr_info("failed to ioremap dec HW regs\n");
571                 goto err;
572         }
573
574         /* check for correct HW */
575         if (!vpu_service_check_hw_id(&dec_dev, dec_hw_ids, ARRAY_SIZE(dec_hw_ids))) {
576                 goto err;
577         }
578
579         iobaseaddr      = enc_dev.iobaseaddr;
580         iosize          = enc_dev.iosize;
581
582         if (!request_mem_region(iobaseaddr, iosize, "hx280enc")) {
583                 pr_info("failed to reserve enc HW regs\n");
584                 goto err;
585         }
586
587         enc_dev.hwregs = (volatile u32 *)ioremap_nocache(iobaseaddr, iosize);
588
589         if (enc_dev.hwregs == NULL) {
590                 pr_info("failed to ioremap enc HW regs\n");
591                 goto err;
592         }
593
594         /* check for correct HW */
595         if (!vpu_service_check_hw_id(&enc_dev, enc_hw_ids, ARRAY_SIZE(enc_hw_ids))) {
596                 goto err;
597         }
598         return 0;
599
600 err:
601         vpu_service_release_io();
602         return -EBUSY;
603 }
604
605 static int vpu_service_open(struct inode *inode, struct file *filp)
606 {
607         vpu_session *session = (vpu_session *)kmalloc(sizeof(vpu_session), GFP_KERNEL);
608         if (NULL == session) {
609                 pr_err("unable to allocate memory for vpu_session.");
610                 return -ENOMEM;
611         }
612
613         session->type   = VPU_TYPE_BUTT;
614         session->pid    = current->pid;
615         INIT_LIST_HEAD(&session->waiting);
616         INIT_LIST_HEAD(&session->running);
617         INIT_LIST_HEAD(&session->done);
618         INIT_LIST_HEAD(&session->list_session);
619         init_waitqueue_head(&session->wait);
620         /* no need to protect */
621         list_add_tail(&session->list_session, &service.session);
622         filp->private_data = (void *)session;
623
624         pr_debug("dev opened\n");
625         return nonseekable_open(inode, filp);
626 }
627
628 static int vpu_service_release(struct inode *inode, struct file *filp)
629 {
630         unsigned long flag;
631         vpu_session *session = (vpu_session *)filp->private_data;
632         if (NULL == session)
633                 return -EINVAL;
634
635         wake_up_interruptible_sync(&session->wait);
636
637         msleep(50);
638         /* remove this filp from the asynchronusly notified filp's */
639         //vpu_service_fasync(-1, filp, 0);
640         list_del(&session->list_session);
641
642         spin_lock_irqsave(&service.lock, flag);
643         {
644         vpu_reg *reg, *n;
645         list_for_each_entry_safe(reg, n, &session->waiting, session_link) {
646                 reg_deinit(reg);
647         }
648         list_for_each_entry_safe(reg, n, &session->running, session_link) {
649                 reg_deinit(reg);
650         }
651         list_for_each_entry_safe(reg, n, &session->done, session_link) {
652                 reg_deinit(reg);
653         }
654         }
655         spin_unlock_irqrestore(&service.lock, flag);
656
657         kfree(session);
658
659         pr_debug("dev closed\n");
660         return 0;
661 }
662
663 static const struct file_operations vpu_service_fops = {
664         .unlocked_ioctl = vpu_service_ioctl,
665         .open           = vpu_service_open,
666         .release        = vpu_service_release,
667         //.fasync       = vpu_service_fasync,
668 };
669
670 static struct miscdevice vpu_service_misc_device = {
671         .minor          = MISC_DYNAMIC_MINOR,
672         .name           = "vpu_service",
673         .fops           = &vpu_service_fops,
674 };
675
676 static void vpu_service_shutdown(struct platform_device *pdev)
677 {
678         pr_cont("shutdown...");
679         del_timer(&service.timer);
680         vpu_service_power_off();
681         pr_cont("done\n");
682 }
683
684 static int vpu_service_suspend(struct platform_device *pdev, pm_message_t state)
685 {
686         bool enabled;
687         pr_info("suspend...");
688         del_timer(&service.timer);
689         enabled = service.enabled;
690         vpu_service_power_off();
691         service.enabled = enabled;
692         return 0;
693 }
694
695 static int vpu_service_resume(struct platform_device *pdev)
696 {
697         pr_info("resume...");
698         if (service.enabled) {
699                 service.enabled = false;
700                 vpu_service_power_on();
701         }
702         return 0;
703 }
704
705 static struct platform_device vpu_service_device = {
706         .name              = "vpu_service",
707         .id                = -1,
708 };
709
710 static struct platform_driver vpu_service_driver = {
711         .driver    = {
712                 .name  = "vpu_service",
713                 .owner = THIS_MODULE,
714         },
715         .shutdown  = vpu_service_shutdown,
716         .suspend   = vpu_service_suspend,
717         .resume    = vpu_service_resume,
718 };
719
720 static void get_hw_info(void)
721 {
722         VPUHwDecConfig_t *dec = &service.dec_config;
723         VPUHwEncConfig_t *enc = &service.enc_config;
724         u32 configReg   = dec_dev.hwregs[VPU_DEC_HWCFG0];
725         u32 asicID      = dec_dev.hwregs[0];
726
727         dec->h264Support    = (configReg >> DWL_H264_E) & 0x3U;
728         dec->jpegSupport    = (configReg >> DWL_JPEG_E) & 0x01U;
729         if (dec->jpegSupport && ((configReg >> DWL_PJPEG_E) & 0x01U))
730                 dec->jpegSupport = JPEG_PROGRESSIVE;
731         dec->mpeg4Support   = (configReg >> DWL_MPEG4_E) & 0x3U;
732         dec->vc1Support     = (configReg >> DWL_VC1_E) & 0x3U;
733         dec->mpeg2Support   = (configReg >> DWL_MPEG2_E) & 0x01U;
734         dec->sorensonSparkSupport = (configReg >> DWL_SORENSONSPARK_E) & 0x01U;
735         dec->refBufSupport  = (configReg >> DWL_REF_BUFF_E) & 0x01U;
736         dec->vp6Support     = (configReg >> DWL_VP6_E) & 0x01U;
737         dec->maxDecPicWidth = configReg & 0x07FFU;
738
739         /* 2nd Config register */
740         configReg   = dec_dev.hwregs[VPU_DEC_HWCFG1];
741         if (dec->refBufSupport) {
742                 if ((configReg >> DWL_REF_BUFF_ILACE_E) & 0x01U)
743                         dec->refBufSupport |= 2;
744                 if ((configReg >> DWL_REF_BUFF_DOUBLE_E) & 0x01U)
745                         dec->refBufSupport |= 4;
746         }
747         dec->customMpeg4Support = (configReg >> DWL_MPEG4_CUSTOM_E) & 0x01U;
748         dec->vp7Support     = (configReg >> DWL_VP7_E) & 0x01U;
749         dec->vp8Support     = (configReg >> DWL_VP8_E) & 0x01U;
750         dec->avsSupport     = (configReg >> DWL_AVS_E) & 0x01U;
751
752         /* JPEG xtensions */
753         if (((asicID >> 16) >= 0x8190U) || ((asicID >> 16) == 0x6731U)) {
754                 dec->jpegESupport = (configReg >> DWL_JPEG_EXT_E) & 0x01U;
755         } else {
756                 dec->jpegESupport = JPEG_EXT_NOT_SUPPORTED;
757         }
758
759         if (((asicID >> 16) >= 0x9170U) || ((asicID >> 16) == 0x6731U) ) {
760                 dec->rvSupport = (configReg >> DWL_RV_E) & 0x03U;
761         } else {
762                 dec->rvSupport = RV_NOT_SUPPORTED;
763         }
764
765         dec->mvcSupport = (configReg >> DWL_MVC_E) & 0x03U;
766
767         if (dec->refBufSupport && (asicID >> 16) == 0x6731U ) {
768                 dec->refBufSupport |= 8; /* enable HW support for offset */
769         }
770
771         {
772         VPUHwFuseStatus_t hwFuseSts;
773         /* Decoder fuse configuration */
774         u32 fuseReg = dec_dev.hwregs[VPU_DEC_HW_FUSE_CFG];
775
776         hwFuseSts.h264SupportFuse = (fuseReg >> DWL_H264_FUSE_E) & 0x01U;
777         hwFuseSts.mpeg4SupportFuse = (fuseReg >> DWL_MPEG4_FUSE_E) & 0x01U;
778         hwFuseSts.mpeg2SupportFuse = (fuseReg >> DWL_MPEG2_FUSE_E) & 0x01U;
779         hwFuseSts.sorensonSparkSupportFuse = (fuseReg >> DWL_SORENSONSPARK_FUSE_E) & 0x01U;
780         hwFuseSts.jpegSupportFuse = (fuseReg >> DWL_JPEG_FUSE_E) & 0x01U;
781         hwFuseSts.vp6SupportFuse = (fuseReg >> DWL_VP6_FUSE_E) & 0x01U;
782         hwFuseSts.vc1SupportFuse = (fuseReg >> DWL_VC1_FUSE_E) & 0x01U;
783         hwFuseSts.jpegProgSupportFuse = (fuseReg >> DWL_PJPEG_FUSE_E) & 0x01U;
784         hwFuseSts.rvSupportFuse = (fuseReg >> DWL_RV_FUSE_E) & 0x01U;
785         hwFuseSts.avsSupportFuse = (fuseReg >> DWL_AVS_FUSE_E) & 0x01U;
786         hwFuseSts.vp7SupportFuse = (fuseReg >> DWL_VP7_FUSE_E) & 0x01U;
787         hwFuseSts.vp8SupportFuse = (fuseReg >> DWL_VP8_FUSE_E) & 0x01U;
788         hwFuseSts.customMpeg4SupportFuse = (fuseReg >> DWL_CUSTOM_MPEG4_FUSE_E) & 0x01U;
789         hwFuseSts.mvcSupportFuse = (fuseReg >> DWL_MVC_FUSE_E) & 0x01U;
790
791         /* check max. decoder output width */
792
793         if (fuseReg & 0x8000U)
794                 hwFuseSts.maxDecPicWidthFuse = 1920;
795         else if (fuseReg & 0x4000U)
796                 hwFuseSts.maxDecPicWidthFuse = 1280;
797         else if (fuseReg & 0x2000U)
798                 hwFuseSts.maxDecPicWidthFuse = 720;
799         else if (fuseReg & 0x1000U)
800                 hwFuseSts.maxDecPicWidthFuse = 352;
801         else    /* remove warning */
802                 hwFuseSts.maxDecPicWidthFuse = 352;
803
804         hwFuseSts.refBufSupportFuse = (fuseReg >> DWL_REF_BUFF_FUSE_E) & 0x01U;
805
806         /* Pp configuration */
807         configReg = dec_dev.hwregs[VPU_PP_HW_SYNTH_CFG];
808
809         if ((configReg >> DWL_PP_E) & 0x01U) {
810                 dec->ppSupport = 1;
811                 dec->maxPpOutPicWidth = configReg & 0x07FFU;
812                 /*pHwCfg->ppConfig = (configReg >> DWL_CFG_E) & 0x0FU; */
813                 dec->ppConfig = configReg;
814         } else {
815                 dec->ppSupport = 0;
816                 dec->maxPpOutPicWidth = 0;
817                 dec->ppConfig = 0;
818         }
819
820         /* check the HW versio */
821         if (((asicID >> 16) >= 0x8190U) || ((asicID >> 16) == 0x6731U)) {
822                 /* Pp configuration */
823                 configReg = dec_dev.hwregs[VPU_DEC_HW_FUSE_CFG];
824
825                 if ((configReg >> DWL_PP_E) & 0x01U) {
826                         /* Pp fuse configuration */
827                         u32 fuseRegPp = dec_dev.hwregs[VPU_PP_HW_FUSE_CFG];
828
829                         if ((fuseRegPp >> DWL_PP_FUSE_E) & 0x01U) {
830                                 hwFuseSts.ppSupportFuse = 1;
831                                 /* check max. pp output width */
832                                 if      (fuseRegPp & 0x8000U) hwFuseSts.maxPpOutPicWidthFuse = 1920;
833                                 else if (fuseRegPp & 0x4000U) hwFuseSts.maxPpOutPicWidthFuse = 1280;
834                                 else if (fuseRegPp & 0x2000U) hwFuseSts.maxPpOutPicWidthFuse = 720;
835                                 else if (fuseRegPp & 0x1000U) hwFuseSts.maxPpOutPicWidthFuse = 352;
836                                 else                          hwFuseSts.maxPpOutPicWidthFuse = 352;
837                                 hwFuseSts.ppConfigFuse = fuseRegPp;
838                         } else {
839                                 hwFuseSts.ppSupportFuse = 0;
840                                 hwFuseSts.maxPpOutPicWidthFuse = 0;
841                                 hwFuseSts.ppConfigFuse = 0;
842                         }
843                 } else {
844                         hwFuseSts.ppSupportFuse = 0;
845                         hwFuseSts.maxPpOutPicWidthFuse = 0;
846                         hwFuseSts.ppConfigFuse = 0;
847                 }
848
849                 if (dec->maxDecPicWidth > hwFuseSts.maxDecPicWidthFuse)
850                         dec->maxDecPicWidth = hwFuseSts.maxDecPicWidthFuse;
851                 if (dec->maxPpOutPicWidth > hwFuseSts.maxPpOutPicWidthFuse)
852                         dec->maxPpOutPicWidth = hwFuseSts.maxPpOutPicWidthFuse;
853                 if (!hwFuseSts.h264SupportFuse) dec->h264Support = H264_NOT_SUPPORTED;
854                 if (!hwFuseSts.mpeg4SupportFuse) dec->mpeg4Support = MPEG4_NOT_SUPPORTED;
855                 if (!hwFuseSts.customMpeg4SupportFuse) dec->customMpeg4Support = MPEG4_CUSTOM_NOT_SUPPORTED;
856                 if (!hwFuseSts.jpegSupportFuse) dec->jpegSupport = JPEG_NOT_SUPPORTED;
857                 if ((dec->jpegSupport == JPEG_PROGRESSIVE) && !hwFuseSts.jpegProgSupportFuse)
858                         dec->jpegSupport = JPEG_BASELINE;
859                 if (!hwFuseSts.mpeg2SupportFuse) dec->mpeg2Support = MPEG2_NOT_SUPPORTED;
860                 if (!hwFuseSts.vc1SupportFuse) dec->vc1Support = VC1_NOT_SUPPORTED;
861                 if (!hwFuseSts.vp6SupportFuse) dec->vp6Support = VP6_NOT_SUPPORTED;
862                 if (!hwFuseSts.vp7SupportFuse) dec->vp7Support = VP7_NOT_SUPPORTED;
863                 if (!hwFuseSts.vp8SupportFuse) dec->vp8Support = VP8_NOT_SUPPORTED;
864                 if (!hwFuseSts.ppSupportFuse) dec->ppSupport = PP_NOT_SUPPORTED;
865
866                 /* check the pp config vs fuse status */
867                 if ((dec->ppConfig & 0xFC000000) && ((hwFuseSts.ppConfigFuse & 0xF0000000) >> 5)) {
868                         u32 deInterlace = ((dec->ppConfig & PP_DEINTERLACING) >> 25);
869                         u32 alphaBlend  = ((dec->ppConfig & PP_ALPHA_BLENDING) >> 24);
870                         u32 deInterlaceFuse = (((hwFuseSts.ppConfigFuse >> 5) & PP_DEINTERLACING) >> 25);
871                         u32 alphaBlendFuse  = (((hwFuseSts.ppConfigFuse >> 5) & PP_ALPHA_BLENDING) >> 24);
872
873                         if (deInterlace && !deInterlaceFuse) dec->ppConfig &= 0xFD000000;
874                         if (alphaBlend && !alphaBlendFuse) dec->ppConfig &= 0xFE000000;
875                 }
876                 if (!hwFuseSts.sorensonSparkSupportFuse) dec->sorensonSparkSupport = SORENSON_SPARK_NOT_SUPPORTED;
877                 if (!hwFuseSts.refBufSupportFuse)   dec->refBufSupport = REF_BUF_NOT_SUPPORTED;
878                 if (!hwFuseSts.rvSupportFuse)       dec->rvSupport = RV_NOT_SUPPORTED;
879                 if (!hwFuseSts.avsSupportFuse)      dec->avsSupport = AVS_NOT_SUPPORTED;
880                 if (!hwFuseSts.mvcSupportFuse)      dec->mvcSupport = MVC_NOT_SUPPORTED;
881         }
882         }
883         configReg = enc_dev.hwregs[63];
884         enc->maxEncodedWidth = configReg & ((1 << 11) - 1);
885         enc->h264Enabled = (configReg >> 27) & 1;
886         enc->mpeg4Enabled = (configReg >> 26) & 1;
887         enc->jpegEnabled = (configReg >> 25) & 1;
888         enc->vsEnabled = (configReg >> 24) & 1;
889         enc->rgbEnabled = (configReg >> 28) & 1;
890         enc->busType = (configReg >> 20) & 15;
891         enc->synthesisLanguage = (configReg >> 16) & 15;
892         enc->busWidth = (configReg >> 12) & 15;
893 }
894
895 static irqreturn_t vdpu_isr(int irq, void *dev_id)
896 {
897         vpu_device *dev = (vpu_device *) dev_id;
898         u32 irq_status_dec = readl(dev->hwregs + DEC_INTERRUPT_REGISTER);
899         u32 irq_status_pp  = readl(dev->hwregs + PP_INTERRUPT_REGISTER);
900
901         pr_debug("vdpu_isr dec %x pp %x\n", irq_status_dec, irq_status_pp);
902
903         if (irq_status_dec & DEC_INTERRUPT_BIT) {
904                 irq_status_dec = readl(dev->hwregs + DEC_INTERRUPT_REGISTER);
905                 if ((irq_status_dec & 0x40001) == 0x40001)
906                 {
907                         do {
908                                 irq_status_dec = readl(dev->hwregs + DEC_INTERRUPT_REGISTER);
909                         } while ((irq_status_dec & 0x40001) == 0x40001);
910                 }
911                 /* clear dec IRQ */
912                 writel(irq_status_dec & (~DEC_INTERRUPT_BIT), dev->hwregs + DEC_INTERRUPT_REGISTER);
913                 pr_debug("DEC IRQ received!\n");
914                 atomic_sub(1, &service.task_running);
915                 if (NULL == service.reg_codec) {
916                         pr_err("dec isr with no task waiting\n");
917                 } else {
918                         reg_from_run_to_done(service.reg_codec);
919                 }
920         }
921
922         if (irq_status_pp & PP_INTERRUPT_BIT) {
923                 /* clear pp IRQ */
924                 writel(irq_status_pp & (~DEC_INTERRUPT_BIT), dev->hwregs + PP_INTERRUPT_REGISTER);
925                 pr_debug("PP IRQ received!\n");
926                 atomic_sub(1, &service.task_running);
927                 if (NULL == service.reg_pproc) {
928                         pr_err("pp isr with no task waiting\n");
929                 } else {
930                         reg_from_run_to_done(service.reg_pproc);
931                 }
932         }
933         try_set_reg();
934         return IRQ_HANDLED;
935 }
936
937 static irqreturn_t vepu_isr(int irq, void *dev_id)
938 {
939         struct vpu_device *dev = (struct vpu_device *) dev_id;
940         u32 irq_status = readl(dev->hwregs + ENC_INTERRUPT_REGISTER);
941
942         pr_debug("enc_isr\n");
943
944         if (likely(irq_status & ENC_INTERRUPT_BIT)) {
945                 /* clear enc IRQ */
946                 writel(irq_status & (~ENC_INTERRUPT_BIT), dev->hwregs + ENC_INTERRUPT_REGISTER);
947                 pr_debug("ENC IRQ received!\n");
948                 atomic_sub(1, &service.task_running);
949                 if (NULL == service.reg_codec) {
950                         pr_err("enc isr with no task waiting\n");
951                 } else {
952                         reg_from_run_to_done(service.reg_codec);
953                 }
954         }
955         try_set_reg();
956         return IRQ_HANDLED;
957 }
958
959 static int __init vpu_service_init(void)
960 {
961         int ret;
962
963         pr_debug("baseaddr = 0x%08x vdpu irq = %d vepu irq = %d\n", RK29_VCODEC_PHYS, IRQ_VDPU, IRQ_VEPU);
964
965         dec_dev.iobaseaddr      = RK29_VCODEC_PHYS + 0x200;
966         dec_dev.iosize          = DEC_IO_SIZE;
967         enc_dev.iobaseaddr      = RK29_VCODEC_PHYS;
968         enc_dev.iosize          = ENC_IO_SIZE;
969
970         INIT_LIST_HEAD(&service.waiting);
971         INIT_LIST_HEAD(&service.running);
972         INIT_LIST_HEAD(&service.done);
973         INIT_LIST_HEAD(&service.session);
974         spin_lock_init(&service.lock);
975         service.reg_codec       = NULL;
976         service.reg_pproc       = NULL;
977         atomic_set(&service.task_running, 0);
978         service.enabled         = false;
979
980         vpu_get_clk();
981         vpu_service_power_on();
982
983         ret = vpu_service_reserve_io();
984         if (ret < 0) {
985                 pr_err("reserve io failed\n");
986                 goto err_reserve_io;
987         }
988
989         /* get the IRQ line */
990         ret = request_irq(IRQ_VDPU, vdpu_isr, IRQF_SHARED, "vdpu", (void *)&dec_dev);
991         if (ret) {
992                 pr_err("can't request vdpu irq %d\n", IRQ_VDPU);
993                 goto err_req_vdpu_irq;
994         }
995
996         ret = request_irq(IRQ_VEPU, vepu_isr, IRQF_SHARED, "vepu", (void *)&enc_dev);
997         if (ret) {
998                 pr_err("can't request vepu irq %d\n", IRQ_VEPU);
999                 goto err_req_vepu_irq;
1000         }
1001
1002         ret = misc_register(&vpu_service_misc_device);
1003         if (ret) {
1004                 pr_err("misc_register failed\n");
1005                 goto err_register;
1006         }
1007
1008         platform_device_register(&vpu_service_device);
1009         platform_driver_probe(&vpu_service_driver, NULL);
1010         get_hw_info();
1011         del_timer(&service.timer);
1012         vpu_service_power_off();
1013         pr_info("init success\n");
1014
1015         return 0;
1016
1017 err_register:
1018         free_irq(IRQ_VEPU, (void *)&enc_dev);
1019 err_req_vepu_irq:
1020         free_irq(IRQ_VDPU, (void *)&dec_dev);
1021 err_req_vdpu_irq:
1022         pr_info("init failed\n");
1023 err_reserve_io:
1024         del_timer(&service.timer);
1025         vpu_service_power_off();
1026         vpu_service_release_io();
1027         vpu_put_clk();
1028         pr_info("init failed\n");
1029         return ret;
1030 }
1031
1032 static void __exit vpu_service_exit(void)
1033 {
1034         del_timer(&service.timer);
1035         vpu_service_power_off();
1036         platform_device_unregister(&vpu_service_device);
1037         platform_driver_unregister(&vpu_service_driver);
1038         misc_deregister(&vpu_service_misc_device);
1039         free_irq(IRQ_VEPU, (void *)&enc_dev);
1040         free_irq(IRQ_VDPU, (void *)&dec_dev);
1041         vpu_put_clk();
1042 }
1043
1044 module_init(vpu_service_init);
1045 module_exit(vpu_service_exit);
1046 MODULE_LICENSE("GPL");
1047
1048 #ifdef CONFIG_PROC_FS
1049 #include <linux/proc_fs.h>
1050 #include <linux/seq_file.h>
1051
1052 static int proc_vpu_service_show(struct seq_file *s, void *v)
1053 {
1054         unsigned int i, n;
1055         unsigned long flag;
1056         vpu_reg *reg, *reg_tmp;
1057         vpu_session *session, *session_tmp;
1058
1059         vpu_service_power_on();
1060         seq_printf(s, "\nENC Registers:\n");
1061         n = enc_dev.iosize >> 2;
1062         for (i = 0; i < n; i++) {
1063                 seq_printf(s, "\tswreg%d = %08X\n", i, readl(enc_dev.hwregs + i));
1064         }
1065         seq_printf(s, "\nDEC Registers:\n");
1066         n = dec_dev.iosize >> 2;
1067         for (i = 0; i < n; i++) {
1068                 seq_printf(s, "\tswreg%d = %08X\n", i, readl(dec_dev.hwregs + i));
1069         }
1070
1071         seq_printf(s, "\nvpu service status:\n");
1072         spin_lock_irqsave(&service.lock, flag);
1073         list_for_each_entry_safe(session, session_tmp, &service.session, list_session) {
1074                 seq_printf(s, "session pid %d type %d:\n", session->pid, session->type);
1075                 //seq_printf(s, "waiting reg set %d\n");
1076                 list_for_each_entry_safe(reg, reg_tmp, &session->waiting, session_link) {
1077                         seq_printf(s, "waiting register set\n");
1078                 }
1079                 list_for_each_entry_safe(reg, reg_tmp, &session->running, session_link) {
1080                         seq_printf(s, "running register set\n");
1081                 }
1082                 list_for_each_entry_safe(reg, reg_tmp, &session->done, session_link) {
1083                         seq_printf(s, "done    register set\n");
1084                 }
1085         }
1086         spin_unlock_irqrestore(&service.lock, flag);
1087
1088         return 0;
1089 }
1090
1091 static int proc_vpu_service_open(struct inode *inode, struct file *file)
1092 {
1093         return single_open(file, proc_vpu_service_show, NULL);
1094 }
1095
1096 static const struct file_operations proc_vpu_service_fops = {
1097         .open           = proc_vpu_service_open,
1098         .read           = seq_read,
1099         .llseek         = seq_lseek,
1100         .release        = single_release,
1101 };
1102
1103 static int __init vpu_service_proc_init(void)
1104 {
1105         proc_create("vpu_service", 0, NULL, &proc_vpu_service_fops);
1106         return 0;
1107
1108 }
1109 late_initcall(vpu_service_proc_init);
1110 #endif /* CONFIG_PROC_FS */
1111