2 * Copyright (c) 2016 MediaTek Inc.
3 * Author: Jungchang Tsao <jungchang.tsao@mediatek.com>
4 * PC Chen <pc.chen@mediatek.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <linux/slab.h>
17 #include "../vdec_drv_if.h"
18 #include "../mtk_vcodec_util.h"
19 #include "../mtk_vcodec_dec.h"
20 #include "../mtk_vcodec_intr.h"
21 #include "../vdec_vpu_if.h"
22 #include "../vdec_drv_base.h"
24 /* Decoding picture buffer size (3 reference frames plus current frame) */
25 #define VP8_DPB_SIZE 4
27 /* HW working buffer size (bytes) */
28 #define VP8_WORKING_BUF_SZ (45 * 4096)
30 /* HW control register address */
31 #define VP8_SEGID_DRAM_ADDR 0x3c
32 #define VP8_HW_VLD_ADDR 0x93C
33 #define VP8_HW_VLD_VALUE 0x940
34 #define VP8_BSASET 0x100
35 #define VP8_BSDSET 0x104
36 #define VP8_RW_CKEN_SET 0x0
37 #define VP8_RW_DCM_CON 0x18
38 #define VP8_WO_VLD_SRST 0x108
39 #define VP8_RW_MISC_SYS_SEL 0x84
40 #define VP8_RW_MISC_SPEC_CON 0xC8
41 #define VP8_WO_VLD_SRST 0x108
42 #define VP8_RW_VP8_CTRL 0xA4
43 #define VP8_RW_MISC_DCM_CON 0xEC
44 #define VP8_RW_MISC_SRST 0xF4
45 #define VP8_RW_MISC_FUNC_CON 0xCC
47 #define VP8_MAX_FRM_BUF_NUM 5
48 #define VP8_MAX_FRM_BUF_NODE_NUM (VP8_MAX_FRM_BUF_NUM * 2)
50 /* required buffer size (bytes) to store decode information */
51 #define VP8_HW_SEGMENT_DATA_SZ 272
52 #define VP8_HW_SEGMENT_UINT 4
54 #define VP8_DEC_TABLE_PROC_LOOP 96
55 #define VP8_DEC_TABLE_UNIT 3
56 #define VP8_DEC_TABLE_SZ 300
57 #define VP8_DEC_TABLE_OFFSET 2
58 #define VP8_DEC_TABLE_RW_UNIT 4
61 * struct vdec_vp8_dec_info - decode misc information
62 * @working_buf_dma : working buffer dma address
63 * @prev_y_dma : previous decoded frame buffer Y plane address
64 * @cur_y_fb_dma : current plane Y frame buffer dma address
65 * @cur_c_fb_dma : current plane C frame buffer dma address
66 * @bs_dma : bitstream dma address
67 * @bs_sz : bitstream size
68 * @resolution_changed: resolution change flag 1 - changed, 0 - not change
69 * @show_frame : display this frame or not
70 * @wait_key_frame : wait key frame coming
72 struct vdec_vp8_dec_info
{
73 uint64_t working_buf_dma
;
75 uint64_t cur_y_fb_dma
;
76 uint64_t cur_c_fb_dma
;
79 uint32_t resolution_changed
;
81 uint32_t wait_key_frame
;
85 * struct vdec_vp8_vsi - VPU shared information
86 * @dec : decoding information
87 * @pic : picture information
88 * @dec_table : decoder coefficient table
89 * @segment_buf : segmentation buffer
90 * @load_data : flag to indicate reload decode data
93 struct vdec_vp8_dec_info dec
;
94 struct vdec_pic_info pic
;
95 uint32_t dec_table
[VP8_DEC_TABLE_SZ
];
96 uint32_t segment_buf
[VP8_HW_SEGMENT_DATA_SZ
][VP8_HW_SEGMENT_UINT
];
101 * struct vdec_vp8_hw_reg_base - HW register base
102 * @sys : base address for sys
103 * @misc : base address for misc
104 * @ld : base address for ld
105 * @top : base address for top
106 * @cm : base address for cm
107 * @hwd : base address for hwd
108 * @hwb : base address for hwb
110 struct vdec_vp8_hw_reg_base
{
121 * struct vdec_vp8_vpu_inst - VPU instance for VP8 decode
122 * @wq_hd : Wait queue to wait VPU message ack
123 * @signaled : 1 - Host has received ack message from VPU, 0 - not recevie
124 * @failure : VPU execution result status 0 - success, others - fail
125 * @inst_addr : VPU decoder instance address
127 struct vdec_vp8_vpu_inst
{
128 wait_queue_head_t wq_hd
;
134 /* frame buffer (fb) list
135 * [available_fb_node_list] - decode fb are initialized to 0 and populated in
136 * [fb_use_list] - fb is set after decode and is moved to this list
137 * [fb_free_list] - fb is not needed for reference will be moved from
138 * [fb_use_list] to [fb_free_list] and
139 * once user remove fb from [fb_free_list],
140 * it is circulated back to [available_fb_node_list]
141 * [fb_disp_list] - fb is set after decode and is moved to this list
142 * once user remove fb from [fb_disp_list] it is
143 * circulated back to [available_fb_node_list]
147 * struct vdec_vp8_inst - VP8 decoder instance
148 * @cur_fb : current frame buffer
149 * @dec_fb : decode frame buffer node
150 * @available_fb_node_list : list to store available frame buffer node
151 * @fb_use_list : list to store frame buffer in use
152 * @fb_free_list : list to store free frame buffer
153 * @fb_disp_list : list to store display ready frame buffer
154 * @working_buf : HW decoder working buffer
155 * @reg_base : HW register base address
156 * @frm_cnt : decode frame count
157 * @ctx : V4L2 context
158 * @dev : platform device
159 * @vpu : VPU instance for decoder
160 * @vsi : VPU share information
162 struct vdec_vp8_inst
{
163 struct vdec_fb
*cur_fb
;
164 struct vdec_fb_node dec_fb
[VP8_MAX_FRM_BUF_NODE_NUM
];
165 struct list_head available_fb_node_list
;
166 struct list_head fb_use_list
;
167 struct list_head fb_free_list
;
168 struct list_head fb_disp_list
;
169 struct mtk_vcodec_mem working_buf
;
170 struct vdec_vp8_hw_reg_base reg_base
;
171 unsigned int frm_cnt
;
172 struct mtk_vcodec_ctx
*ctx
;
173 struct vdec_vpu_inst vpu
;
174 struct vdec_vp8_vsi
*vsi
;
177 static void get_hw_reg_base(struct vdec_vp8_inst
*inst
)
179 inst
->reg_base
.top
= mtk_vcodec_get_reg_addr(inst
->ctx
, VDEC_TOP
);
180 inst
->reg_base
.cm
= mtk_vcodec_get_reg_addr(inst
->ctx
, VDEC_CM
);
181 inst
->reg_base
.hwd
= mtk_vcodec_get_reg_addr(inst
->ctx
, VDEC_HWD
);
182 inst
->reg_base
.sys
= mtk_vcodec_get_reg_addr(inst
->ctx
, VDEC_SYS
);
183 inst
->reg_base
.misc
= mtk_vcodec_get_reg_addr(inst
->ctx
, VDEC_MISC
);
184 inst
->reg_base
.ld
= mtk_vcodec_get_reg_addr(inst
->ctx
, VDEC_LD
);
185 inst
->reg_base
.hwb
= mtk_vcodec_get_reg_addr(inst
->ctx
, VDEC_HWB
);
188 static void write_hw_segmentation_data(struct vdec_vp8_inst
*inst
)
193 void __iomem
*cm
= inst
->reg_base
.cm
;
194 struct vdec_vp8_vsi
*vsi
= inst
->vsi
;
196 seg_id_addr
= readl(inst
->reg_base
.top
+ VP8_SEGID_DRAM_ADDR
) >> 4;
198 for (i
= 0; i
< ARRAY_SIZE(vsi
->segment_buf
); i
++) {
199 for (j
= ARRAY_SIZE(vsi
->segment_buf
[i
]) - 1; j
>= 0; j
--) {
200 val
= (1 << 16) + ((seg_id_addr
+ i
) << 2) + j
;
201 writel(val
, cm
+ VP8_HW_VLD_ADDR
);
203 val
= vsi
->segment_buf
[i
][j
];
204 writel(val
, cm
+ VP8_HW_VLD_VALUE
);
209 static void read_hw_segmentation_data(struct vdec_vp8_inst
*inst
)
214 void __iomem
*cm
= inst
->reg_base
.cm
;
215 struct vdec_vp8_vsi
*vsi
= inst
->vsi
;
217 seg_id_addr
= readl(inst
->reg_base
.top
+ VP8_SEGID_DRAM_ADDR
) >> 4;
219 for (i
= 0; i
< ARRAY_SIZE(vsi
->segment_buf
); i
++) {
220 for (j
= ARRAY_SIZE(vsi
->segment_buf
[i
]) - 1; j
>= 0; j
--) {
221 val
= ((seg_id_addr
+ i
) << 2) + j
;
222 writel(val
, cm
+ VP8_HW_VLD_ADDR
);
224 val
= readl(cm
+ VP8_HW_VLD_VALUE
);
225 vsi
->segment_buf
[i
][j
] = val
;
230 /* reset HW and enable HW read/write data function */
231 static void enable_hw_rw_function(struct vdec_vp8_inst
*inst
)
234 void __iomem
*sys
= inst
->reg_base
.sys
;
235 void __iomem
*misc
= inst
->reg_base
.misc
;
236 void __iomem
*ld
= inst
->reg_base
.ld
;
237 void __iomem
*hwb
= inst
->reg_base
.hwb
;
238 void __iomem
*hwd
= inst
->reg_base
.hwd
;
240 writel(0x1, sys
+ VP8_RW_CKEN_SET
);
241 writel(0x101, ld
+ VP8_WO_VLD_SRST
);
242 writel(0x101, hwb
+ VP8_WO_VLD_SRST
);
245 val
= readl(misc
+ VP8_RW_MISC_SRST
);
246 writel((val
& 0xFFFFFFFE), misc
+ VP8_RW_MISC_SRST
);
248 writel(0x1, misc
+ VP8_RW_MISC_SYS_SEL
);
249 writel(0x17F, misc
+ VP8_RW_MISC_SPEC_CON
);
250 writel(0x71201100, misc
+ VP8_RW_MISC_FUNC_CON
);
251 writel(0x0, ld
+ VP8_WO_VLD_SRST
);
252 writel(0x0, hwb
+ VP8_WO_VLD_SRST
);
253 writel(0x1, sys
+ VP8_RW_DCM_CON
);
254 writel(0x1, misc
+ VP8_RW_MISC_DCM_CON
);
255 writel(0x1, hwd
+ VP8_RW_VP8_CTRL
);
258 static void store_dec_table(struct vdec_vp8_inst
*inst
)
261 u32 addr
= 0, val
= 0;
262 void __iomem
*hwd
= inst
->reg_base
.hwd
;
263 u32
*p
= &inst
->vsi
->dec_table
[VP8_DEC_TABLE_OFFSET
];
265 for (i
= 0; i
< VP8_DEC_TABLE_PROC_LOOP
; i
++) {
266 writel(addr
, hwd
+ VP8_BSASET
);
267 for (j
= 0; j
< VP8_DEC_TABLE_UNIT
; j
++) {
269 writel(val
, hwd
+ VP8_BSDSET
);
271 addr
+= VP8_DEC_TABLE_RW_UNIT
;
275 static void load_dec_table(struct vdec_vp8_inst
*inst
)
279 u32
*p
= &inst
->vsi
->dec_table
[VP8_DEC_TABLE_OFFSET
];
280 void __iomem
*hwd
= inst
->reg_base
.hwd
;
282 for (i
= 0; i
< VP8_DEC_TABLE_PROC_LOOP
; i
++) {
283 writel(addr
, hwd
+ VP8_BSASET
);
284 /* read total 11 bytes */
285 *p
++ = readl(hwd
+ VP8_BSDSET
);
286 *p
++ = readl(hwd
+ VP8_BSDSET
);
287 *p
++ = readl(hwd
+ VP8_BSDSET
) & 0xFFFFFF;
288 addr
+= VP8_DEC_TABLE_RW_UNIT
;
292 static void get_pic_info(struct vdec_vp8_inst
*inst
, struct vdec_pic_info
*pic
)
294 *pic
= inst
->vsi
->pic
;
296 mtk_vcodec_debug(inst
, "pic(%d, %d), buf(%d, %d)",
297 pic
->pic_w
, pic
->pic_h
, pic
->buf_w
, pic
->buf_h
);
298 mtk_vcodec_debug(inst
, "Y(%d, %d), C(%d, %d)", pic
->y_bs_sz
,
299 pic
->y_len_sz
, pic
->c_bs_sz
, pic
->c_len_sz
);
302 static void vp8_dec_finish(struct vdec_vp8_inst
*inst
)
304 struct vdec_fb_node
*node
;
305 uint64_t prev_y_dma
= inst
->vsi
->dec
.prev_y_dma
;
307 mtk_vcodec_debug(inst
, "prev fb base dma=%llx", prev_y_dma
);
309 /* put last decode ok frame to fb_free_list */
310 if (prev_y_dma
!= 0) {
311 list_for_each_entry(node
, &inst
->fb_use_list
, list
) {
312 struct vdec_fb
*fb
= (struct vdec_fb
*)node
->fb
;
314 if (prev_y_dma
== (uint64_t)fb
->base_y
.dma_addr
) {
315 list_move_tail(&node
->list
,
316 &inst
->fb_free_list
);
322 /* available_fb_node_list -> fb_use_list */
323 node
= list_first_entry(&inst
->available_fb_node_list
,
324 struct vdec_fb_node
, list
);
325 node
->fb
= inst
->cur_fb
;
326 list_move_tail(&node
->list
, &inst
->fb_use_list
);
328 /* available_fb_node_list -> fb_disp_list */
329 if (inst
->vsi
->dec
.show_frame
) {
330 node
= list_first_entry(&inst
->available_fb_node_list
,
331 struct vdec_fb_node
, list
);
332 node
->fb
= inst
->cur_fb
;
333 list_move_tail(&node
->list
, &inst
->fb_disp_list
);
337 static void move_fb_list_use_to_free(struct vdec_vp8_inst
*inst
)
339 struct vdec_fb_node
*node
, *tmp
;
341 list_for_each_entry_safe(node
, tmp
, &inst
->fb_use_list
, list
)
342 list_move_tail(&node
->list
, &inst
->fb_free_list
);
345 static void init_list(struct vdec_vp8_inst
*inst
)
349 INIT_LIST_HEAD(&inst
->available_fb_node_list
);
350 INIT_LIST_HEAD(&inst
->fb_use_list
);
351 INIT_LIST_HEAD(&inst
->fb_free_list
);
352 INIT_LIST_HEAD(&inst
->fb_disp_list
);
354 for (i
= 0; i
< ARRAY_SIZE(inst
->dec_fb
); i
++) {
355 INIT_LIST_HEAD(&inst
->dec_fb
[i
].list
);
356 inst
->dec_fb
[i
].fb
= NULL
;
357 list_add_tail(&inst
->dec_fb
[i
].list
,
358 &inst
->available_fb_node_list
);
362 static void add_fb_to_free_list(struct vdec_vp8_inst
*inst
, void *fb
)
364 struct vdec_fb_node
*node
;
367 node
= list_first_entry(&inst
->available_fb_node_list
,
368 struct vdec_fb_node
, list
);
370 list_move_tail(&node
->list
, &inst
->fb_free_list
);
374 static int alloc_working_buf(struct vdec_vp8_inst
*inst
)
377 struct mtk_vcodec_mem
*mem
= &inst
->working_buf
;
379 mem
->size
= VP8_WORKING_BUF_SZ
;
380 err
= mtk_vcodec_mem_alloc(inst
->ctx
, mem
);
382 mtk_vcodec_err(inst
, "Cannot allocate working buffer");
386 inst
->vsi
->dec
.working_buf_dma
= (uint64_t)mem
->dma_addr
;
390 static void free_working_buf(struct vdec_vp8_inst
*inst
)
392 struct mtk_vcodec_mem
*mem
= &inst
->working_buf
;
395 mtk_vcodec_mem_free(inst
->ctx
, mem
);
397 inst
->vsi
->dec
.working_buf_dma
= 0;
400 static int vdec_vp8_init(struct mtk_vcodec_ctx
*ctx
, unsigned long *h_vdec
)
402 struct vdec_vp8_inst
*inst
;
405 inst
= kzalloc(sizeof(*inst
), GFP_KERNEL
);
411 inst
->vpu
.id
= IPI_VDEC_VP8
;
412 inst
->vpu
.dev
= ctx
->dev
->vpu_plat_dev
;
414 inst
->vpu
.handler
= vpu_dec_ipi_handler
;
416 err
= vpu_dec_init(&inst
->vpu
);
418 mtk_vcodec_err(inst
, "vdec_vp8 init err=%d", err
);
419 goto error_free_inst
;
422 inst
->vsi
= (struct vdec_vp8_vsi
*)inst
->vpu
.vsi
;
424 err
= alloc_working_buf(inst
);
428 get_hw_reg_base(inst
);
429 mtk_vcodec_debug(inst
, "VP8 Instance >> %p", inst
);
431 *h_vdec
= (unsigned long)inst
;
435 vpu_dec_deinit(&inst
->vpu
);
441 static int vdec_vp8_decode(unsigned long h_vdec
, struct mtk_vcodec_mem
*bs
,
442 struct vdec_fb
*fb
, bool *res_chg
)
444 struct vdec_vp8_inst
*inst
= (struct vdec_vp8_inst
*)h_vdec
;
445 struct vdec_vp8_dec_info
*dec
= &inst
->vsi
->dec
;
446 struct vdec_vpu_inst
*vpu
= &inst
->vpu
;
447 unsigned char *bs_va
;
453 /* bs NULL means flush decoder */
455 move_fb_list_use_to_free(inst
);
456 return vpu_dec_reset(vpu
);
459 y_fb_dma
= fb
? (u64
)fb
->base_y
.dma_addr
: 0;
460 c_fb_dma
= fb
? (u64
)fb
->base_c
.dma_addr
: 0;
462 mtk_vcodec_debug(inst
, "+ [%d] FB y_dma=%llx c_dma=%llx fb=%p",
463 inst
->frm_cnt
, y_fb_dma
, c_fb_dma
, fb
);
466 dec
->bs_dma
= (unsigned long)bs
->dma_addr
;
467 dec
->bs_sz
= bs
->size
;
468 dec
->cur_y_fb_dma
= y_fb_dma
;
469 dec
->cur_c_fb_dma
= c_fb_dma
;
471 mtk_vcodec_debug(inst
, "\n + FRAME[%d] +\n", inst
->frm_cnt
);
473 write_hw_segmentation_data(inst
);
474 enable_hw_rw_function(inst
);
475 store_dec_table(inst
);
477 bs_va
= (unsigned char *)bs
->va
;
479 /* retrieve width/hight and scale info from header */
480 data
= (*(bs_va
+ 9) << 24) | (*(bs_va
+ 8) << 16) |
481 (*(bs_va
+ 7) << 8) | *(bs_va
+ 6);
482 err
= vpu_dec_start(vpu
, &data
, 1);
484 add_fb_to_free_list(inst
, fb
);
485 if (dec
->wait_key_frame
) {
486 mtk_vcodec_debug(inst
, "wait key frame !");
493 if (dec
->resolution_changed
) {
494 mtk_vcodec_debug(inst
, "- resolution_changed -");
496 add_fb_to_free_list(inst
, fb
);
500 /* wait decoder done interrupt */
501 mtk_vcodec_wait_for_done_ctx(inst
->ctx
, MTK_INST_IRQ_RECEIVED
,
502 WAIT_INTR_TIMEOUT_MS
);
504 if (inst
->vsi
->load_data
)
505 load_dec_table(inst
);
507 vp8_dec_finish(inst
);
508 read_hw_segmentation_data(inst
);
510 err
= vpu_dec_end(vpu
);
514 mtk_vcodec_debug(inst
, "\n - FRAME[%d] - show=%d\n", inst
->frm_cnt
,
521 mtk_vcodec_err(inst
, "\n - FRAME[%d] - err=%d\n", inst
->frm_cnt
, err
);
525 static void get_disp_fb(struct vdec_vp8_inst
*inst
, struct vdec_fb
**out_fb
)
527 struct vdec_fb_node
*node
;
530 node
= list_first_entry_or_null(&inst
->fb_disp_list
,
531 struct vdec_fb_node
, list
);
533 list_move_tail(&node
->list
, &inst
->available_fb_node_list
);
534 fb
= (struct vdec_fb
*)node
->fb
;
535 fb
->status
|= FB_ST_DISPLAY
;
536 mtk_vcodec_debug(inst
, "[FB] get disp fb %p st=%d",
537 node
->fb
, fb
->status
);
540 mtk_vcodec_debug(inst
, "[FB] there is no disp fb");
546 static void get_free_fb(struct vdec_vp8_inst
*inst
, struct vdec_fb
**out_fb
)
548 struct vdec_fb_node
*node
;
551 node
= list_first_entry_or_null(&inst
->fb_free_list
,
552 struct vdec_fb_node
, list
);
554 list_move_tail(&node
->list
, &inst
->available_fb_node_list
);
555 fb
= (struct vdec_fb
*)node
->fb
;
556 fb
->status
|= FB_ST_FREE
;
557 mtk_vcodec_debug(inst
, "[FB] get free fb %p st=%d",
558 node
->fb
, fb
->status
);
561 mtk_vcodec_debug(inst
, "[FB] there is no free fb");
567 static void get_crop_info(struct vdec_vp8_inst
*inst
, struct v4l2_rect
*cr
)
571 cr
->width
= inst
->vsi
->pic
.pic_w
;
572 cr
->height
= inst
->vsi
->pic
.pic_h
;
573 mtk_vcodec_debug(inst
, "get crop info l=%d, t=%d, w=%d, h=%d",
574 cr
->left
, cr
->top
, cr
->width
, cr
->height
);
577 static int vdec_vp8_get_param(unsigned long h_vdec
,
578 enum vdec_get_param_type type
, void *out
)
580 struct vdec_vp8_inst
*inst
= (struct vdec_vp8_inst
*)h_vdec
;
583 case GET_PARAM_DISP_FRAME_BUFFER
:
584 get_disp_fb(inst
, out
);
587 case GET_PARAM_FREE_FRAME_BUFFER
:
588 get_free_fb(inst
, out
);
591 case GET_PARAM_PIC_INFO
:
592 get_pic_info(inst
, out
);
595 case GET_PARAM_CROP_INFO
:
596 get_crop_info(inst
, out
);
599 case GET_PARAM_DPB_SIZE
:
600 *((unsigned int *)out
) = VP8_DPB_SIZE
;
604 mtk_vcodec_err(inst
, "invalid get parameter type=%d", type
);
611 static void vdec_vp8_deinit(unsigned long h_vdec
)
613 struct vdec_vp8_inst
*inst
= (struct vdec_vp8_inst
*)h_vdec
;
615 mtk_vcodec_debug_enter(inst
);
617 vpu_dec_deinit(&inst
->vpu
);
618 free_working_buf(inst
);
622 static struct vdec_common_if vdec_vp8_if
= {
629 struct vdec_common_if
*get_vp8_dec_comm_if(void);
631 struct vdec_common_if
*get_vp8_dec_comm_if(void)