summaryrefslogtreecommitdiff
path: root/drivers/frame_provider/decoder/mpeg12/vmpeg12_multi.c (plain)
blob: 6f64b7c4ae9054562df7b9206107a9a3e6f9df6c
1/*
2 * drivers/amlogic/amports/vmpeg12.c
3 *
4 * Copyright (C) 2015 Amlogic, Inc. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16*/
17
18#include <linux/kernel.h>
19#include <linux/types.h>
20#include <linux/errno.h>
21#include <linux/interrupt.h>
22#include <linux/timer.h>
23#include <linux/kfifo.h>
24#include <linux/platform_device.h>
25#include <linux/random.h>
26#include <linux/dma-mapping.h>
27#include <linux/slab.h>
28#include <linux/delay.h>
29
30#include <linux/amlogic/media/frame_sync/ptsserv.h>
31#include <linux/amlogic/media/utils/amstream.h>
32#include <linux/amlogic/media/canvas/canvas.h>
33#include <linux/amlogic/media/vfm/vframe.h>
34#include <linux/amlogic/media/vfm/vframe_provider.h>
35#include <linux/amlogic/media/vfm/vframe_receiver.h>
36#include <linux/amlogic/tee.h>
37
38#include <linux/amlogic/media/utils/vdec_reg.h>
39#include <linux/amlogic/media/registers/register.h>
40#include "../../../stream_input/amports/amports_priv.h"
41
42#include "../utils/vdec_input.h"
43#include "../utils/vdec.h"
44#include "../utils/amvdec.h"
45#include "../utils/decoder_mmu_box.h"
46#include "../utils/decoder_bmmu_box.h"
47#include <linux/amlogic/media/codec_mm/codec_mm.h>
48#include <linux/amlogic/media/codec_mm/configs.h>
49#include "../utils/firmware.h"
50
51
52#define MEM_NAME "codec_mmpeg12"
53#define CHECK_INTERVAL (HZ/100)
54
55#define DRIVER_NAME "ammvdec_mpeg12"
56#define MODULE_NAME "ammvdec_mpeg12"
57#define MREG_REF0 AV_SCRATCH_2
58#define MREG_REF1 AV_SCRATCH_3
59/* protocol registers */
60#define MREG_SEQ_INFO AV_SCRATCH_4
61#define MREG_PIC_INFO AV_SCRATCH_5
62#define MREG_PIC_WIDTH AV_SCRATCH_6
63#define MREG_PIC_HEIGHT AV_SCRATCH_7
64#define MREG_INPUT AV_SCRATCH_8 /*input_type*/
65#define MREG_BUFFEROUT AV_SCRATCH_9 /*FROM_AMRISC_REG*/
66
67#define MREG_CMD AV_SCRATCH_A
68#define MREG_CO_MV_START AV_SCRATCH_B
69#define MREG_ERROR_COUNT AV_SCRATCH_C
70#define MREG_FRAME_OFFSET AV_SCRATCH_D
71#define MREG_WAIT_BUFFER AV_SCRATCH_E
72#define MREG_FATAL_ERROR AV_SCRATCH_F
73
74#define GET_SLICE_TYPE(type) ("IPB##"[((type&PICINFO_TYPE_MASK)>>16)&0x3])
75#define PICINFO_ERROR 0x80000000
76#define PICINFO_TYPE_MASK 0x00030000
77#define PICINFO_TYPE_I 0x00000000
78#define PICINFO_TYPE_P 0x00010000
79#define PICINFO_TYPE_B 0x00020000
80#define PICINFO_PROG 0x8000
81#define PICINFO_RPT_FIRST 0x4000
82#define PICINFO_TOP_FIRST 0x2000
83#define PICINFO_FRAME 0x1000
84
85#define TOP_FIELD 0x1000
86#define BOTTOM_FIELD 0x2000
87#define FRAME_PICTURE 0x3000
88#define FRAME_PICTURE_MASK 0x3000
89
90#define SEQINFO_EXT_AVAILABLE 0x80000000
91#define SEQINFO_PROG 0x00010000
92#define CCBUF_SIZE (5*1024)
93
94#define VF_POOL_SIZE 32
95#define DECODE_BUFFER_NUM_MAX 8
96#define PUT_INTERVAL (HZ/100)
97#define WORKSPACE_SIZE (4*SZ_64K) /*swap&ccbuf&matirx&MV*/
98#define CTX_LMEM_SWAP_OFFSET 0
99#define CTX_CCBUF_OFFSET 0x800
100#define CTX_QUANT_MATRIX_OFFSET (CTX_CCBUF_OFFSET + 5*1024)
101#define CTX_CO_MV_OFFSET (CTX_QUANT_MATRIX_OFFSET + 1*1024)
102#define CTX_DECBUF_OFFSET (CTX_CO_MV_OFFSET + 0x11000)
103
104#define MAX_BMMU_BUFFER_NUM (DECODE_BUFFER_NUM_MAX + 1)
105#define DEFAULT_MEM_SIZE (32*SZ_1M)
106static u32 buf_size = 32 * 1024 * 1024;
107static int pre_decode_buf_level = 0x800;
108static u32 dec_control;
109static u32 error_frame_skip_level = 2;
110static u32 stat;
111static u32 udebug_flag;
112static unsigned int radr;
113static unsigned int rval;
114
115static u32 without_display_mode;
116
117#define VMPEG12_DEV_NUM 9
118static unsigned int max_decode_instance_num = VMPEG12_DEV_NUM;
119static unsigned int max_process_time[VMPEG12_DEV_NUM];
120static unsigned int decode_timeout_val = 100;
121#define INCPTR(p) ptr_atomic_wrap_inc(&p)
122
123#define DEC_CONTROL_FLAG_FORCE_2500_720_576_INTERLACE 0x0002
124#define DEC_CONTROL_FLAG_FORCE_3000_704_480_INTERLACE 0x0004
125#define DEC_CONTROL_FLAG_FORCE_2500_704_576_INTERLACE 0x0008
126#define DEC_CONTROL_FLAG_FORCE_2500_544_576_INTERLACE 0x0010
127#define DEC_CONTROL_FLAG_FORCE_2500_480_576_INTERLACE 0x0020
128#define DEC_CONTROL_INTERNAL_MASK 0x0fff
129#define DEC_CONTROL_FLAG_FORCE_SEQ_INTERLACE 0x1000
130
131#define INTERLACE_SEQ_ALWAYS
132
133#if 1/* MESON_CPU_TYPE >= MESON_CPU_TYPE_MESON6 */
134#define NV21
135#endif
136
137#define AGAIN_HAS_THRESHOLD
138
139#ifdef AGAIN_HAS_THRESHOLD
140u32 again_threshold;
141#endif
142
143/*
144#define DUMP_USER_DATA
145*/
146
147enum {
148 FRAME_REPEAT_TOP,
149 FRAME_REPEAT_BOT,
150 FRAME_REPEAT_NONE
151};
152#define DEC_RESULT_NONE 0
153#define DEC_RESULT_DONE 1
154#define DEC_RESULT_AGAIN 2
155#define DEC_RESULT_ERROR 3
156#define DEC_RESULT_FORCE_EXIT 4
157#define DEC_RESULT_EOS 5
158#define DEC_RESULT_GET_DATA 6
159#define DEC_RESULT_GET_DATA_RETRY 7
160
161#define DEC_DECODE_TIMEOUT 0x21
162#define DECODE_ID(hw) (hw_to_vdec(hw)->id)
163#define DECODE_STOP_POS AV_SCRATCH_K
164
165static struct vframe_s *vmpeg_vf_peek(void *);
166static struct vframe_s *vmpeg_vf_get(void *);
167static void vmpeg_vf_put(struct vframe_s *, void *);
168static int vmpeg_vf_states(struct vframe_states *states, void *);
169static int vmpeg_event_cb(int type, void *data, void *private_data);
170
171struct mmpeg2_userdata_record_t {
172 struct userdata_meta_info_t meta_info;
173 u32 rec_start;
174 u32 rec_len;
175};
176
177#define USERDATA_FIFO_NUM 256
178#define MAX_FREE_USERDATA_NODES 5
179
180struct mmpeg2_userdata_info_t {
181 struct mmpeg2_userdata_record_t records[USERDATA_FIFO_NUM];
182 u8 *data_buf;
183 u8 *data_buf_end;
184 u32 buf_len;
185 u32 read_index;
186 u32 write_index;
187 u32 last_wp;
188};
189#define MAX_UD_RECORDS 5
190
191struct pic_info_t {
192 u32 buffer_info;
193 u32 index;
194 u32 offset;
195 u32 width;
196 u32 height;
197 u32 pts;
198 u64 pts64;
199 bool pts_valid;
200};
201
202struct vdec_mpeg12_hw_s {
203 spinlock_t lock;
204 struct platform_device *platform_dev;
205 DECLARE_KFIFO(newframe_q, struct vframe_s *, VF_POOL_SIZE);
206 DECLARE_KFIFO(display_q, struct vframe_s *, VF_POOL_SIZE);
207 struct vframe_s vfpool[VF_POOL_SIZE];
208 s32 vfbuf_use[DECODE_BUFFER_NUM_MAX];
209 u32 frame_width;
210 u32 frame_height;
211 u32 frame_dur;
212 u32 frame_prog;
213 u32 seqinfo;
214 u32 ctx_valid;
215 u32 dec_control;
216 void *mm_blk_handle;
217 struct vframe_chunk_s *chunk;
218 u32 stat;
219 u8 init_flag;
220 unsigned long buf_start;
221 u32 buf_size;
222 u32 reg_pic_width;
223 u32 reg_pic_height;
224 u32 reg_mpeg1_2_reg;
225 u32 reg_pic_head_info;
226 u32 reg_f_code_reg;
227 u32 reg_slice_ver_pos_pic_type;
228 u32 reg_vcop_ctrl_reg;
229 u32 reg_mb_info;
230 u32 reg_signal_type;
231 u32 dec_num;
232 u32 disp_num;
233 struct timer_list check_timer;
234 u32 decode_timeout_count;
235 unsigned long int start_process_time;
236 u32 last_vld_level;
237 u32 eos;
238 struct pic_info_t pics[DECODE_BUFFER_NUM_MAX];
239 u32 canvas_spec[DECODE_BUFFER_NUM_MAX];
240 u64 lastpts64;
241 u32 last_chunk_pts;
242 struct canvas_config_s canvas_config[DECODE_BUFFER_NUM_MAX][2];
243 struct dec_sysinfo vmpeg12_amstream_dec_info;
244
245 s32 refs[2];
246 int dec_result;
247 struct work_struct work;
248 struct work_struct notify_work;
249 void (*vdec_cb)(struct vdec_s *, void *);
250 void *vdec_cb_arg;
251 unsigned long ccbuf_phyAddress;
252 void *ccbuf_phyAddress_virt;
253 unsigned long ccbuf_phyAddress_is_remaped_nocache;
254 u32 frame_rpt_state;
255/* for error handling */
256 s32 frame_force_skip_flag;
257 s32 error_frame_skip_level;
258 s32 wait_buffer_counter;
259 u32 first_i_frame_ready;
260 u32 run_count;
261 u32 not_run_ready;
262 u32 input_empty;
263 u32 put_num;
264 u32 peek_num;
265 u32 get_num;
266 u32 drop_frame_count;
267 u32 buffer_not_ready;
268 u32 ratio_control;
269 int frameinfo_enable;
270 struct firmware_s *fw;
271 u32 canvas_mode;
272#ifdef AGAIN_HAS_THRESHOLD
273 u32 pre_parser_wr_ptr;
274 u8 next_again_flag;
275#endif
276 struct work_struct userdata_push_work;
277 struct mutex userdata_mutex;
278 struct mmpeg2_userdata_info_t userdata_info;
279 struct mmpeg2_userdata_record_t ud_record[MAX_UD_RECORDS];
280 int cur_ud_idx;
281 u8 *user_data_buffer;
282 int wait_for_udr_send;
283 u32 ucode_cc_last_wp;
284 u32 notify_ucode_cc_last_wp;
285 u32 notify_data_cc_last_wp;
286 u32 userdata_wp_ctx;
287#ifdef DUMP_USER_DATA
288#define MAX_USER_DATA_SIZE 1572864
289 void *user_data_dump_buf;
290 unsigned char *pdump_buf_cur_start;
291 int total_len;
292 int bskip;
293 int n_userdata_id;
294 u32 reference[MAX_UD_RECORDS];
295#endif
296 int tvp_flag;
297};
298static void vmpeg12_local_init(struct vdec_mpeg12_hw_s *hw);
299static int vmpeg12_hw_ctx_restore(struct vdec_mpeg12_hw_s *hw);
300static void reset_process_time(struct vdec_mpeg12_hw_s *hw);
301static struct vdec_info gvs;
302static int debug_enable;
303/*static struct work_struct userdata_push_work;*/
304#undef pr_info
305#define pr_info printk
306unsigned int mpeg12_debug_mask = 0xff;
307/*static int counter_max = 5;*/
308
309#define PRINT_FLAG_ERROR 0x0
310#define PRINT_FLAG_RUN_FLOW 0X0001
311#define PRINT_FLAG_TIMEINFO 0x0002
312#define PRINT_FLAG_UCODE_DETAIL 0x0004
313#define PRINT_FLAG_VLD_DETAIL 0x0008
314#define PRINT_FLAG_DEC_DETAIL 0x0010
315#define PRINT_FLAG_BUFFER_DETAIL 0x0020
316#define PRINT_FLAG_RESTORE 0x0040
317#define PRINT_FRAME_NUM 0x0080
318#define PRINT_FLAG_FORCE_DONE 0x0100
319#define PRINT_FLAG_COUNTER 0X0200
320#define PRINT_FRAMEBASE_DATA 0x0400
321#define PRINT_FLAG_VDEC_STATUS 0x0800
322#define PRINT_FLAG_PARA_DATA 0x1000
323#define PRINT_FLAG_USERDATA_DETAIL 0x2000
324#define PRINT_FLAG_TIMEOUT_STATUS 0x4000
325
326
327
328int debug_print(int index, int debug_flag, const char *fmt, ...)
329{
330 if (((debug_enable & debug_flag) &&
331 ((1 << index) & mpeg12_debug_mask))
332 || (debug_flag == PRINT_FLAG_ERROR)) {
333 unsigned char buf[512];
334 int len = 0;
335 va_list args;
336 va_start(args, fmt);
337 len = sprintf(buf, "%d: ", index);
338 vsnprintf(buf + len, 512-len, fmt, args);
339 pr_info("%s", buf);
340 va_end(args);
341 }
342 return 0;
343}
344
345
346/*static bool is_reset;*/
347#define PROVIDER_NAME "vdec.mpeg12"
348static const struct vframe_operations_s vf_provider_ops = {
349 .peek = vmpeg_vf_peek,
350 .get = vmpeg_vf_get,
351 .put = vmpeg_vf_put,
352 .event_cb = vmpeg_event_cb,
353 .vf_states = vmpeg_vf_states,
354};
355
356
357static const u32 frame_rate_tab[16] = {
358 96000 / 30, 96000000 / 23976, 96000 / 24, 96000 / 25,
359 9600000 / 2997, 96000 / 30, 96000 / 50, 9600000 / 5994,
360 96000 / 60,
361 /* > 8 reserved, use 24 */
362 96000 / 24, 96000 / 24, 96000 / 24, 96000 / 24,
363 96000 / 24, 96000 / 24, 96000 / 24
364};
365
366
367static u32 find_buffer(struct vdec_mpeg12_hw_s *hw)
368{
369 u32 i;
370
371 for (i = 0; i < DECODE_BUFFER_NUM_MAX; i++) {
372 if (hw->vfbuf_use[i] == 0)
373 return i;
374 }
375
376 return DECODE_BUFFER_NUM_MAX;
377}
378
379static u32 spec_to_index(struct vdec_mpeg12_hw_s *hw, u32 spec)
380{
381 u32 i;
382
383 for (i = 0; i < DECODE_BUFFER_NUM_MAX; i++) {
384 if (hw->canvas_spec[i] == spec)
385 return i;
386 }
387
388 return DECODE_BUFFER_NUM_MAX;
389}
390
391static void set_frame_info(struct vdec_mpeg12_hw_s *hw, struct vframe_s *vf)
392{
393 u32 ar_bits;
394 u32 buffer_index = vf->index;
395
396 vf->width = hw->pics[buffer_index].width;
397 vf->height = hw->pics[buffer_index].height;
398
399 if (hw->frame_dur > 0)
400 vf->duration = hw->frame_dur;
401 else {
402 vf->duration = hw->frame_dur =
403 frame_rate_tab[(READ_VREG(MREG_SEQ_INFO) >> 4) & 0xf];
404 vdec_schedule_work(&hw->notify_work);
405 }
406
407 ar_bits = READ_VREG(MREG_SEQ_INFO) & 0xf;
408
409 if (ar_bits == 0x2)
410 vf->ratio_control = 0xc0 << DISP_RATIO_ASPECT_RATIO_BIT;
411
412 else if (ar_bits == 0x3)
413 vf->ratio_control = 0x90 << DISP_RATIO_ASPECT_RATIO_BIT;
414
415 else if (ar_bits == 0x4)
416 vf->ratio_control = 0x74 << DISP_RATIO_ASPECT_RATIO_BIT;
417 else
418 vf->ratio_control = 0;
419
420 hw->ratio_control = vf->ratio_control;
421
422 vf->canvas0Addr = vf->canvas1Addr = -1;
423 vf->plane_num = 2;
424
425 vf->canvas0_config[0] = hw->canvas_config[buffer_index][0];
426 vf->canvas0_config[1] = hw->canvas_config[buffer_index][1];
427
428 vf->canvas1_config[0] = hw->canvas_config[buffer_index][0];
429 vf->canvas1_config[1] = hw->canvas_config[buffer_index][1];
430
431 debug_print(DECODE_ID(hw), PRINT_FLAG_PARA_DATA,
432 "mpeg2dec: w(%d), h(%d), dur(%d), dur-ES(%d)\n",
433 hw->frame_width, hw->frame_height, hw->frame_dur,
434 frame_rate_tab[(READ_VREG(MREG_SEQ_INFO) >> 4) & 0xf]);
435}
436
437static bool error_skip(struct vdec_mpeg12_hw_s *hw,
438 u32 info, struct vframe_s *vf)
439{
440 if (hw->error_frame_skip_level) {
441 /* skip error frame */
442 if ((info & PICINFO_ERROR) || (hw->frame_force_skip_flag)) {
443 if ((info & PICINFO_ERROR) == 0) {
444 if ((info & PICINFO_TYPE_MASK) ==
445 PICINFO_TYPE_I)
446 hw->frame_force_skip_flag = 0;
447 } else {
448 if (hw->error_frame_skip_level >= 2)
449 hw->frame_force_skip_flag = 1;
450 }
451 if ((info & PICINFO_ERROR)
452 || (hw->frame_force_skip_flag))
453 return true;
454 }
455 }
456 return false;
457}
458
459static inline void vmpeg12_save_hw_context(struct vdec_mpeg12_hw_s *hw, u32 reg)
460{
461 if (reg == 3) {
462 hw->ctx_valid = 0;
463 //pr_info("%s, hw->userdata_wp_ctx %d\n", __func__, hw->userdata_wp_ctx);
464 } else {
465 hw->seqinfo = READ_VREG(MREG_SEQ_INFO);
466 hw->reg_pic_width = READ_VREG(MREG_PIC_WIDTH);
467 hw->reg_pic_height = READ_VREG(MREG_PIC_HEIGHT);
468 hw->reg_mpeg1_2_reg = READ_VREG(MPEG1_2_REG);
469 hw->reg_pic_head_info = READ_VREG(PIC_HEAD_INFO);
470 hw->reg_f_code_reg = READ_VREG(F_CODE_REG);
471 hw->reg_slice_ver_pos_pic_type = READ_VREG(SLICE_VER_POS_PIC_TYPE);
472 hw->reg_vcop_ctrl_reg = READ_VREG(VCOP_CTRL_REG);
473 hw->reg_mb_info = READ_VREG(MB_INFO);
474 hw->reg_signal_type = READ_VREG(AV_SCRATCH_H);
475 debug_print(DECODE_ID(hw), PRINT_FLAG_PARA_DATA,
476 "signal_type = %x", hw->reg_signal_type);
477 hw->ctx_valid = 1;
478 }
479}
480
481static void vmmpeg2_reset_udr_mgr(struct vdec_mpeg12_hw_s *hw)
482{
483 hw->wait_for_udr_send = 0;
484 hw->cur_ud_idx = 0;
485 memset(&hw->ud_record, 0, sizeof(hw->ud_record));
486}
487
488static void vmmpeg2_crate_userdata_manager(
489 struct vdec_mpeg12_hw_s *hw,
490 u8 *userdata_buf,
491 int buf_len)
492{
493 if (hw) {
494 mutex_init(&hw->userdata_mutex);
495
496 memset(&hw->userdata_info, 0,
497 sizeof(struct mmpeg2_userdata_info_t));
498 hw->userdata_info.data_buf = userdata_buf;
499 hw->userdata_info.buf_len = buf_len;
500 hw->userdata_info.data_buf_end = userdata_buf + buf_len;
501 hw->userdata_wp_ctx = 0;
502
503 vmmpeg2_reset_udr_mgr(hw);
504 }
505}
506
507static void vmmpeg2_destroy_userdata_manager(struct vdec_mpeg12_hw_s *hw)
508{
509 if (hw)
510 memset(&hw->userdata_info,
511 0,
512 sizeof(struct mmpeg2_userdata_info_t));
513}
514
515static void aml_swap_data(uint8_t *user_data, int ud_size)
516{
517 int swap_blocks, i, j, k, m;
518 unsigned char c_temp;
519
520 /* swap byte order */
521 swap_blocks = ud_size / 8;
522 for (i = 0; i < swap_blocks; i++) {
523 j = i * 8;
524 k = j + 7;
525 for (m = 0; m < 4; m++) {
526 c_temp = user_data[j];
527 user_data[j++] = user_data[k];
528 user_data[k--] = c_temp;
529 }
530 }
531}
532
533#ifdef DUMP_USER_DATA
534static void push_to_buf(struct vdec_mpeg12_hw_s *hw,
535 u8 *pdata,
536 int len,
537 struct userdata_meta_info_t *pmeta,
538 u32 reference)
539{
540 u32 *pLen;
541 int info_cnt;
542 u8 *pbuf_end;
543
544 if (!hw->user_data_dump_buf)
545 return;
546
547 if (hw->bskip) {
548 pr_info("over size, skip\n");
549 return;
550 }
551 info_cnt = 0;
552 pLen = (u32 *)hw->pdump_buf_cur_start;
553
554 *pLen = len;
555 hw->pdump_buf_cur_start += sizeof(u32);
556 info_cnt++;
557 pLen++;
558
559 *pLen = pmeta->duration;
560 hw->pdump_buf_cur_start += sizeof(u32);
561 info_cnt++;
562 pLen++;
563
564 *pLen = pmeta->flags;
565 hw->pdump_buf_cur_start += sizeof(u32);
566 info_cnt++;
567 pLen++;
568
569 *pLen = pmeta->vpts;
570 hw->pdump_buf_cur_start += sizeof(u32);
571 info_cnt++;
572 pLen++;
573
574 *pLen = pmeta->vpts_valid;
575 hw->pdump_buf_cur_start += sizeof(u32);
576 info_cnt++;
577 pLen++;
578
579
580 *pLen = hw->n_userdata_id;
581 hw->pdump_buf_cur_start += sizeof(u32);
582 info_cnt++;
583 pLen++;
584
585 *pLen = reference;
586 hw->pdump_buf_cur_start += sizeof(u32);
587 info_cnt++;
588 pLen++;
589
590 pbuf_end = hw->userdata_info.data_buf_end;
591 if (pdata + len > pbuf_end) {
592 int first_section_len;
593
594 first_section_len = pbuf_end - pdata;
595 memcpy(hw->pdump_buf_cur_start, pdata, first_section_len);
596 pdata = (u8 *)hw->userdata_info.data_buf;
597 hw->pdump_buf_cur_start += first_section_len;
598 memcpy(hw->pdump_buf_cur_start, pdata, len - first_section_len);
599 hw->pdump_buf_cur_start += len - first_section_len;
600 } else {
601 memcpy(hw->pdump_buf_cur_start, pdata, len);
602 hw->pdump_buf_cur_start += len;
603 }
604
605 hw->total_len += len + info_cnt * sizeof(u32);
606 if (hw->total_len >= MAX_USER_DATA_SIZE-4096)
607 hw->bskip = 1;
608}
609
610static void dump_userdata_info(struct vdec_mpeg12_hw_s *hw,
611 void *puser_data,
612 int len,
613 struct userdata_meta_info_t *pmeta,
614 u32 reference)
615{
616 u8 *pstart;
617
618 pstart = (u8 *)puser_data;
619
620#ifdef DUMP_HEAD_INFO_DATA
621 push_to_buf(hw, pstart, len, pmeta, reference);
622#else
623 push_to_buf(hw, pstart+8, len - 8, pmeta, reference);
624#endif
625}
626
627
628static void print_data(unsigned char *pdata,
629 int len,
630 unsigned int flag,
631 unsigned int duration,
632 unsigned int vpts,
633 unsigned int vpts_valid,
634 int rec_id,
635 u32 reference)
636{
637 int nLeft;
638
639 nLeft = len;
640
641 pr_info("%d len:%d, flag:0x%x, dur:%d, vpts:0x%x, valid:%d, refer:%d\n",
642 rec_id, len, flag,
643 duration, vpts, vpts_valid,
644 reference);
645 while (nLeft >= 16) {
646 pr_info("%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
647 pdata[0], pdata[1], pdata[2], pdata[3],
648 pdata[4], pdata[5], pdata[6], pdata[7],
649 pdata[8], pdata[9], pdata[10], pdata[11],
650 pdata[12], pdata[13], pdata[14], pdata[15]);
651 nLeft -= 16;
652 pdata += 16;
653 }
654
655
656 while (nLeft > 0) {
657 pr_info("%02x %02x %02x %02x %02x %02x %02x %02x\n",
658 pdata[0], pdata[1], pdata[2], pdata[3],
659 pdata[4], pdata[5], pdata[6], pdata[7]);
660 nLeft -= 8;
661 pdata += 8;
662 }
663}
664
665static void dump_data(u8 *pdata,
666 unsigned int user_data_length,
667 unsigned int flag,
668 unsigned int duration,
669 unsigned int vpts,
670 unsigned int vpts_valid,
671 int rec_id,
672 u32 reference)
673{
674 unsigned char szBuf[256];
675
676
677 memset(szBuf, 0, 256);
678 memcpy(szBuf, pdata, user_data_length);
679
680 aml_swap_data(szBuf, user_data_length);
681
682 print_data(szBuf,
683 user_data_length,
684 flag,
685 duration,
686 vpts,
687 vpts_valid,
688 rec_id,
689 reference);
690}
691
692
693static void show_user_data_buf(struct vdec_mpeg12_hw_s *hw)
694{
695 u8 *pbuf;
696 int len;
697 unsigned int flag;
698 unsigned int duration;
699 unsigned int vpts;
700 unsigned int vpts_valid;
701 int rec_id;
702 u32 reference;
703
704 pr_info("show user data buf\n");
705 pbuf = hw->user_data_dump_buf;
706
707 while (pbuf < hw->pdump_buf_cur_start) {
708 u32 *pLen;
709
710 pLen = (u32 *)pbuf;
711
712 len = *pLen;
713 pLen++;
714 pbuf += sizeof(u32);
715
716 duration = *pLen;
717 pLen++;
718 pbuf += sizeof(u32);
719
720 flag = *pLen;
721 pLen++;
722 pbuf += sizeof(u32);
723
724 vpts = *pLen;
725 pLen++;
726 pbuf += sizeof(u32);
727
728 vpts_valid = *pLen;
729 pLen++;
730 pbuf += sizeof(u32);
731
732 rec_id = *pLen;
733 pLen++;
734 pbuf += sizeof(u32);
735
736 reference = *pLen;
737 pLen++;
738 pbuf += sizeof(u32);
739
740
741 dump_data(pbuf, len, flag, duration,
742 vpts, vpts_valid, rec_id, reference);
743 pbuf += len;
744 msleep(30);
745 }
746}
747
748static int amvdec_mmpeg12_init_userdata_dump(struct vdec_mpeg12_hw_s *hw)
749{
750 hw->user_data_dump_buf = kmalloc(MAX_USER_DATA_SIZE, GFP_KERNEL);
751 if (hw->user_data_dump_buf)
752 return 1;
753 else
754 return 0;
755}
756
757static void amvdec_mmpeg12_uninit_userdata_dump(struct vdec_mpeg12_hw_s *hw)
758{
759 if (hw->user_data_dump_buf) {
760 show_user_data_buf(hw);
761 kfree(hw->user_data_dump_buf);
762 hw->user_data_dump_buf = NULL;
763 }
764}
765
766static void reset_user_data_buf(struct vdec_mpeg12_hw_s *hw)
767{
768 hw->total_len = 0;
769 hw->pdump_buf_cur_start = hw->user_data_dump_buf;
770 hw->bskip = 0;
771 hw->n_userdata_id = 0;
772}
773#endif
774
775static void user_data_ready_notify(struct vdec_mpeg12_hw_s *hw,
776 u32 pts, u32 pts_valid)
777{
778 struct mmpeg2_userdata_record_t *p_userdata_rec;
779 int i;
780
781 if (hw->wait_for_udr_send) {
782 for (i = 0; i < hw->cur_ud_idx; i++) {
783 mutex_lock(&hw->userdata_mutex);
784
785
786 p_userdata_rec = hw->userdata_info.records
787 + hw->userdata_info.write_index;
788
789 hw->ud_record[i].meta_info.vpts_valid = pts_valid;
790 hw->ud_record[i].meta_info.vpts = pts;
791
792 *p_userdata_rec = hw->ud_record[i];
793#ifdef DUMP_USER_DATA
794 dump_userdata_info(hw,
795 hw->userdata_info.data_buf + p_userdata_rec->rec_start,
796 p_userdata_rec->rec_len,
797 &p_userdata_rec->meta_info,
798 hw->reference[i]);
799 hw->n_userdata_id++;
800#endif
801/*
802 pr_info("notify: rec_start:%d, rec_len:%d, wi:%d, reference:%d\n",
803 p_userdata_rec->rec_start,
804 p_userdata_rec->rec_len,
805 hw->userdata_info.write_index,
806 hw->reference[i]);
807*/
808 hw->userdata_info.write_index++;
809 if (hw->userdata_info.write_index >= USERDATA_FIFO_NUM)
810 hw->userdata_info.write_index = 0;
811
812 mutex_unlock(&hw->userdata_mutex);
813
814
815 vdec_wakeup_userdata_poll(hw_to_vdec(hw));
816 }
817 hw->wait_for_udr_send = 0;
818 hw->cur_ud_idx = 0;
819 }
820 hw->notify_ucode_cc_last_wp = hw->ucode_cc_last_wp;
821 hw->notify_data_cc_last_wp = hw->userdata_info.last_wp;
822}
823
824static int vmmpeg2_user_data_read(struct vdec_s *vdec,
825 struct userdata_param_t *puserdata_para)
826{
827 struct vdec_mpeg12_hw_s *hw = NULL;
828 int rec_ri, rec_wi;
829 int rec_len;
830 u8 *rec_data_start;
831 u8 *pdest_buf;
832 struct mmpeg2_userdata_record_t *p_userdata_rec;
833 u32 data_size;
834 u32 res;
835 int copy_ok = 1;
836
837 hw = (struct vdec_mpeg12_hw_s *)vdec->private;
838
839 pdest_buf = puserdata_para->pbuf_addr;
840
841 mutex_lock(&hw->userdata_mutex);
842
843/*
844 pr_info("ri = %d, wi = %d\n",
845 hw->userdata_info.read_index,
846 hw->userdata_info.write_index);
847*/
848 rec_ri = hw->userdata_info.read_index;
849 rec_wi = hw->userdata_info.write_index;
850
851 if (rec_ri == rec_wi) {
852 mutex_unlock(&hw->userdata_mutex);
853 return 0;
854 }
855
856 p_userdata_rec = hw->userdata_info.records + rec_ri;
857
858 rec_len = p_userdata_rec->rec_len;
859 rec_data_start = p_userdata_rec->rec_start + hw->userdata_info.data_buf;
860/*
861 pr_info("ri:%d, wi:%d, rec_len:%d, rec_start:%d, buf_len:%d\n",
862 rec_ri, rec_wi,
863 p_userdata_rec->rec_len,
864 p_userdata_rec->rec_start,
865 puserdata_para->buf_len);
866*/
867 if (rec_len <= puserdata_para->buf_len) {
868 /* dvb user data buffer is enought to
869 copy the whole recored. */
870 data_size = rec_len;
871 if (rec_data_start + data_size
872 > hw->userdata_info.data_buf_end) {
873 int first_section_len;
874
875 first_section_len = hw->userdata_info.buf_len -
876 p_userdata_rec->rec_start;
877 res = (u32)copy_to_user((void *)pdest_buf,
878 (void *)rec_data_start,
879 first_section_len);
880 if (res) {
881 pr_info("p1 read not end res=%d, request=%d\n",
882 res, first_section_len);
883 copy_ok = 0;
884
885 p_userdata_rec->rec_len -=
886 first_section_len - res;
887 p_userdata_rec->rec_start +=
888 first_section_len - res;
889 puserdata_para->data_size =
890 first_section_len - res;
891 } else {
892 res = (u32)copy_to_user(
893 (void *)(pdest_buf+first_section_len),
894 (void *)hw->userdata_info.data_buf,
895 data_size - first_section_len);
896 if (res) {
897 pr_info("p2 read not end res=%d, request=%d\n",
898 res, data_size);
899 copy_ok = 0;
900 }
901 p_userdata_rec->rec_len -=
902 data_size - res;
903 p_userdata_rec->rec_start =
904 data_size - first_section_len - res;
905 puserdata_para->data_size =
906 data_size - res;
907 }
908 } else {
909 res = (u32)copy_to_user((void *)pdest_buf,
910 (void *)rec_data_start,
911 data_size);
912 if (res) {
913 pr_info("p3 read not end res=%d, request=%d\n",
914 res, data_size);
915 copy_ok = 0;
916 }
917 p_userdata_rec->rec_len -= data_size - res;
918 p_userdata_rec->rec_start += data_size - res;
919 puserdata_para->data_size = data_size - res;
920 }
921
922 if (copy_ok) {
923 hw->userdata_info.read_index++;
924 if (hw->userdata_info.read_index >= USERDATA_FIFO_NUM)
925 hw->userdata_info.read_index = 0;
926 }
927 } else {
928 /* dvb user data buffer is not enought
929 to copy the whole recored. */
930 data_size = puserdata_para->buf_len;
931 if (rec_data_start + data_size
932 > hw->userdata_info.data_buf_end) {
933 int first_section_len;
934
935 first_section_len = hw->userdata_info.buf_len -
936 p_userdata_rec->rec_start;
937 res = (u32)copy_to_user((void *)pdest_buf,
938 (void *)rec_data_start,
939 first_section_len);
940 if (res) {
941 pr_info("p4 read not end res=%d, request=%d\n",
942 res, first_section_len);
943 copy_ok = 0;
944 p_userdata_rec->rec_len -=
945 first_section_len - res;
946 p_userdata_rec->rec_start +=
947 first_section_len - res;
948 puserdata_para->data_size =
949 first_section_len - res;
950 } else {
951 /* first secton copy is ok*/
952 res = (u32)copy_to_user(
953 (void *)(pdest_buf+first_section_len),
954 (void *)hw->userdata_info.data_buf,
955 data_size - first_section_len);
956 if (res) {
957 pr_info("p5 read not end res=%d, request=%d\n",
958 res,
959 data_size - first_section_len);
960 copy_ok = 0;
961 }
962
963 p_userdata_rec->rec_len -=
964 data_size - res;
965 p_userdata_rec->rec_start =
966 data_size - first_section_len - res;
967 puserdata_para->data_size =
968 data_size - res;
969 }
970 } else {
971 res = (u32)copy_to_user((void *)pdest_buf,
972 (void *)rec_data_start,
973 data_size);
974 if (res) {
975 pr_info("p6 read not end res=%d, request=%d\n",
976 res, data_size);
977 copy_ok = 0;
978 }
979
980 p_userdata_rec->rec_len -= data_size - res;
981 p_userdata_rec->rec_start += data_size - res;
982 puserdata_para->data_size = data_size - res;
983 }
984
985 if (copy_ok) {
986 hw->userdata_info.read_index++;
987 if (hw->userdata_info.read_index >= USERDATA_FIFO_NUM)
988 hw->userdata_info.read_index = 0;
989 }
990
991 }
992 puserdata_para->meta_info = p_userdata_rec->meta_info;
993
994 if (hw->userdata_info.read_index <= hw->userdata_info.write_index)
995 puserdata_para->meta_info.records_in_que =
996 hw->userdata_info.write_index -
997 hw->userdata_info.read_index;
998 else
999 puserdata_para->meta_info.records_in_que =
1000 hw->userdata_info.write_index +
1001 USERDATA_FIFO_NUM -
1002 hw->userdata_info.read_index;
1003
1004 puserdata_para->version = (0<<24|0<<16|0<<8|1);
1005
1006 mutex_unlock(&hw->userdata_mutex);
1007
1008
1009 return 1;
1010}
1011
1012static void vmmpeg2_reset_userdata_fifo(struct vdec_s *vdec, int bInit)
1013{
1014 struct vdec_mpeg12_hw_s *hw = NULL;
1015
1016 hw = (struct vdec_mpeg12_hw_s *)vdec->private;
1017
1018 if (hw) {
1019 mutex_lock(&hw->userdata_mutex);
1020 pr_info("vmh264_reset_userdata_fifo: bInit: %d, ri: %d, wi: %d\n",
1021 bInit,
1022 hw->userdata_info.read_index,
1023 hw->userdata_info.write_index);
1024 hw->userdata_info.read_index = 0;
1025 hw->userdata_info.write_index = 0;
1026
1027 if (bInit)
1028 hw->userdata_info.last_wp = 0;
1029 mutex_unlock(&hw->userdata_mutex);
1030 }
1031}
1032
1033static void vmmpeg2_wakeup_userdata_poll(struct vdec_s *vdec)
1034{
1035 amstream_wakeup_userdata_poll(vdec);
1036}
1037
1038/*
1039#define PRINT_HEAD_INFO
1040*/
1041static void userdata_push_do_work(struct work_struct *work)
1042{
1043 u32 reg;
1044 u8 *pdata;
1045 u8 *psrc_data;
1046 u8 head_info[8];
1047 struct userdata_meta_info_t meta_info;
1048 u32 wp;
1049 u32 index;
1050 u32 picture_struct;
1051 u32 reference;
1052 u32 picture_type;
1053 u32 temp;
1054 u32 data_length;
1055 u32 data_start;
1056 int i;
1057 u32 offset;
1058 u32 cur_wp;
1059#ifdef PRINT_HEAD_INFO
1060 u8 *ptype_str;
1061#endif
1062 struct mmpeg2_userdata_record_t *pcur_ud_rec;
1063
1064 struct vdec_mpeg12_hw_s *hw = container_of(work,
1065 struct vdec_mpeg12_hw_s, userdata_push_work);
1066
1067 memset(&meta_info, 0, sizeof(meta_info));
1068
1069 meta_info.duration = hw->frame_dur;
1070
1071
1072 reg = READ_VREG(AV_SCRATCH_J);
1073 hw->userdata_wp_ctx = reg;
1074 meta_info.flags = ((reg >> 30) << 1);
1075 meta_info.flags |= (VFORMAT_MPEG12 << 3);
1076 /* check top_field_first flag */
1077 if ((reg >> 28) & 0x1) {
1078 meta_info.flags |= (1 << 10);
1079 meta_info.flags |= (((reg >> 29) & 0x1) << 11);
1080 }
1081
1082 cur_wp = reg & 0x7fff;
1083 if (cur_wp == hw->ucode_cc_last_wp) {
1084 debug_print(DECODE_ID(hw), 0,
1085 "Null user data package: wp = %d\n", cur_wp);
1086 WRITE_VREG(AV_SCRATCH_J, 0);
1087 return;
1088 }
1089
1090 if (hw->cur_ud_idx >= MAX_UD_RECORDS) {
1091 debug_print(DECODE_ID(hw), 0,
1092 "UD Records over: %d, skip it\n", MAX_UD_RECORDS);
1093 WRITE_VREG(AV_SCRATCH_J, 0);
1094 return;
1095 }
1096
1097 if (cur_wp < hw->ucode_cc_last_wp)
1098 hw->ucode_cc_last_wp = 0;
1099
1100 offset = READ_VREG(AV_SCRATCH_I);
1101
1102 codec_mm_dma_flush(
1103 hw->ccbuf_phyAddress_virt,
1104 CCBUF_SIZE,
1105 DMA_FROM_DEVICE);
1106
1107 mutex_lock(&hw->userdata_mutex);
1108 if (hw->ccbuf_phyAddress_virt) {
1109 pdata = (u8 *)hw->ccbuf_phyAddress_virt + hw->ucode_cc_last_wp;
1110 memcpy(head_info, pdata, 8);
1111 } else
1112 memset(head_info, 0, 8);
1113 mutex_unlock(&hw->userdata_mutex);
1114 aml_swap_data(head_info, 8);
1115
1116 wp = (head_info[0] << 8 | head_info[1]);
1117 index = (head_info[2] << 8 | head_info[3]);
1118
1119 picture_struct = (head_info[6] << 8 | head_info[7]);
1120 temp = (head_info[4] << 8 | head_info[5]);
1121 reference = temp & 0x3FF;
1122 picture_type = (temp >> 10) & 0x7;
1123
1124 if (debug_enable & PRINT_FLAG_USERDATA_DETAIL)
1125 pr_info("index:%d, wp:%d, ref:%d, type:%d, struct:0x%x, u_last_wp:0x%x\n",
1126 index, wp, reference,
1127 picture_type, picture_struct,
1128 hw->ucode_cc_last_wp);
1129
1130 switch (picture_type) {
1131 case 1:
1132 /* pr_info("I type, pos:%d\n",
1133 (meta_info.flags>>1)&0x3); */
1134 meta_info.flags |= (1<<7);
1135#ifdef PRINT_HEAD_INFO
1136 ptype_str = " I";
1137#endif
1138 break;
1139 case 2:
1140 /* pr_info("P type, pos:%d\n",
1141 (meta_info.flags>>1)&0x3); */
1142 meta_info.flags |= (2<<7);
1143#ifdef PRINT_HEAD_INFO
1144 ptype_str = " P";
1145#endif
1146 break;
1147 case 3:
1148 /* pr_info("B type, pos:%d\n",
1149 (meta_info.flags>>1)&0x3); */
1150 meta_info.flags |= (3<<7);
1151#ifdef PRINT_HEAD_INFO
1152 ptype_str = " B";
1153#endif
1154 break;
1155 case 4:
1156 /* pr_info("D type, pos:%d\n",
1157 (meta_info.flags>>1)&0x3); */
1158 meta_info.flags |= (4<<7);
1159#ifdef PRINT_HEAD_INFO
1160 ptype_str = " D";
1161#endif
1162 break;
1163 default:
1164 /* pr_info("Unknown type:0x%x, pos:%d\n",
1165 pheader->picture_coding_type,
1166 (meta_info.flags>>1)&0x3); */
1167#ifdef PRINT_HEAD_INFO
1168 ptype_str = " U";
1169#endif
1170 break;
1171 }
1172#ifdef PRINT_HEAD_INFO
1173 pr_info("ref:%d, type:%s, ext:%d, first:%d, data_length:%d\n",
1174 reference, ptype_str,
1175 (reg >> 30),
1176 (reg >> 28)&0x3,
1177 reg & 0xffff);
1178#endif
1179 data_length = cur_wp - hw->ucode_cc_last_wp;
1180 data_start = reg & 0xffff;
1181 psrc_data = (u8 *)hw->ccbuf_phyAddress_virt + hw->ucode_cc_last_wp;
1182
1183 pdata = hw->userdata_info.data_buf + hw->userdata_info.last_wp;
1184 for (i = 0; i < data_length; i++) {
1185 *pdata++ = *psrc_data++;
1186 if (pdata >= hw->userdata_info.data_buf_end)
1187 pdata = hw->userdata_info.data_buf;
1188 }
1189 pcur_ud_rec = hw->ud_record + hw->cur_ud_idx;
1190
1191 pcur_ud_rec->meta_info = meta_info;
1192 pcur_ud_rec->rec_start = hw->userdata_info.last_wp;
1193 pcur_ud_rec->rec_len = data_length;
1194
1195 hw->userdata_info.last_wp += data_length;
1196 if (hw->userdata_info.last_wp >= USER_DATA_SIZE)
1197 hw->userdata_info.last_wp -= USER_DATA_SIZE;
1198
1199 hw->wait_for_udr_send = 1;
1200
1201 hw->ucode_cc_last_wp = cur_wp;
1202
1203 if (debug_enable & PRINT_FLAG_USERDATA_DETAIL)
1204 pr_info("cur_wp:%d, rec_start:%d, rec_len:%d\n",
1205 cur_wp,
1206 pcur_ud_rec->rec_start,
1207 pcur_ud_rec->rec_len);
1208
1209#ifdef DUMP_USER_DATA
1210 hw->reference[hw->cur_ud_idx] = reference;
1211#endif
1212
1213 hw->cur_ud_idx++;
1214 WRITE_VREG(AV_SCRATCH_J, 0);
1215
1216}
1217
1218
1219void userdata_pushed_drop(struct vdec_mpeg12_hw_s *hw)
1220{
1221 hw->userdata_info.last_wp = hw->notify_data_cc_last_wp;
1222 hw->ucode_cc_last_wp = hw->notify_ucode_cc_last_wp;
1223 hw->cur_ud_idx = 0;
1224 hw->wait_for_udr_send = 0;
1225
1226}
1227
1228
1229static int prepare_display_buf(struct vdec_mpeg12_hw_s *hw,
1230 struct pic_info_t *pic)
1231{
1232 u32 field_num = 0, i;
1233 u32 first_field_type = 0, type = 0;
1234 struct vframe_s *vf = NULL;
1235 u32 index = pic->index;
1236 u32 info = pic->buffer_info;
1237 struct vdec_s *vdec = hw_to_vdec(hw);
1238
1239 if (hw == NULL || pic == NULL)
1240 return -1;
1241
1242 user_data_ready_notify(hw, pic->pts, pic->pts_valid);
1243#ifdef NV21
1244 type = VIDTYPE_VIU_NV21;
1245#endif
1246
1247 if (hw->frame_prog & PICINFO_PROG) {
1248 field_num = 1;
1249 type |= VIDTYPE_PROGRESSIVE | VIDTYPE_VIU_FIELD;
1250 } else {
1251#ifdef INTERLACE_SEQ_ALWAYS
1252 /* once an interlace seq, force interlace, to make di easy. */
1253 hw->dec_control |= DEC_CONTROL_FLAG_FORCE_SEQ_INTERLACE;
1254#endif
1255 hw->frame_rpt_state = FRAME_REPEAT_NONE;
1256
1257 first_field_type = (info & PICINFO_TOP_FIRST) ?
1258 VIDTYPE_INTERLACE_TOP : VIDTYPE_INTERLACE_BOTTOM;
1259 field_num = (info & PICINFO_RPT_FIRST) ? 3 : 2;
1260 }
1261
1262 for (i = 0; i < field_num; i++) {
1263 if (kfifo_get(&hw->newframe_q, &vf) == 0) {
1264 debug_print(DECODE_ID(hw), PRINT_FLAG_ERROR,
1265 "fatal error, no available buffer slot.");
1266 hw->dec_result = DEC_RESULT_ERROR;
1267 vdec_schedule_work(&hw->work);
1268 return -1;
1269 }
1270 hw->vfbuf_use[index]++;
1271 vf->index = index;
1272 set_frame_info(hw, vf);
1273 if (field_num > 1) {
1274 vf->duration = vf->duration / field_num;
1275 vf->duration_pulldown = (field_num == 3) ?
1276 (vf->duration >> 1):0;
1277 if (i > 0)
1278 type = VIDTYPE_VIU_NV21;
1279 if (i == 1) /* second field*/
1280 type |= (first_field_type == VIDTYPE_INTERLACE_TOP) ?
1281 VIDTYPE_INTERLACE_BOTTOM : VIDTYPE_INTERLACE_TOP;
1282 else
1283 type |= (first_field_type == VIDTYPE_INTERLACE_TOP) ?
1284 VIDTYPE_INTERLACE_TOP : VIDTYPE_INTERLACE_BOTTOM;
1285 } else {
1286 if ((hw->seqinfo & SEQINFO_EXT_AVAILABLE) &&
1287 (hw->seqinfo & SEQINFO_PROG)) {
1288 if (info & PICINFO_RPT_FIRST) {
1289 if (info & PICINFO_TOP_FIRST)
1290 vf->duration *= 3;
1291 else
1292 vf->duration *= 2;
1293 }
1294 vf->duration_pulldown = 0;
1295 } else {
1296 vf->duration_pulldown =
1297 (info & PICINFO_RPT_FIRST) ?
1298 vf->duration >> 1 : 0;
1299 }
1300 }
1301 vf->duration += vf->duration_pulldown;
1302 vf->type = type;
1303 vf->signal_type = hw->reg_signal_type;
1304 vf->orientation = 0;
1305 if (i > 0) {
1306 vf->pts = 0;
1307 vf->pts_us64 = 0;
1308 } else {
1309 vf->pts = (pic->pts_valid) ? pic->pts : 0;
1310 vf->pts_us64 = (pic->pts_valid) ? pic->pts64 : 0;
1311 }
1312 vf->type_original = vf->type;
1313
1314 if ((error_skip(hw, pic->buffer_info, vf)) ||
1315 ((hw->first_i_frame_ready == 0) &&
1316 ((PICINFO_TYPE_MASK & pic->buffer_info) !=
1317 PICINFO_TYPE_I))) {
1318 hw->drop_frame_count++;
1319 hw->vfbuf_use[index]--;
1320 kfifo_put(&hw->newframe_q,
1321 (const struct vframe_s *)vf);
1322 } else {
1323 debug_print(DECODE_ID(hw), PRINT_FLAG_TIMEINFO,
1324 "%s, num: %d(%c), i: %d, pts: %d(%lld), dur: %d, type: %x\n",
1325 __func__, hw->disp_num, GET_SLICE_TYPE(info), i,
1326 vf->pts, vf->pts_us64, vf->duration, vf->type);
1327 hw->disp_num++;
1328 if (i == 0)
1329 decoder_do_frame_check(hw_to_vdec(hw), vf);
1330 vdec->vdec_fps_detec(vdec->id);
1331 vf->mem_handle =
1332 decoder_bmmu_box_get_mem_handle(
1333 hw->mm_blk_handle, index);
1334 kfifo_put(&hw->display_q,
1335 (const struct vframe_s *)vf);
1336 if (without_display_mode == 0) {
1337 vf_notify_receiver(vdec->vf_provider_name,
1338 VFRAME_EVENT_PROVIDER_VFRAME_READY,
1339 NULL);
1340 } else
1341 vmpeg_vf_put(vmpeg_vf_get(vdec), vdec);
1342
1343
1344 }
1345 }
1346 return 0;
1347}
1348
1349static void force_interlace_check(struct vdec_mpeg12_hw_s *hw)
1350{
1351 if ((hw->dec_control &
1352 DEC_CONTROL_FLAG_FORCE_2500_720_576_INTERLACE) &&
1353 (hw->frame_width == 720) &&
1354 (hw->frame_height == 576) &&
1355 (hw->frame_dur == 3840)) {
1356 hw->frame_prog = 0;
1357 } else if ((hw->dec_control
1358 & DEC_CONTROL_FLAG_FORCE_3000_704_480_INTERLACE) &&
1359 (hw->frame_width == 704) &&
1360 (hw->frame_height == 480) &&
1361 (hw->frame_dur == 3200)) {
1362 hw->frame_prog = 0;
1363 } else if ((hw->dec_control
1364 & DEC_CONTROL_FLAG_FORCE_2500_704_576_INTERLACE) &&
1365 (hw->frame_width == 704) &&
1366 (hw->frame_height == 576) &&
1367 (hw->frame_dur == 3840)) {
1368 hw->frame_prog = 0;
1369 } else if ((hw->dec_control
1370 & DEC_CONTROL_FLAG_FORCE_2500_544_576_INTERLACE) &&
1371 (hw->frame_width == 544) &&
1372 (hw->frame_height == 576) &&
1373 (hw->frame_dur == 3840)) {
1374 hw->frame_prog = 0;
1375 } else if ((hw->dec_control
1376 & DEC_CONTROL_FLAG_FORCE_2500_480_576_INTERLACE) &&
1377 (hw->frame_width == 480) &&
1378 (hw->frame_height == 576) &&
1379 (hw->frame_dur == 3840)) {
1380 hw->frame_prog = 0;
1381 } else if (hw->dec_control
1382 & DEC_CONTROL_FLAG_FORCE_SEQ_INTERLACE) {
1383 hw->frame_prog = 0;
1384 }
1385
1386}
1387
1388static int update_reference(struct vdec_mpeg12_hw_s *hw,
1389 int index)
1390{
1391 hw->vfbuf_use[index]++;
1392 if (hw->refs[1] == -1) {
1393 hw->refs[1] = index;
1394 /*
1395 * first pic need output to show
1396 * usecnt do not decrease.
1397 */
1398 } else if (hw->refs[0] == -1) {
1399 hw->refs[0] = hw->refs[1];
1400 hw->refs[1] = index;
1401 /* second pic do not output */
1402 index = DECODE_BUFFER_NUM_MAX;
1403 } else {
1404 hw->vfbuf_use[hw->refs[0]]--;
1405 hw->refs[0] = hw->refs[1];
1406 hw->refs[1] = index;
1407 index = hw->refs[0];
1408 }
1409 return index;
1410}
1411
1412static bool is_ref_error(struct vdec_mpeg12_hw_s *hw)
1413{
1414 if ((hw->pics[hw->refs[0]].buffer_info & PICINFO_ERROR) ||
1415 (hw->pics[hw->refs[1]].buffer_info & PICINFO_ERROR))
1416 return 1;
1417 return 0;
1418}
1419
1420
1421static irqreturn_t vmpeg12_isr_thread_fn(struct vdec_s *vdec, int irq)
1422{
1423 u32 reg, index, info, seqinfo, offset, pts, frame_size, tmp;
1424 u64 pts_us64 = 0;
1425 struct pic_info_t *new_pic, *disp_pic;
1426 struct vdec_mpeg12_hw_s *hw =
1427 (struct vdec_mpeg12_hw_s *)(vdec->private);
1428
1429 if (READ_VREG(AV_SCRATCH_M) != 0 &&
1430 (debug_enable & PRINT_FLAG_UCODE_DETAIL)) {
1431
1432 debug_print(DECODE_ID(hw), PRINT_FLAG_DEC_DETAIL,
1433 "dbg %x: %x, level %x, wp %x, rp %x, cnt %x\n",
1434 READ_VREG(AV_SCRATCH_M), READ_VREG(AV_SCRATCH_N),
1435 READ_VREG(VLD_MEM_VIFIFO_LEVEL),
1436 READ_VREG(VLD_MEM_VIFIFO_WP),
1437 READ_VREG(VLD_MEM_VIFIFO_RP),
1438 READ_VREG(VIFF_BIT_CNT));
1439 WRITE_VREG(AV_SCRATCH_M, 0);
1440 return IRQ_HANDLED;
1441 }
1442
1443 reg = READ_VREG(AV_SCRATCH_J);
1444 if (reg & (1<<16)) {
1445 vdec_schedule_work(&hw->userdata_push_work);
1446 return IRQ_HANDLED;
1447 }
1448
1449 reg = READ_VREG(MREG_BUFFEROUT);
1450 if (reg == 2) {
1451 /*timeout when decoding next frame*/
1452 debug_print(DECODE_ID(hw), PRINT_FLAG_VLD_DETAIL,
1453 "mmpeg12: lack data, lvl=%x ctrl=%x bcnt=%x\n",
1454 READ_VREG(VLD_MEM_VIFIFO_LEVEL),
1455 READ_VREG(VLD_MEM_VIFIFO_CONTROL),
1456 READ_VREG(VIFF_BIT_CNT));
1457
1458 if (vdec_frame_based(vdec)) {
1459 /* vmpeg12_save_hw_context(hw); */
1460 reset_process_time(hw);
1461 hw->dec_result = DEC_RESULT_DONE;
1462 vdec_schedule_work(&hw->work);
1463 } else {
1464 hw->dec_result = DEC_RESULT_AGAIN;
1465 vdec_schedule_work(&hw->work);
1466 userdata_pushed_drop(hw);
1467 reset_process_time(hw);
1468 }
1469 return IRQ_HANDLED;
1470 } else {
1471 reset_process_time(hw);
1472
1473 info = READ_VREG(MREG_PIC_INFO);
1474 offset = READ_VREG(MREG_FRAME_OFFSET);
1475 index = spec_to_index(hw, READ_VREG(REC_CANVAS_ADDR));
1476 seqinfo = READ_VREG(MREG_SEQ_INFO);
1477
1478 if ((info & PICINFO_PROG) == 0 &&
1479 (info & FRAME_PICTURE_MASK) != FRAME_PICTURE)
1480 hw->first_i_frame_ready = 1; /* for field struct case*/
1481
1482 if (index >= DECODE_BUFFER_NUM_MAX) {
1483 debug_print(DECODE_ID(hw), PRINT_FLAG_ERROR,
1484 "mmpeg12: invalid buf index: %d\n", index);
1485 hw->dec_result = DEC_RESULT_ERROR;
1486 vdec_schedule_work(&hw->work);
1487 return IRQ_HANDLED;
1488 }
1489 hw->dec_num++;
1490 hw->dec_result = DEC_RESULT_DONE;
1491 new_pic = &hw->pics[index];
1492 tmp = READ_VREG(MREG_PIC_WIDTH);
1493 if ((tmp > 1920) || (tmp == 0)) {
1494 new_pic->width = 1920;
1495 hw->frame_width = 1920;
1496 } else {
1497 new_pic->width = tmp;
1498 hw->frame_width = tmp;
1499 }
1500
1501 tmp = READ_VREG(MREG_PIC_HEIGHT);
1502 if ((tmp > 1088) || (tmp == 0)) {
1503 new_pic->height = 1088;
1504 hw->frame_height = 1088;
1505 } else {
1506 new_pic->height = tmp;
1507 hw->frame_height = tmp;
1508 }
1509
1510 new_pic->buffer_info = info;
1511 new_pic->offset = offset;
1512 new_pic->index = index;
1513 if (((info & PICINFO_TYPE_MASK) == PICINFO_TYPE_I) ||
1514 ((info & PICINFO_TYPE_MASK) == PICINFO_TYPE_P)) {
1515 if (hw->chunk) {
1516 new_pic->pts_valid = hw->chunk->pts_valid;
1517 new_pic->pts = hw->chunk->pts;
1518 new_pic->pts64 = hw->chunk->pts64;
1519 if (hw->last_chunk_pts == hw->chunk->pts) {
1520 new_pic->pts_valid = 0;
1521 debug_print(DECODE_ID(hw), PRINT_FLAG_TIMEINFO,
1522 "pts invalid\n");
1523 }
1524 } else {
1525 if (pts_lookup_offset_us64(PTS_TYPE_VIDEO, offset,
1526 &pts, &frame_size, 0, &pts_us64) == 0) {
1527 new_pic->pts_valid = true;
1528 new_pic->pts = pts;
1529 new_pic->pts64 = pts_us64;
1530 } else
1531 new_pic->pts_valid = false;
1532 }
1533 } else {
1534 if (hw->chunk)
1535 hw->last_chunk_pts = hw->chunk->pts;
1536 new_pic->pts_valid = false;
1537 }
1538
1539 debug_print(DECODE_ID(hw), PRINT_FLAG_RUN_FLOW,
1540 "mmpeg12: new_pic=%d, ind=%d, info=%x, seq=%x, offset=%d\n",
1541 hw->dec_num, index, info, seqinfo, offset);
1542
1543 hw->frame_prog = info & PICINFO_PROG;
1544 if ((seqinfo & SEQINFO_EXT_AVAILABLE) &&
1545 ((seqinfo & SEQINFO_PROG) == 0))
1546 hw->frame_prog = 0;
1547 force_interlace_check(hw);
1548
1549 if (is_ref_error(hw)) {
1550 if ((info & PICINFO_TYPE_MASK) == PICINFO_TYPE_B)
1551 new_pic->buffer_info |= PICINFO_ERROR;
1552 }
1553
1554 if (((info & PICINFO_TYPE_MASK) == PICINFO_TYPE_I) ||
1555 ((info & PICINFO_TYPE_MASK) == PICINFO_TYPE_P)) {
1556 index = update_reference(hw, index);
1557 } else {
1558 /* drop b frame before reference pic ready */
1559 if (hw->refs[0] == -1)
1560 index = DECODE_BUFFER_NUM_MAX;
1561 }
1562 vmpeg12_save_hw_context(hw, reg);
1563
1564 if (index >= DECODE_BUFFER_NUM_MAX) {
1565 if (hw->dec_num != 2) {
1566 debug_print(DECODE_ID(hw), 0,
1567 "mmpeg12: drop pic num %d, type %c, index %d, offset %x\n",
1568 hw->dec_num, GET_SLICE_TYPE(info), index, offset);
1569 hw->dec_result = DEC_RESULT_ERROR;
1570 }
1571 vdec_schedule_work(&hw->work);
1572 return IRQ_HANDLED;
1573 }
1574
1575 disp_pic = &hw->pics[index];
1576 info = hw->pics[index].buffer_info;
1577 if (disp_pic->pts_valid && hw->lastpts64 == disp_pic->pts64)
1578 disp_pic->pts_valid = false;
1579 if (disp_pic->pts_valid)
1580 hw->lastpts64 = disp_pic->pts64;
1581
1582 if ((hw->first_i_frame_ready == 0) &&
1583 ((info & PICINFO_TYPE_MASK) == PICINFO_TYPE_I) &&
1584 ((info & PICINFO_ERROR) == 0))
1585 hw->first_i_frame_ready = 1;
1586
1587 debug_print(DECODE_ID(hw), PRINT_FLAG_RUN_FLOW,
1588 "mmpeg12: disp_pic=%d(%c), ind=%d, offst=%x, pts=(%d,%lld)(%d)\n",
1589 hw->disp_num, GET_SLICE_TYPE(info), index, disp_pic->offset,
1590 disp_pic->pts, disp_pic->pts64, disp_pic->pts_valid);
1591
1592 prepare_display_buf(hw, disp_pic);
1593 vdec_schedule_work(&hw->work);
1594 }
1595
1596 return IRQ_HANDLED;
1597}
1598static irqreturn_t vmpeg12_isr(struct vdec_s *vdec, int irq)
1599{
1600 u32 info, offset;
1601 struct vdec_mpeg12_hw_s *hw =
1602 (struct vdec_mpeg12_hw_s *)(vdec->private);
1603 if (hw->eos)
1604 return IRQ_HANDLED;
1605 info = READ_VREG(MREG_PIC_INFO);
1606 offset = READ_VREG(MREG_FRAME_OFFSET);
1607
1608 vdec_count_info(&gvs, info & PICINFO_ERROR, offset);
1609
1610 WRITE_VREG(ASSIST_MBOX1_CLR_REG, 1);
1611
1612 return IRQ_WAKE_THREAD;
1613}
1614
1615static void vmpeg12_notify_work(struct work_struct *work)
1616{
1617 struct vdec_mpeg12_hw_s *hw = container_of(work,
1618 struct vdec_mpeg12_hw_s, notify_work);
1619 struct vdec_s *vdec = hw_to_vdec(hw);
1620
1621 if (vdec->fr_hint_state == VDEC_NEED_HINT) {
1622 vf_notify_receiver(vdec->vf_provider_name,
1623 VFRAME_EVENT_PROVIDER_FR_HINT,
1624 (void *)((unsigned long)hw->frame_dur));
1625 vdec->fr_hint_state = VDEC_HINTED;
1626 }
1627}
1628
1629static void wait_vmmpeg12_search_done(struct vdec_mpeg12_hw_s *hw)
1630{
1631 u32 vld_rp = READ_VREG(VLD_MEM_VIFIFO_RP);
1632 int count = 0;
1633
1634 do {
1635 usleep_range(100, 500);
1636 if (vld_rp == READ_VREG(VLD_MEM_VIFIFO_RP))
1637 break;
1638 if (count > 1000) {
1639 debug_print(DECODE_ID(hw), 0,
1640 "%s, count %d vld_rp 0x%x VLD_MEM_VIFIFO_RP 0x%x\n",
1641 __func__, count, vld_rp, READ_VREG(VLD_MEM_VIFIFO_RP));
1642 break;
1643 } else
1644 vld_rp = READ_VREG(VLD_MEM_VIFIFO_RP);
1645 count++;
1646 } while (1);
1647}
1648
1649static void flush_output(struct vdec_mpeg12_hw_s *hw)
1650{
1651 int index = hw->refs[1];
1652
1653 /* video only one frame need not flush. */
1654 if (hw->dec_num < 2)
1655 return;
1656
1657 if (index >= 0 && index < DECODE_BUFFER_NUM_MAX)
1658 prepare_display_buf(hw, &hw->pics[index]);
1659}
1660
1661static void vmpeg12_work(struct work_struct *work)
1662{
1663 struct vdec_mpeg12_hw_s *hw =
1664 container_of(work, struct vdec_mpeg12_hw_s, work);
1665 struct vdec_s *vdec = hw_to_vdec(hw);
1666
1667 if (hw->dec_result != DEC_RESULT_DONE)
1668 debug_print(DECODE_ID(hw), PRINT_FLAG_RUN_FLOW,
1669 "%s, result=%d, status=%d\n", __func__,
1670 hw->dec_result, vdec->next_status);
1671
1672 if (hw->dec_result == DEC_RESULT_DONE) {
1673 vdec_vframe_dirty(vdec, hw->chunk);
1674 hw->chunk = NULL;
1675 } else if (hw->dec_result == DEC_RESULT_AGAIN &&
1676 (vdec->next_status != VDEC_STATUS_DISCONNECTED)) {
1677 /*
1678 stream base: stream buf empty or timeout
1679 frame base: vdec_prepare_input fail
1680 */
1681 if (!vdec_has_more_input(vdec)) {
1682 hw->dec_result = DEC_RESULT_EOS;
1683 vdec_schedule_work(&hw->work);
1684 return;
1685 }
1686#ifdef AGAIN_HAS_THRESHOLD
1687 hw->next_again_flag = 1;
1688#endif
1689 } else if (hw->dec_result == DEC_RESULT_GET_DATA &&
1690 vdec->next_status != VDEC_STATUS_DISCONNECTED) {
1691 if (!vdec_has_more_input(vdec)) {
1692 hw->dec_result = DEC_RESULT_EOS;
1693 vdec_schedule_work(&hw->work);
1694 return;
1695 }
1696 debug_print(DECODE_ID(hw), PRINT_FLAG_VLD_DETAIL,
1697 "%s DEC_RESULT_GET_DATA %x %x %x\n",
1698 __func__,
1699 READ_VREG(VLD_MEM_VIFIFO_LEVEL),
1700 READ_VREG(VLD_MEM_VIFIFO_WP),
1701 READ_VREG(VLD_MEM_VIFIFO_RP));
1702 vdec_vframe_dirty(vdec, hw->chunk);
1703 hw->chunk = NULL;
1704 vdec_clean_input(vdec);
1705 return;
1706 } else if (hw->dec_result == DEC_RESULT_FORCE_EXIT) {
1707 debug_print(DECODE_ID(hw), PRINT_FLAG_ERROR,
1708 "%s: force exit\n", __func__);
1709 if (hw->stat & STAT_ISR_REG) {
1710 amvdec_stop();
1711 vdec_free_irq(VDEC_IRQ_1, (void *)hw);
1712 hw->stat &= ~STAT_ISR_REG;
1713 }
1714 } else if (hw->dec_result == DEC_RESULT_EOS) {
1715 if (hw->stat & STAT_VDEC_RUN) {
1716 amvdec_stop();
1717 hw->stat &= ~STAT_VDEC_RUN;
1718 }
1719 hw->eos = 1;
1720 vdec_vframe_dirty(vdec, hw->chunk);
1721 hw->chunk = NULL;
1722 vdec_clean_input(vdec);
1723 flush_output(hw);
1724 debug_print(DECODE_ID(hw), 0,
1725 "%s: end of stream, num %d(%d)\n",
1726 __func__, hw->disp_num, hw->dec_num);
1727 }
1728 if (hw->stat & STAT_VDEC_RUN) {
1729 amvdec_stop();
1730 hw->stat &= ~STAT_VDEC_RUN;
1731 }
1732 /*disable mbox interrupt */
1733 WRITE_VREG(ASSIST_MBOX1_MASK, 0);
1734 wait_vmmpeg12_search_done(hw);
1735 if (vdec->parallel_dec == 1)
1736 vdec_core_finish_run(vdec, CORE_MASK_VDEC_1);
1737 else
1738 vdec_core_finish_run(vdec, CORE_MASK_VDEC_1 | CORE_MASK_HEVC);
1739 del_timer_sync(&hw->check_timer);
1740 hw->stat &= ~STAT_TIMER_ARM;
1741
1742 if (hw->vdec_cb)
1743 hw->vdec_cb(vdec, hw->vdec_cb_arg);
1744}
1745
1746static struct vframe_s *vmpeg_vf_peek(void *op_arg)
1747{
1748 struct vframe_s *vf;
1749 struct vdec_s *vdec = op_arg;
1750 struct vdec_mpeg12_hw_s *hw =
1751 (struct vdec_mpeg12_hw_s *)vdec->private;
1752 hw->peek_num++;
1753 if (kfifo_peek(&hw->display_q, &vf))
1754 return vf;
1755
1756 return NULL;
1757}
1758
1759static struct vframe_s *vmpeg_vf_get(void *op_arg)
1760{
1761 struct vframe_s *vf;
1762 struct vdec_s *vdec = op_arg;
1763 struct vdec_mpeg12_hw_s *hw =
1764 (struct vdec_mpeg12_hw_s *)vdec->private;
1765
1766 hw->get_num++;
1767 if (kfifo_get(&hw->display_q, &vf))
1768 return vf;
1769
1770 return NULL;
1771}
1772
1773static void vmpeg_vf_put(struct vframe_s *vf, void *op_arg)
1774{
1775 struct vdec_s *vdec = op_arg;
1776 struct vdec_mpeg12_hw_s *hw =
1777 (struct vdec_mpeg12_hw_s *)vdec->private;
1778
1779 hw->vfbuf_use[vf->index]--;
1780 hw->put_num++;
1781 kfifo_put(&hw->newframe_q,
1782 (const struct vframe_s *)vf);
1783 debug_print(DECODE_ID(hw), PRINT_FLAG_RUN_FLOW,
1784 "%s: index %d, use %d\n", __func__,
1785 vf->index, hw->vfbuf_use[vf->index]);
1786}
1787
1788static int vmpeg_event_cb(int type, void *data, void *private_data)
1789{
1790 return 0;
1791}
1792
1793static int vmpeg_vf_states(struct vframe_states *states, void *op_arg)
1794{
1795 unsigned long flags;
1796 struct vdec_s *vdec = op_arg;
1797 struct vdec_mpeg12_hw_s *hw =
1798 (struct vdec_mpeg12_hw_s *)vdec->private;
1799
1800 spin_lock_irqsave(&hw->lock, flags);
1801
1802 states->vf_pool_size = VF_POOL_SIZE;
1803 states->buf_free_num = kfifo_len(&hw->newframe_q);
1804 states->buf_avail_num = kfifo_len(&hw->display_q);
1805 states->buf_recycle_num = 0;
1806
1807 spin_unlock_irqrestore(&hw->lock, flags);
1808 return 0;
1809}
1810static int vmmpeg12_dec_status(struct vdec_s *vdec, struct vdec_info *vstatus)
1811{
1812 struct vdec_mpeg12_hw_s *hw =
1813 (struct vdec_mpeg12_hw_s *)vdec->private;
1814
1815 if (!hw)
1816 return -1;
1817
1818 vstatus->frame_width = hw->frame_width;
1819 vstatus->frame_height = hw->frame_height;
1820 if (hw->frame_dur != 0)
1821 vstatus->frame_rate = 96000 / hw->frame_dur;
1822 else
1823 vstatus->frame_rate = -1;
1824 vstatus->error_count = READ_VREG(AV_SCRATCH_C);
1825 vstatus->status = hw->stat;
1826 vstatus->bit_rate = gvs.bit_rate;
1827 vstatus->frame_dur = hw->frame_dur;
1828 vstatus->frame_data = gvs.frame_data;
1829 vstatus->total_data = gvs.total_data;
1830 vstatus->frame_count = gvs.frame_count;
1831 vstatus->error_frame_count = gvs.error_frame_count;
1832 vstatus->drop_frame_count = hw->drop_frame_count;
1833 vstatus->total_data = gvs.total_data;
1834 vstatus->samp_cnt = gvs.samp_cnt;
1835 vstatus->offset = gvs.offset;
1836 vstatus->ratio_control = hw->ratio_control;
1837 snprintf(vstatus->vdec_name, sizeof(vstatus->vdec_name),
1838 "%s", DRIVER_NAME);
1839
1840 return 0;
1841}
1842
1843
1844
1845/****************************************/
1846static void vmpeg12_canvas_init(struct vdec_mpeg12_hw_s *hw)
1847{
1848 int i, ret;
1849 u32 canvas_width, canvas_height;
1850 u32 decbuf_size, decbuf_y_size, decbuf_uv_size;
1851 unsigned long decbuf_start;
1852 /*u32 disp_addr = 0xffffffff;*/
1853 struct vdec_s *vdec = hw_to_vdec(hw);
1854
1855 if (buf_size <= 0x00400000) {
1856 /* SD only */
1857 canvas_width = 768;
1858 canvas_height = 576;
1859 decbuf_y_size = 0x80000;
1860 decbuf_uv_size = 0x20000;
1861 decbuf_size = 0x100000;
1862 } else {
1863 /* HD & SD */
1864 canvas_width = 1920;
1865 canvas_height = 1088;
1866 decbuf_y_size = 0x200000;
1867 decbuf_uv_size = 0x80000;
1868 decbuf_size = 0x300000;
1869 }
1870
1871 for (i = 0; i < MAX_BMMU_BUFFER_NUM; i++) {
1872
1873 unsigned canvas;
1874 if (i == (MAX_BMMU_BUFFER_NUM - 1)) /* SWAP&CCBUF&MATIRX&MV */
1875 decbuf_size = WORKSPACE_SIZE;
1876 ret = decoder_bmmu_box_alloc_buf_phy(hw->mm_blk_handle, i,
1877 decbuf_size, DRIVER_NAME, &decbuf_start);
1878 if (ret < 0) {
1879 pr_err("mmu alloc failed! size 0x%d idx %d\n",
1880 decbuf_size, i);
1881 return;
1882 }
1883
1884 if (i == (MAX_BMMU_BUFFER_NUM - 1)) {
1885 if (hw->ccbuf_phyAddress_is_remaped_nocache)
1886 codec_mm_unmap_phyaddr(hw->ccbuf_phyAddress_virt);
1887 hw->ccbuf_phyAddress_virt = NULL;
1888 hw->ccbuf_phyAddress = 0;
1889 hw->ccbuf_phyAddress_is_remaped_nocache = 0;
1890
1891 hw->buf_start = decbuf_start;
1892 hw->ccbuf_phyAddress = hw->buf_start + CTX_CCBUF_OFFSET;
1893 hw->ccbuf_phyAddress_virt
1894 = codec_mm_phys_to_virt(
1895 hw->ccbuf_phyAddress);
1896 if ((!hw->ccbuf_phyAddress_virt) && (!hw->tvp_flag)) {
1897 hw->ccbuf_phyAddress_virt
1898 = codec_mm_vmap(
1899 hw->ccbuf_phyAddress,
1900 CCBUF_SIZE);
1901 hw->ccbuf_phyAddress_is_remaped_nocache = 1;
1902 }
1903
1904 WRITE_VREG(MREG_CO_MV_START, hw->buf_start);
1905 } else {
1906 if (vdec->parallel_dec == 1) {
1907 unsigned tmp;
1908 if (canvas_u(hw->canvas_spec[i]) == 0xff) {
1909 tmp =
1910 vdec->get_canvas_ex(CORE_MASK_VDEC_1, vdec->id);
1911 hw->canvas_spec[i] &= ~(0xffff << 8);
1912 hw->canvas_spec[i] |= tmp << 8;
1913 hw->canvas_spec[i] |= tmp << 16;
1914 }
1915 if (canvas_y(hw->canvas_spec[i]) == 0xff) {
1916 tmp =
1917 vdec->get_canvas_ex(CORE_MASK_VDEC_1, vdec->id);
1918 hw->canvas_spec[i] &= ~0xff;
1919 hw->canvas_spec[i] |= tmp;
1920 }
1921 canvas = hw->canvas_spec[i];
1922 } else {
1923 canvas = vdec->get_canvas(i, 2);
1924 hw->canvas_spec[i] = canvas;
1925 }
1926
1927 hw->canvas_config[i][0].phy_addr =
1928 decbuf_start;
1929 hw->canvas_config[i][0].width =
1930 canvas_width;
1931 hw->canvas_config[i][0].height =
1932 canvas_height;
1933 hw->canvas_config[i][0].block_mode =
1934 hw->canvas_mode;
1935 hw->canvas_config[i][0].endian =
1936 (hw->canvas_mode == CANVAS_BLKMODE_LINEAR)?7:0;
1937
1938 canvas_config_config(canvas_y(canvas),
1939 &hw->canvas_config[i][0]);
1940
1941 hw->canvas_config[i][1].phy_addr =
1942 decbuf_start + decbuf_y_size;
1943 hw->canvas_config[i][1].width = canvas_width;
1944 hw->canvas_config[i][1].height = canvas_height / 2;
1945 hw->canvas_config[i][1].block_mode = hw->canvas_mode;
1946 hw->canvas_config[i][1].endian =
1947 (hw->canvas_mode == CANVAS_BLKMODE_LINEAR)?7:0;
1948
1949 canvas_config_config(canvas_u(canvas),
1950 &hw->canvas_config[i][1]);
1951 }
1952 }
1953 return;
1954}
1955
1956static void vmpeg2_dump_state(struct vdec_s *vdec)
1957{
1958 struct vdec_mpeg12_hw_s *hw =
1959 (struct vdec_mpeg12_hw_s *)(vdec->private);
1960 u32 i;
1961 debug_print(DECODE_ID(hw), 0,
1962 "====== %s\n", __func__);
1963 debug_print(DECODE_ID(hw), 0,
1964 "width/height (%d/%d),i_first %d\n",
1965 hw->frame_width,
1966 hw->frame_height,
1967 hw->first_i_frame_ready
1968 );
1969 debug_print(DECODE_ID(hw), 0,
1970 "is_framebase(%d), eos %d, state 0x%x, dec_result 0x%x dec_frm %d put_frm %d run %d not_run_ready %d,input_empty %d\n",
1971 vdec_frame_based(vdec),
1972 hw->eos,
1973 hw->stat,
1974 hw->dec_result,
1975 hw->dec_num,
1976 hw->put_num,
1977 hw->run_count,
1978 hw->not_run_ready,
1979 hw->input_empty
1980 );
1981
1982 for (i = 0; i < DECODE_BUFFER_NUM_MAX; i++) {
1983 debug_print(DECODE_ID(hw), 0,
1984 "index %d, used %d\n", i, hw->vfbuf_use[i]);
1985 }
1986
1987 if (vf_get_receiver(vdec->vf_provider_name)) {
1988 enum receviver_start_e state =
1989 vf_notify_receiver(vdec->vf_provider_name,
1990 VFRAME_EVENT_PROVIDER_QUREY_STATE,
1991 NULL);
1992 debug_print(DECODE_ID(hw), 0,
1993 "\nreceiver(%s) state %d\n",
1994 vdec->vf_provider_name,
1995 state);
1996 }
1997 debug_print(DECODE_ID(hw), 0,
1998 "%s, newq(%d/%d), dispq(%d/%d) vf peek/get/put (%d/%d/%d),drop=%d, buffer_not_ready %d\n",
1999 __func__,
2000 kfifo_len(&hw->newframe_q),
2001 VF_POOL_SIZE,
2002 kfifo_len(&hw->display_q),
2003 VF_POOL_SIZE,
2004 hw->peek_num,
2005 hw->get_num,
2006 hw->put_num,
2007 hw->drop_frame_count,
2008 hw->buffer_not_ready
2009 );
2010 debug_print(DECODE_ID(hw), 0,
2011 "VIFF_BIT_CNT=0x%x\n",
2012 READ_VREG(VIFF_BIT_CNT));
2013 debug_print(DECODE_ID(hw), 0,
2014 "VLD_MEM_VIFIFO_LEVEL=0x%x\n",
2015 READ_VREG(VLD_MEM_VIFIFO_LEVEL));
2016 debug_print(DECODE_ID(hw), 0,
2017 "VLD_MEM_VIFIFO_WP=0x%x\n",
2018 READ_VREG(VLD_MEM_VIFIFO_WP));
2019 debug_print(DECODE_ID(hw), 0,
2020 "VLD_MEM_VIFIFO_RP=0x%x\n",
2021 READ_VREG(VLD_MEM_VIFIFO_RP));
2022 debug_print(DECODE_ID(hw), 0,
2023 "PARSER_VIDEO_RP=0x%x\n",
2024 READ_PARSER_REG(PARSER_VIDEO_RP));
2025 debug_print(DECODE_ID(hw), 0,
2026 "PARSER_VIDEO_WP=0x%x\n",
2027 READ_PARSER_REG(PARSER_VIDEO_WP));
2028 if (vdec_frame_based(vdec) &&
2029 debug_enable & PRINT_FRAMEBASE_DATA
2030 ) {
2031 int jj;
2032 if (hw->chunk && hw->chunk->block &&
2033 hw->chunk->size > 0) {
2034 u8 *data = NULL;
2035
2036 if (!hw->chunk->block->is_mapped)
2037 data = codec_mm_vmap(hw->chunk->block->start +
2038 hw->chunk->offset, hw->chunk->size);
2039 else
2040 data = ((u8 *)hw->chunk->block->start_virt) +
2041 hw->chunk->offset;
2042
2043 debug_print(DECODE_ID(hw), 0,
2044 "frame data size 0x%x\n",
2045 hw->chunk->size);
2046 for (jj = 0; jj < hw->chunk->size; jj++) {
2047 if ((jj & 0xf) == 0)
2048 debug_print(DECODE_ID(hw),
2049 PRINT_FRAMEBASE_DATA,
2050 "%06x:", jj);
2051 debug_print(DECODE_ID(hw),
2052 PRINT_FRAMEBASE_DATA,
2053 "%02x ", data[jj]);
2054 if (((jj + 1) & 0xf) == 0)
2055 debug_print(DECODE_ID(hw),
2056 PRINT_FRAMEBASE_DATA, "\n");
2057 }
2058
2059 if (!hw->chunk->block->is_mapped)
2060 codec_mm_unmap_phyaddr(data);
2061 }
2062 }
2063}
2064
2065static void reset_process_time(struct vdec_mpeg12_hw_s *hw)
2066{
2067 if (hw->start_process_time) {
2068 unsigned process_time =
2069 1000 * (jiffies - hw->start_process_time) / HZ;
2070 hw->start_process_time = 0;
2071 if (process_time > max_process_time[DECODE_ID(hw)])
2072 max_process_time[DECODE_ID(hw)] = process_time;
2073 }
2074}
2075static void start_process_time(struct vdec_mpeg12_hw_s *hw)
2076{
2077 hw->decode_timeout_count = 10;
2078 hw->start_process_time = jiffies;
2079}
2080static void timeout_process(struct vdec_mpeg12_hw_s *hw)
2081{
2082 struct vdec_s *vdec = hw_to_vdec(hw);
2083
2084 reset_process_time(hw);
2085 amvdec_stop();
2086 debug_print(DECODE_ID(hw), PRINT_FLAG_ERROR,
2087 "%s decoder timeout, status=%d, level=%d\n",
2088 __func__, vdec->status, READ_VREG(VLD_MEM_VIFIFO_LEVEL));
2089 hw->dec_result = DEC_RESULT_DONE;
2090 hw->first_i_frame_ready = 0;
2091 vdec_schedule_work(&hw->work);
2092}
2093
2094static void check_timer_func(unsigned long arg)
2095{
2096 struct vdec_mpeg12_hw_s *hw = (struct vdec_mpeg12_hw_s *)arg;
2097 struct vdec_s *vdec = hw_to_vdec(hw);
2098 unsigned int timeout_val = decode_timeout_val;
2099
2100 if (radr != 0) {
2101 if (rval != 0) {
2102 WRITE_VREG(radr, rval);
2103 pr_info("WRITE_VREG(%x,%x)\n", radr, rval);
2104 } else
2105 pr_info("READ_VREG(%x)=%x\n", radr, READ_VREG(radr));
2106 rval = 0;
2107 radr = 0;
2108 }
2109
2110 if (((debug_enable & PRINT_FLAG_TIMEOUT_STATUS) == 0) &&
2111 (timeout_val > 0) &&
2112 (hw->start_process_time > 0) &&
2113 ((1000 * (jiffies - hw->start_process_time) / HZ)
2114 > timeout_val)) {
2115 if (hw->last_vld_level == READ_VREG(VLD_MEM_VIFIFO_LEVEL)) {
2116 if (hw->decode_timeout_count > 0)
2117 hw->decode_timeout_count--;
2118 if (hw->decode_timeout_count == 0)
2119 timeout_process(hw);
2120 }
2121 hw->last_vld_level = READ_VREG(VLD_MEM_VIFIFO_LEVEL);
2122 }
2123
2124 if (vdec->next_status == VDEC_STATUS_DISCONNECTED) {
2125 hw->dec_result = DEC_RESULT_FORCE_EXIT;
2126 vdec_schedule_work(&hw->work);
2127 pr_info("vdec requested to be disconnected\n");
2128 return;
2129 }
2130
2131 mod_timer(&hw->check_timer, jiffies + CHECK_INTERVAL);
2132}
2133
2134static int vmpeg12_hw_ctx_restore(struct vdec_mpeg12_hw_s *hw)
2135{
2136 u32 index, i;
2137 index = find_buffer(hw);
2138 if (index >= DECODE_BUFFER_NUM_MAX)
2139 return -1;
2140 if (!hw->init_flag)
2141 vmpeg12_canvas_init(hw);
2142 else {
2143 WRITE_VREG(MREG_CO_MV_START, hw->buf_start);
2144 for (i = 0; i < DECODE_BUFFER_NUM_MAX; i++) {
2145 canvas_config_config(canvas_y(hw->canvas_spec[i]),
2146 &hw->canvas_config[i][0]);
2147 canvas_config_config(canvas_u(hw->canvas_spec[i]),
2148 &hw->canvas_config[i][1]);
2149 }
2150 }
2151
2152 /* prepare REF0 & REF1
2153 points to the past two IP buffers
2154 prepare REC_CANVAS_ADDR and ANC2_CANVAS_ADDR
2155 points to the output buffer*/
2156 WRITE_VREG(MREG_REF0,
2157 (hw->refs[0] == -1) ? 0xffffffff :
2158 hw->canvas_spec[hw->refs[0]]);
2159 WRITE_VREG(MREG_REF1,
2160 (hw->refs[1] == -1) ? 0xffffffff :
2161 hw->canvas_spec[hw->refs[1]]);
2162 WRITE_VREG(REC_CANVAS_ADDR, hw->canvas_spec[index]);
2163 WRITE_VREG(ANC2_CANVAS_ADDR, hw->canvas_spec[index]);
2164
2165 debug_print(DECODE_ID(hw), PRINT_FLAG_RESTORE,
2166 "%s,ref0=0x%x, ref1=0x%x,rec=0x%x, ctx_valid=%d,index=%d\n",
2167 __func__,
2168 READ_VREG(MREG_REF0),
2169 READ_VREG(MREG_REF1),
2170 READ_VREG(REC_CANVAS_ADDR),
2171 hw->ctx_valid, index);
2172
2173 /* set to mpeg1 default */
2174 WRITE_VREG(MPEG1_2_REG,
2175 (hw->ctx_valid) ? hw->reg_mpeg1_2_reg : 0);
2176 /* disable PSCALE for hardware sharing */
2177 WRITE_VREG(PSCALE_CTRL, 0);
2178 /* for Mpeg1 default value */
2179 WRITE_VREG(PIC_HEAD_INFO,
2180 (hw->ctx_valid) ? hw->reg_pic_head_info : 0x380);
2181 /* disable mpeg4 */
2182 WRITE_VREG(M4_CONTROL_REG, 0);
2183 /* clear mailbox interrupt */
2184 WRITE_VREG(ASSIST_MBOX1_CLR_REG, 1);
2185 /* clear buffer IN/OUT registers */
2186 WRITE_VREG(MREG_BUFFEROUT, 0);
2187 /* enable mailbox interrupt */
2188 WRITE_VREG(ASSIST_MBOX1_MASK, 1);
2189 /* set reference width and height */
2190 if ((hw->frame_width != 0) && (hw->frame_height != 0))
2191 WRITE_VREG(MREG_CMD,
2192 (hw->frame_width << 16) | hw->frame_height);
2193 else
2194 WRITE_VREG(MREG_CMD, 0);
2195
2196 debug_print(DECODE_ID(hw), PRINT_FLAG_RESTORE,
2197 "0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n",
2198 hw->frame_width, hw->frame_height, hw->seqinfo,
2199 hw->reg_f_code_reg, hw->reg_slice_ver_pos_pic_type,
2200 hw->reg_mb_info);
2201
2202 WRITE_VREG(MREG_PIC_WIDTH, hw->reg_pic_width);
2203 WRITE_VREG(MREG_PIC_HEIGHT, hw->reg_pic_height);
2204 WRITE_VREG(MREG_SEQ_INFO, hw->seqinfo);
2205 WRITE_VREG(F_CODE_REG, hw->reg_f_code_reg);
2206 WRITE_VREG(SLICE_VER_POS_PIC_TYPE,
2207 hw->reg_slice_ver_pos_pic_type);
2208 WRITE_VREG(MB_INFO, hw->reg_mb_info);
2209 WRITE_VREG(VCOP_CTRL_REG, hw->reg_vcop_ctrl_reg);
2210 WRITE_VREG(AV_SCRATCH_H, hw->reg_signal_type);
2211
2212 /* clear error count */
2213 WRITE_VREG(MREG_ERROR_COUNT, 0);
2214 WRITE_VREG(MREG_FATAL_ERROR, 0);
2215 /* clear wait buffer status */
2216 WRITE_VREG(MREG_WAIT_BUFFER, 0);
2217#ifdef NV21
2218 SET_VREG_MASK(MDEC_PIC_DC_CTRL, 1<<17);
2219#endif
2220 if (!hw->ctx_valid)
2221 WRITE_VREG(AV_SCRATCH_J, hw->userdata_wp_ctx);
2222
2223 if (hw->chunk) {
2224 /*frame based input*/
2225 WRITE_VREG(MREG_INPUT,
2226 (hw->chunk->offset & 7) | (1<<7) | (hw->ctx_valid<<6));
2227 } else {
2228 /*stream based input*/
2229 WRITE_VREG(MREG_INPUT, (hw->ctx_valid<<6));
2230 }
2231 return 0;
2232}
2233
2234static void vmpeg12_local_init(struct vdec_mpeg12_hw_s *hw)
2235{
2236 int i;
2237 INIT_KFIFO(hw->display_q);
2238 INIT_KFIFO(hw->newframe_q);
2239
2240 for (i = 0; i < VF_POOL_SIZE; i++) {
2241 const struct vframe_s *vf;
2242 vf = &hw->vfpool[i];
2243 hw->vfpool[i].index = DECODE_BUFFER_NUM_MAX;
2244 kfifo_put(&hw->newframe_q, (const struct vframe_s *)vf);
2245 }
2246
2247 for (i = 0; i < DECODE_BUFFER_NUM_MAX; i++)
2248 hw->vfbuf_use[i] = 0;
2249
2250
2251 if (hw->mm_blk_handle) {
2252 decoder_bmmu_box_free(hw->mm_blk_handle);
2253 hw->mm_blk_handle = NULL;
2254 }
2255
2256 hw->mm_blk_handle = decoder_bmmu_box_alloc_box(
2257 DRIVER_NAME,
2258 0,
2259 MAX_BMMU_BUFFER_NUM,
2260 4 + PAGE_SHIFT,
2261 CODEC_MM_FLAGS_CMA_CLEAR |
2262 CODEC_MM_FLAGS_FOR_VDECODER |
2263 hw->tvp_flag);
2264 hw->eos = 0;
2265 hw->frame_width = hw->frame_height = 0;
2266 hw->frame_dur = hw->frame_prog = 0;
2267 hw->frame_force_skip_flag = 0;
2268 hw->wait_buffer_counter = 0;
2269 hw->first_i_frame_ready = 0;
2270 hw->dec_control &= DEC_CONTROL_INTERNAL_MASK;
2271 hw->refs[0] = -1;
2272 hw->refs[1] = -1;
2273 hw->disp_num = 0;
2274 hw->dec_num = 0;
2275 hw->put_num = 0;
2276 hw->run_count = 0;
2277 hw->not_run_ready = 0;
2278 hw->input_empty = 0;
2279 hw->peek_num = 0;
2280 hw->get_num = 0;
2281 hw->drop_frame_count = 0;
2282 hw->buffer_not_ready = 0;
2283 hw->start_process_time = 0;
2284 hw->init_flag = 0;
2285 hw->error_frame_skip_level = error_frame_skip_level;
2286 if (dec_control)
2287 hw->dec_control = dec_control;
2288}
2289
2290static s32 vmpeg12_init(struct vdec_mpeg12_hw_s *hw)
2291{
2292 int size;
2293 u32 fw_size = 16*0x1000;
2294 struct firmware_s *fw;
2295
2296 vmpeg12_local_init(hw);
2297
2298 fw = vmalloc(sizeof(struct firmware_s) + fw_size);
2299 if (IS_ERR_OR_NULL(fw))
2300 return -ENOMEM;
2301
2302 pr_debug("get firmware ...\n");
2303 size = get_firmware_data(VIDEO_DEC_MPEG12_MULTI, fw->data);
2304 if (size < 0) {
2305 pr_err("get firmware fail.\n");
2306 vfree(fw);
2307 return -1;
2308 }
2309
2310 fw->len = size;
2311 hw->fw = fw;
2312
2313 INIT_WORK(&hw->userdata_push_work, userdata_push_do_work);
2314 INIT_WORK(&hw->work, vmpeg12_work);
2315 INIT_WORK(&hw->notify_work, vmpeg12_notify_work);
2316
2317 if (NULL == hw->user_data_buffer) {
2318 hw->user_data_buffer = kmalloc(USER_DATA_SIZE,
2319 GFP_KERNEL);
2320 if (!hw->user_data_buffer) {
2321 pr_info("%s: Can not allocate user_data_buffer\n",
2322 __func__);
2323 return -1;
2324 }
2325 }
2326
2327 vmmpeg2_crate_userdata_manager(hw,
2328 hw->user_data_buffer,
2329 USER_DATA_SIZE);
2330
2331 amvdec_enable();
2332 init_timer(&hw->check_timer);
2333 hw->check_timer.data = (unsigned long)hw;
2334 hw->check_timer.function = check_timer_func;
2335 hw->check_timer.expires = jiffies + CHECK_INTERVAL;
2336
2337 hw->stat |= STAT_TIMER_ARM;
2338 hw->stat |= STAT_ISR_REG;
2339
2340 hw->buf_start = 0;
2341 WRITE_VREG(DECODE_STOP_POS, udebug_flag);
2342
2343 return 0;
2344}
2345
2346static unsigned long run_ready(struct vdec_s *vdec, unsigned long mask)
2347{
2348 int index;
2349
2350 struct vdec_mpeg12_hw_s *hw =
2351 (struct vdec_mpeg12_hw_s *)vdec->private;
2352 if (hw->eos)
2353 return 0;
2354 if (vdec_stream_based(vdec) && (hw->init_flag == 0)
2355 && pre_decode_buf_level != 0) {
2356 u32 rp, wp, level;
2357
2358 rp = READ_PARSER_REG(PARSER_VIDEO_RP);
2359 wp = READ_PARSER_REG(PARSER_VIDEO_WP);
2360 if (wp < rp)
2361 level = vdec->input.size + wp - rp;
2362 else
2363 level = wp - rp;
2364
2365 if (level < pre_decode_buf_level) {
2366 hw->not_run_ready++;
2367 return 0;
2368 }
2369 }
2370
2371#ifdef AGAIN_HAS_THRESHOLD
2372 if (hw->next_again_flag&&
2373 (!vdec_frame_based(vdec))) {
2374 u32 parser_wr_ptr =
2375 READ_PARSER_REG(PARSER_VIDEO_WP);
2376 if (parser_wr_ptr >= hw->pre_parser_wr_ptr &&
2377 (parser_wr_ptr - hw->pre_parser_wr_ptr) <
2378 again_threshold) {
2379 int r = vdec_sync_input(vdec);
2380 debug_print(DECODE_ID(hw), PRINT_FLAG_RUN_FLOW,
2381 "%s buf level%x\n",
2382 __func__, r);
2383 return 0;
2384 }
2385 }
2386#endif
2387
2388 index = find_buffer(hw);
2389 if (index >= DECODE_BUFFER_NUM_MAX) {
2390 hw->buffer_not_ready++;
2391 return 0;
2392 }
2393 hw->not_run_ready = 0;
2394 hw->buffer_not_ready = 0;
2395 if (vdec->parallel_dec == 1)
2396 return (unsigned long)(CORE_MASK_VDEC_1);
2397 else
2398 return (unsigned long)(CORE_MASK_VDEC_1 | CORE_MASK_HEVC);
2399}
2400
2401static unsigned char get_data_check_sum
2402 (struct vdec_mpeg12_hw_s *hw, int size)
2403{
2404 int jj;
2405 int sum = 0;
2406 u8 *data = NULL;
2407
2408 if (!hw->chunk->block->is_mapped)
2409 data = codec_mm_vmap(hw->chunk->block->start +
2410 hw->chunk->offset, size);
2411 else
2412 data = ((u8 *)hw->chunk->block->start_virt) +
2413 hw->chunk->offset;
2414
2415 for (jj = 0; jj < size; jj++)
2416 sum += data[jj];
2417
2418 if (!hw->chunk->block->is_mapped)
2419 codec_mm_unmap_phyaddr(data);
2420 return sum;
2421}
2422
2423static void run(struct vdec_s *vdec, unsigned long mask,
2424void (*callback)(struct vdec_s *, void *),
2425 void *arg)
2426{
2427 struct vdec_mpeg12_hw_s *hw =
2428 (struct vdec_mpeg12_hw_s *)vdec->private;
2429 int save_reg = READ_VREG(POWER_CTL_VLD);
2430 int size, ret;
2431 /* reset everything except DOS_TOP[1] and APB_CBUS[0]*/
2432 WRITE_VREG(DOS_SW_RESET0, 0xfffffff0);
2433 WRITE_VREG(DOS_SW_RESET0, 0);
2434 WRITE_VREG(POWER_CTL_VLD, save_reg);
2435 hw->run_count++;
2436 vdec_reset_core(vdec);
2437 hw->vdec_cb_arg = arg;
2438 hw->vdec_cb = callback;
2439
2440#ifdef AGAIN_HAS_THRESHOLD
2441 hw->pre_parser_wr_ptr =
2442 READ_PARSER_REG(PARSER_VIDEO_WP);
2443 hw->next_again_flag = 0;
2444#endif
2445
2446 size = vdec_prepare_input(vdec, &hw->chunk);
2447 if (size < 0) {
2448 hw->input_empty++;
2449 hw->dec_result = DEC_RESULT_AGAIN;
2450 vdec_schedule_work(&hw->work);
2451 return;
2452 }
2453 if (vdec_frame_based(vdec) && debug_enable) {
2454 u8 *data = NULL;
2455
2456
2457 if (hw->chunk)
2458 debug_print(DECODE_ID(hw), PRINT_FLAG_RUN_FLOW,
2459 "run: chunk offset 0x%x, size %d\n",
2460 hw->chunk->offset, hw->chunk->size);
2461
2462 if (!hw->chunk->block->is_mapped)
2463 data = codec_mm_vmap(hw->chunk->block->start +
2464 hw->chunk->offset, size);
2465 else
2466 data = ((u8 *)hw->chunk->block->start_virt) +
2467 hw->chunk->offset;
2468
2469 if (debug_enable & PRINT_FLAG_VDEC_STATUS
2470 ) {
2471 debug_print(DECODE_ID(hw), 0,
2472 "%s: size 0x%x sum 0x%x %02x %02x %02x %02x %02x %02x .. %02x %02x %02x %02x\n",
2473 __func__, size, get_data_check_sum(hw, size),
2474 data[0], data[1], data[2], data[3],
2475 data[4], data[5], data[size - 4],
2476 data[size - 3], data[size - 2],
2477 data[size - 1]);
2478 }
2479 if (debug_enable & PRINT_FRAMEBASE_DATA
2480 ) {
2481 int jj;
2482
2483 for (jj = 0; jj < size; jj++) {
2484 if ((jj & 0xf) == 0)
2485 debug_print(DECODE_ID(hw),
2486 PRINT_FRAMEBASE_DATA,
2487 "%06x:", jj);
2488 debug_print(DECODE_ID(hw),
2489 PRINT_FRAMEBASE_DATA,
2490 "%02x ", data[jj]);
2491 if (((jj + 1) & 0xf) == 0)
2492 debug_print(DECODE_ID(hw),
2493 PRINT_FRAMEBASE_DATA,
2494 "\n");
2495 }
2496 }
2497
2498 if (!hw->chunk->block->is_mapped)
2499 codec_mm_unmap_phyaddr(data);
2500 } else
2501 debug_print(DECODE_ID(hw), PRINT_FLAG_VDEC_STATUS,
2502 "%s: %x %x %x %x %x size 0x%x\n",
2503 __func__,
2504 READ_VREG(VLD_MEM_VIFIFO_LEVEL),
2505 READ_VREG(VLD_MEM_VIFIFO_WP),
2506 READ_VREG(VLD_MEM_VIFIFO_RP),
2507 READ_PARSER_REG(PARSER_VIDEO_RP),
2508 READ_PARSER_REG(PARSER_VIDEO_WP),
2509 size);
2510
2511
2512 hw->input_empty = 0;
2513 vdec_enable_input(vdec);
2514
2515 hw->dec_result = DEC_RESULT_NONE;
2516 if (vdec->mc_loaded) {
2517 /*firmware have load before,
2518 and not changes to another.
2519 ignore reload.
2520 */
2521 } else {
2522 ret = amvdec_vdec_loadmc_buf_ex(VFORMAT_MPEG12, "mmpeg12", vdec,
2523 hw->fw->data, hw->fw->len);
2524 if (ret < 0) {
2525 pr_err("[%d] %s: the %s fw loading failed, err: %x\n", vdec->id,
2526 hw->fw->name, tee_enabled() ? "TEE" : "local", ret);
2527 hw->dec_result = DEC_RESULT_FORCE_EXIT;
2528 vdec_schedule_work(&hw->work);
2529 return;
2530 }
2531 vdec->mc_loaded = 1;
2532 vdec->mc_type = VFORMAT_MPEG12;
2533 }
2534 if (vmpeg12_hw_ctx_restore(hw) < 0) {
2535 hw->dec_result = DEC_RESULT_ERROR;
2536 debug_print(DECODE_ID(hw), PRINT_FLAG_ERROR,
2537 "ammvdec_mpeg12: error HW context restore\n");
2538 vdec_schedule_work(&hw->work);
2539 return;
2540 }
2541 /*wmb();*/
2542 hw->stat |= STAT_MC_LOAD;
2543 hw->last_vld_level = 0;
2544 start_process_time(hw);
2545 amvdec_start();
2546 hw->stat |= STAT_VDEC_RUN;
2547 hw->init_flag = 1;
2548 mod_timer(&hw->check_timer, jiffies + CHECK_INTERVAL);
2549}
2550
2551static void reset(struct vdec_s *vdec)
2552{
2553 pr_info("ammvdec_mpeg12: reset.\n");
2554}
2555
2556static int ammvdec_mpeg12_probe(struct platform_device *pdev)
2557{
2558 struct vdec_s *pdata = *(struct vdec_s **)pdev->dev.platform_data;
2559 struct vdec_mpeg12_hw_s *hw = NULL;
2560
2561 pr_info("ammvdec_mpeg12 probe start.\n");
2562
2563 if (pdata == NULL) {
2564 pr_info("ammvdec_mpeg12 platform data undefined.\n");
2565 return -EFAULT;
2566 }
2567
2568 hw = vzalloc(sizeof(struct vdec_mpeg12_hw_s));
2569 if (hw == NULL) {
2570 pr_info("\nammvdec_mpeg12 decoder driver alloc failed\n");
2571 return -ENOMEM;
2572 }
2573
2574 pdata->private = hw;
2575 pdata->dec_status = vmmpeg12_dec_status;
2576 pdata->run_ready = run_ready;
2577 pdata->run = run;
2578 pdata->reset = reset;
2579 pdata->irq_handler = vmpeg12_isr;
2580 pdata->threaded_irq_handler = vmpeg12_isr_thread_fn;
2581 pdata->dump_state = vmpeg2_dump_state;
2582
2583 pdata->user_data_read = vmmpeg2_user_data_read;
2584 pdata->reset_userdata_fifo = vmmpeg2_reset_userdata_fifo;
2585 pdata->wakeup_userdata_poll = vmmpeg2_wakeup_userdata_poll;
2586
2587 if (pdata->use_vfm_path) {
2588 snprintf(pdata->vf_provider_name, VDEC_PROVIDER_NAME_SIZE,
2589 VFM_DEC_PROVIDER_NAME);
2590 hw->frameinfo_enable = 1;
2591 }
2592 else
2593 snprintf(pdata->vf_provider_name, VDEC_PROVIDER_NAME_SIZE,
2594 PROVIDER_NAME ".%02x", pdev->id & 0xff);
2595 if (pdata->parallel_dec == 1) {
2596 int i;
2597 for (i = 0; i < DECODE_BUFFER_NUM_MAX; i++)
2598 hw->canvas_spec[i] = 0xffffff;
2599 }
2600 vf_provider_init(&pdata->vframe_provider, pdata->vf_provider_name,
2601 &vf_provider_ops, pdata);
2602
2603 platform_set_drvdata(pdev, pdata);
2604 hw->canvas_mode = pdata->canvas_mode;
2605 hw->platform_dev = pdev;
2606
2607 hw->tvp_flag = vdec_secure(pdata) ? CODEC_MM_FLAGS_TVP : 0;
2608 if (pdata->sys_info)
2609 hw->vmpeg12_amstream_dec_info = *pdata->sys_info;
2610
2611 debug_print(DECODE_ID(hw), 0,
2612 "%s, sysinfo: %dx%d, tvp_flag = 0x%x\n",
2613 __func__,
2614 hw->vmpeg12_amstream_dec_info.width,
2615 hw->vmpeg12_amstream_dec_info.height,
2616 hw->tvp_flag);
2617
2618 if (vmpeg12_init(hw) < 0) {
2619 pr_info("ammvdec_mpeg12 init failed.\n");
2620 if (hw) {
2621 vfree(hw);
2622 hw = NULL;
2623 }
2624 pdata->dec_status = NULL;
2625 return -ENODEV;
2626 }
2627 if (pdata->parallel_dec == 1)
2628 vdec_core_request(pdata, CORE_MASK_VDEC_1);
2629 else {
2630 vdec_core_request(pdata, CORE_MASK_VDEC_1 | CORE_MASK_HEVC
2631 | CORE_MASK_COMBINE);
2632 }
2633#ifdef DUMP_USER_DATA
2634 amvdec_mmpeg12_init_userdata_dump(hw);
2635 reset_user_data_buf(hw);
2636#endif
2637
2638 /*INIT_WORK(&userdata_push_work, userdata_push_do_work);*/
2639 return 0;
2640}
2641
2642static int ammvdec_mpeg12_remove(struct platform_device *pdev)
2643
2644{
2645 struct vdec_mpeg12_hw_s *hw =
2646 (struct vdec_mpeg12_hw_s *)
2647 (((struct vdec_s *)(platform_get_drvdata(pdev)))->private);
2648 struct vdec_s *vdec = hw_to_vdec(hw);
2649 int i;
2650
2651 if (hw->stat & STAT_VDEC_RUN) {
2652 amvdec_stop();
2653 hw->stat &= ~STAT_VDEC_RUN;
2654 }
2655
2656 if (hw->stat & STAT_ISR_REG) {
2657 vdec_free_irq(VDEC_IRQ_1, (void *)hw);
2658 hw->stat &= ~STAT_ISR_REG;
2659 }
2660
2661 if (hw->stat & STAT_TIMER_ARM) {
2662 del_timer_sync(&hw->check_timer);
2663 hw->stat &= ~STAT_TIMER_ARM;
2664 }
2665 cancel_work_sync(&hw->userdata_push_work);
2666 cancel_work_sync(&hw->notify_work);
2667 cancel_work_sync(&hw->work);
2668
2669 if (hw->mm_blk_handle) {
2670 decoder_bmmu_box_free(hw->mm_blk_handle);
2671 hw->mm_blk_handle = NULL;
2672 }
2673 if (vdec->parallel_dec == 1)
2674 vdec_core_release(hw_to_vdec(hw), CORE_MASK_VDEC_1);
2675 else
2676 vdec_core_release(hw_to_vdec(hw), CORE_MASK_VDEC_1 | CORE_MASK_HEVC);
2677 vdec_set_status(hw_to_vdec(hw), VDEC_STATUS_DISCONNECTED);
2678
2679 if (vdec->parallel_dec == 1) {
2680 for (i = 0; i < DECODE_BUFFER_NUM_MAX; i++) {
2681 vdec->free_canvas_ex(canvas_y(hw->canvas_spec[i]), vdec->id);
2682 vdec->free_canvas_ex(canvas_u(hw->canvas_spec[i]), vdec->id);
2683 }
2684 }
2685
2686 if (hw->ccbuf_phyAddress_is_remaped_nocache)
2687 codec_mm_unmap_phyaddr(hw->ccbuf_phyAddress_virt);
2688 hw->ccbuf_phyAddress_virt = NULL;
2689 hw->ccbuf_phyAddress = 0;
2690 hw->ccbuf_phyAddress_is_remaped_nocache = 0;
2691
2692 if (hw->user_data_buffer != NULL) {
2693 kfree(hw->user_data_buffer);
2694 hw->user_data_buffer = NULL;
2695 }
2696 vmmpeg2_destroy_userdata_manager(hw);
2697
2698#ifdef DUMP_USER_DATA
2699 amvdec_mmpeg12_uninit_userdata_dump(hw);
2700#endif
2701
2702 if (hw->fw) {
2703 vfree(hw->fw);
2704 hw->fw = NULL;
2705 }
2706 if (hw) {
2707 vfree(hw);
2708 hw = NULL;
2709 }
2710 pr_info("ammvdec_mpeg12 removed.\n");
2711 memset(&gvs, 0x0, sizeof(gvs));
2712
2713 return 0;
2714}
2715
2716/****************************************/
2717#ifdef CONFIG_PM
2718static int mmpeg12_suspend(struct device *dev)
2719{
2720 amvdec_suspend(to_platform_device(dev), dev->power.power_state);
2721 return 0;
2722}
2723
2724static int mmpeg12_resume(struct device *dev)
2725{
2726 amvdec_resume(to_platform_device(dev));
2727 return 0;
2728}
2729
2730static const struct dev_pm_ops mmpeg12_pm_ops = {
2731 SET_SYSTEM_SLEEP_PM_OPS(mmpeg12_suspend, mmpeg12_resume)
2732};
2733#endif
2734
2735static struct platform_driver ammvdec_mpeg12_driver = {
2736 .probe = ammvdec_mpeg12_probe,
2737 .remove = ammvdec_mpeg12_remove,
2738 .driver = {
2739 .name = DRIVER_NAME,
2740#ifdef CONFIG_PM
2741 .pm = &mmpeg12_pm_ops,
2742#endif
2743 }
2744};
2745
2746static struct codec_profile_t ammvdec_mpeg12_profile = {
2747 .name = "mmpeg12",
2748 .profile = ""
2749};
2750
2751static struct mconfig mmpeg12_configs[] = {
2752 MC_PU32("stat", &stat),
2753 MC_PU32("radr", &radr),
2754 MC_PU32("rval", &rval),
2755 MC_PU32("dec_control", &dec_control),
2756 MC_PU32("error_frame_skip_level", &error_frame_skip_level),
2757 MC_PU32("decode_timeout_val", &decode_timeout_val),
2758};
2759static struct mconfig_node mmpeg12_node;
2760
2761static int __init ammvdec_mpeg12_driver_init_module(void)
2762{
2763 pr_info("ammvdec_mpeg12 module init\n");
2764
2765 if (platform_driver_register(&ammvdec_mpeg12_driver)) {
2766 pr_info("failed to register ammvdec_mpeg12 driver\n");
2767 return -ENODEV;
2768 }
2769 vcodec_profile_register(&ammvdec_mpeg12_profile);
2770 INIT_REG_NODE_CONFIGS("media.decoder", &mmpeg12_node,
2771 "mmpeg12", mmpeg12_configs, CONFIG_FOR_RW);
2772 return 0;
2773}
2774
2775static void __exit ammvdec_mpeg12_driver_remove_module(void)
2776{
2777 pr_info("ammvdec_mpeg12 module exit.\n");
2778 platform_driver_unregister(&ammvdec_mpeg12_driver);
2779}
2780
2781/****************************************/
2782module_param(dec_control, uint, 0664);
2783MODULE_PARM_DESC(dec_control, "\n ammvdec_mpeg12 decoder control\n");
2784module_param(error_frame_skip_level, uint, 0664);
2785MODULE_PARM_DESC(error_frame_skip_level,
2786 "\n ammvdec_mpeg12 error_frame_skip_level\n");
2787
2788module_param(radr, uint, 0664);
2789MODULE_PARM_DESC(radr, "\nradr\n");
2790
2791module_param(rval, uint, 0664);
2792MODULE_PARM_DESC(rval, "\nrval\n");
2793
2794module_param(debug_enable, uint, 0664);
2795MODULE_PARM_DESC(debug_enable,
2796 "\n ammvdec_mpeg12 debug enable\n");
2797module_param(pre_decode_buf_level, int, 0664);
2798MODULE_PARM_DESC(pre_decode_buf_level,
2799 "\n ammvdec_mpeg12 pre_decode_buf_level\n");
2800module_param(decode_timeout_val, uint, 0664);
2801MODULE_PARM_DESC(decode_timeout_val, "\n ammvdec_mpeg12 decode_timeout_val\n");
2802
2803module_param_array(max_process_time, uint, &max_decode_instance_num, 0664);
2804
2805module_param(udebug_flag, uint, 0664);
2806MODULE_PARM_DESC(udebug_flag, "\n ammvdec_mpeg12 udebug_flag\n");
2807
2808
2809#ifdef AGAIN_HAS_THRESHOLD
2810module_param(again_threshold, uint, 0664);
2811MODULE_PARM_DESC(again_threshold, "\n again_threshold\n");
2812#endif
2813
2814module_param(without_display_mode, uint, 0664);
2815MODULE_PARM_DESC(without_display_mode, "\n ammvdec_mpeg12 without_display_mode\n");
2816
2817
2818module_init(ammvdec_mpeg12_driver_init_module);
2819module_exit(ammvdec_mpeg12_driver_remove_module);
2820
2821MODULE_DESCRIPTION("AMLOGIC MULTI MPEG1/2 Video Decoder Driver");
2822MODULE_LICENSE("GPL");
2823
2824
2825