summaryrefslogtreecommitdiff
path: root/drivers/frame_provider/decoder/mpeg12/vmpeg12_multi.c (plain)
blob: a6f25beae54211dd12cccd0c609191ada11110fd
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;
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 irqreturn_t vmpeg12_isr_thread_fn(struct vdec_s *vdec, int irq)
1413{
1414 u32 reg, index, info, seqinfo, offset, pts, frame_size, tmp;
1415 u64 pts_us64 = 0;
1416 struct pic_info_t *new_pic, *disp_pic;
1417 struct vdec_mpeg12_hw_s *hw =
1418 (struct vdec_mpeg12_hw_s *)(vdec->private);
1419
1420 if (READ_VREG(AV_SCRATCH_M) != 0 &&
1421 (debug_enable & PRINT_FLAG_UCODE_DETAIL)) {
1422
1423 debug_print(DECODE_ID(hw), PRINT_FLAG_DEC_DETAIL,
1424 "dbg %x: %x, level %x, wp %x, rp %x, cnt %x\n",
1425 READ_VREG(AV_SCRATCH_M), READ_VREG(AV_SCRATCH_N),
1426 READ_VREG(VLD_MEM_VIFIFO_LEVEL),
1427 READ_VREG(VLD_MEM_VIFIFO_WP),
1428 READ_VREG(VLD_MEM_VIFIFO_RP),
1429 READ_VREG(VIFF_BIT_CNT));
1430 WRITE_VREG(AV_SCRATCH_M, 0);
1431 return IRQ_HANDLED;
1432 }
1433
1434 reg = READ_VREG(AV_SCRATCH_J);
1435 if (reg & (1<<16)) {
1436 vdec_schedule_work(&hw->userdata_push_work);
1437 return IRQ_HANDLED;
1438 }
1439
1440 reg = READ_VREG(MREG_BUFFEROUT);
1441 if (reg == 2) {
1442 /*timeout when decoding next frame*/
1443 debug_print(DECODE_ID(hw), PRINT_FLAG_VLD_DETAIL,
1444 "mmpeg12: lack data, lvl=%x ctrl=%x bcnt=%x\n",
1445 READ_VREG(VLD_MEM_VIFIFO_LEVEL),
1446 READ_VREG(VLD_MEM_VIFIFO_CONTROL),
1447 READ_VREG(VIFF_BIT_CNT));
1448
1449 if (vdec_frame_based(vdec)) {
1450 /* vmpeg12_save_hw_context(hw); */
1451 reset_process_time(hw);
1452 hw->dec_result = DEC_RESULT_DONE;
1453 vdec_schedule_work(&hw->work);
1454 } else {
1455 hw->dec_result = DEC_RESULT_AGAIN;
1456 vdec_schedule_work(&hw->work);
1457 userdata_pushed_drop(hw);
1458 reset_process_time(hw);
1459 }
1460 return IRQ_HANDLED;
1461 } else {
1462 reset_process_time(hw);
1463
1464 info = READ_VREG(MREG_PIC_INFO);
1465 offset = READ_VREG(MREG_FRAME_OFFSET);
1466 index = spec_to_index(hw, READ_VREG(REC_CANVAS_ADDR));
1467 seqinfo = READ_VREG(MREG_SEQ_INFO);
1468
1469 if ((info & PICINFO_PROG) == 0 &&
1470 (info & FRAME_PICTURE_MASK) != FRAME_PICTURE)
1471 hw->first_i_frame_ready = 1; /* for field struct case*/
1472
1473 if (index >= DECODE_BUFFER_NUM_MAX) {
1474 debug_print(DECODE_ID(hw), PRINT_FLAG_ERROR,
1475 "mmpeg12: invalid buf index: %d\n", index);
1476 hw->dec_result = DEC_RESULT_ERROR;
1477 vdec_schedule_work(&hw->work);
1478 return IRQ_HANDLED;
1479 }
1480 hw->dec_num++;
1481 hw->dec_result = DEC_RESULT_DONE;
1482 new_pic = &hw->pics[index];
1483 tmp = READ_VREG(MREG_PIC_WIDTH);
1484 if ((tmp > 1920) || (tmp == 0)) {
1485 new_pic->width = 1920;
1486 hw->frame_width = 1920;
1487 } else {
1488 new_pic->width = tmp;
1489 hw->frame_width = tmp;
1490 }
1491
1492 tmp = READ_VREG(MREG_PIC_HEIGHT);
1493 if ((tmp > 1088) || (tmp == 0)) {
1494 new_pic->height = 1088;
1495 hw->frame_height = 1088;
1496 } else {
1497 new_pic->height = tmp;
1498 hw->frame_height = tmp;
1499 }
1500
1501 new_pic->buffer_info = info;
1502 new_pic->offset = offset;
1503 new_pic->index = index;
1504 if (((info & PICINFO_TYPE_MASK) == PICINFO_TYPE_I) ||
1505 ((info & PICINFO_TYPE_MASK) == PICINFO_TYPE_P)) {
1506 if (hw->chunk) {
1507 new_pic->pts_valid = hw->chunk->pts_valid;
1508 new_pic->pts = hw->chunk->pts;
1509 new_pic->pts64 = hw->chunk->pts64;
1510 if (hw->last_chunk_pts == hw->chunk->pts) {
1511 new_pic->pts_valid = 0;
1512 debug_print(DECODE_ID(hw), PRINT_FLAG_TIMEINFO,
1513 "pts invalid\n");
1514 }
1515 } else {
1516 if (pts_lookup_offset_us64(PTS_TYPE_VIDEO, offset,
1517 &pts, &frame_size, 0, &pts_us64) == 0) {
1518 new_pic->pts_valid = true;
1519 new_pic->pts = pts;
1520 new_pic->pts64 = pts_us64;
1521 } else
1522 new_pic->pts_valid = false;
1523 }
1524 } else {
1525 if (hw->chunk)
1526 hw->last_chunk_pts = hw->chunk->pts;
1527 new_pic->pts_valid = false;
1528 }
1529
1530 debug_print(DECODE_ID(hw), PRINT_FLAG_RUN_FLOW,
1531 "mmpeg12: new_pic=%d, ind=%d, info=%x, seq=%x, offset=%d\n",
1532 hw->dec_num, index, info, seqinfo, offset);
1533
1534 hw->frame_prog = info & PICINFO_PROG;
1535 if ((seqinfo & SEQINFO_EXT_AVAILABLE) &&
1536 ((seqinfo & SEQINFO_PROG) == 0))
1537 hw->frame_prog = 0;
1538 force_interlace_check(hw);
1539
1540 if (((info & PICINFO_TYPE_MASK) == PICINFO_TYPE_I) ||
1541 ((info & PICINFO_TYPE_MASK) == PICINFO_TYPE_P)) {
1542 index = update_reference(hw, index);
1543 } else {
1544 /* drop b frame before reference pic ready */
1545 if (hw->refs[0] == -1)
1546 index = DECODE_BUFFER_NUM_MAX;
1547 }
1548 vmpeg12_save_hw_context(hw, reg);
1549
1550 if (index >= DECODE_BUFFER_NUM_MAX) {
1551 if (hw->dec_num != 2) {
1552 debug_print(DECODE_ID(hw), 0,
1553 "mmpeg12: drop pic num %d, type %c, index %d, offset %x\n",
1554 hw->dec_num, GET_SLICE_TYPE(info), index, offset);
1555 hw->dec_result = DEC_RESULT_ERROR;
1556 }
1557 vdec_schedule_work(&hw->work);
1558 return IRQ_HANDLED;
1559 }
1560
1561 disp_pic = &hw->pics[index];
1562 info = hw->pics[index].buffer_info;
1563 if (disp_pic->pts_valid && hw->lastpts64 == disp_pic->pts64)
1564 disp_pic->pts_valid = false;
1565 if (disp_pic->pts_valid)
1566 hw->lastpts64 = disp_pic->pts64;
1567
1568 if ((hw->first_i_frame_ready == 0) &&
1569 ((info & PICINFO_TYPE_MASK) == PICINFO_TYPE_I) &&
1570 ((info & PICINFO_ERROR) == 0))
1571 hw->first_i_frame_ready = 1;
1572
1573 debug_print(DECODE_ID(hw), PRINT_FLAG_RUN_FLOW,
1574 "mmpeg12: disp_pic=%d(%c), ind=%d, offst=%x, pts=(%d,%lld)(%d)\n",
1575 hw->disp_num, GET_SLICE_TYPE(info), index, disp_pic->offset,
1576 disp_pic->pts, disp_pic->pts64, disp_pic->pts_valid);
1577
1578 prepare_display_buf(hw, disp_pic);
1579 vdec_schedule_work(&hw->work);
1580 }
1581
1582 return IRQ_HANDLED;
1583}
1584static irqreturn_t vmpeg12_isr(struct vdec_s *vdec, int irq)
1585{
1586 u32 info, offset;
1587 struct vdec_mpeg12_hw_s *hw =
1588 (struct vdec_mpeg12_hw_s *)(vdec->private);
1589 if (hw->eos)
1590 return IRQ_HANDLED;
1591 info = READ_VREG(MREG_PIC_INFO);
1592 offset = READ_VREG(MREG_FRAME_OFFSET);
1593
1594 vdec_count_info(&gvs, info & PICINFO_ERROR, offset);
1595
1596 WRITE_VREG(ASSIST_MBOX1_CLR_REG, 1);
1597
1598 return IRQ_WAKE_THREAD;
1599}
1600
1601static void vmpeg12_notify_work(struct work_struct *work)
1602{
1603 struct vdec_mpeg12_hw_s *hw = container_of(work,
1604 struct vdec_mpeg12_hw_s, notify_work);
1605 struct vdec_s *vdec = hw_to_vdec(hw);
1606
1607 if (vdec->fr_hint_state == VDEC_NEED_HINT) {
1608 vf_notify_receiver(vdec->vf_provider_name,
1609 VFRAME_EVENT_PROVIDER_FR_HINT,
1610 (void *)((unsigned long)hw->frame_dur));
1611 vdec->fr_hint_state = VDEC_HINTED;
1612 }
1613}
1614
1615static void wait_vmmpeg12_search_done(struct vdec_mpeg12_hw_s *hw)
1616{
1617 u32 vld_rp = READ_VREG(VLD_MEM_VIFIFO_RP);
1618 int count = 0;
1619
1620 do {
1621 usleep_range(100, 500);
1622 if (vld_rp == READ_VREG(VLD_MEM_VIFIFO_RP))
1623 break;
1624 if (count > 1000) {
1625 debug_print(DECODE_ID(hw), 0,
1626 "%s, count %d vld_rp 0x%x VLD_MEM_VIFIFO_RP 0x%x\n",
1627 __func__, count, vld_rp, READ_VREG(VLD_MEM_VIFIFO_RP));
1628 break;
1629 } else
1630 vld_rp = READ_VREG(VLD_MEM_VIFIFO_RP);
1631 count++;
1632 } while (1);
1633}
1634
1635static void flush_output(struct vdec_mpeg12_hw_s *hw)
1636{
1637 int index = hw->refs[1];
1638
1639 /* video only one frame need not flush. */
1640 if (hw->dec_num < 2)
1641 return;
1642
1643 if (index >= 0 && index < DECODE_BUFFER_NUM_MAX)
1644 prepare_display_buf(hw, &hw->pics[index]);
1645}
1646
1647static void vmpeg12_work(struct work_struct *work)
1648{
1649 struct vdec_mpeg12_hw_s *hw =
1650 container_of(work, struct vdec_mpeg12_hw_s, work);
1651 struct vdec_s *vdec = hw_to_vdec(hw);
1652
1653 if (hw->dec_result != DEC_RESULT_DONE)
1654 debug_print(DECODE_ID(hw), PRINT_FLAG_RUN_FLOW,
1655 "%s, result=%d, status=%d\n", __func__,
1656 hw->dec_result, vdec->next_status);
1657
1658 if (hw->dec_result == DEC_RESULT_DONE) {
1659 vdec_vframe_dirty(vdec, hw->chunk);
1660 hw->chunk = NULL;
1661 } else if (hw->dec_result == DEC_RESULT_AGAIN &&
1662 (vdec->next_status != VDEC_STATUS_DISCONNECTED)) {
1663 /*
1664 stream base: stream buf empty or timeout
1665 frame base: vdec_prepare_input fail
1666 */
1667 if (!vdec_has_more_input(vdec)) {
1668 hw->dec_result = DEC_RESULT_EOS;
1669 vdec_schedule_work(&hw->work);
1670 return;
1671 }
1672#ifdef AGAIN_HAS_THRESHOLD
1673 hw->next_again_flag = 1;
1674#endif
1675 } else if (hw->dec_result == DEC_RESULT_GET_DATA &&
1676 vdec->next_status != VDEC_STATUS_DISCONNECTED) {
1677 if (!vdec_has_more_input(vdec)) {
1678 hw->dec_result = DEC_RESULT_EOS;
1679 vdec_schedule_work(&hw->work);
1680 return;
1681 }
1682 debug_print(DECODE_ID(hw), PRINT_FLAG_VLD_DETAIL,
1683 "%s DEC_RESULT_GET_DATA %x %x %x\n",
1684 __func__,
1685 READ_VREG(VLD_MEM_VIFIFO_LEVEL),
1686 READ_VREG(VLD_MEM_VIFIFO_WP),
1687 READ_VREG(VLD_MEM_VIFIFO_RP));
1688 vdec_vframe_dirty(vdec, hw->chunk);
1689 hw->chunk = NULL;
1690 vdec_clean_input(vdec);
1691 return;
1692 } else if (hw->dec_result == DEC_RESULT_FORCE_EXIT) {
1693 debug_print(DECODE_ID(hw), PRINT_FLAG_ERROR,
1694 "%s: force exit\n", __func__);
1695 if (hw->stat & STAT_ISR_REG) {
1696 amvdec_stop();
1697 vdec_free_irq(VDEC_IRQ_1, (void *)hw);
1698 hw->stat &= ~STAT_ISR_REG;
1699 }
1700 } else if (hw->dec_result == DEC_RESULT_EOS) {
1701 if (hw->stat & STAT_VDEC_RUN) {
1702 amvdec_stop();
1703 hw->stat &= ~STAT_VDEC_RUN;
1704 }
1705 hw->eos = 1;
1706 vdec_vframe_dirty(vdec, hw->chunk);
1707 hw->chunk = NULL;
1708 vdec_clean_input(vdec);
1709 flush_output(hw);
1710 debug_print(DECODE_ID(hw), 0,
1711 "%s: end of stream, num %d(%d)\n",
1712 __func__, hw->disp_num, hw->dec_num);
1713 }
1714 if (hw->stat & STAT_VDEC_RUN) {
1715 amvdec_stop();
1716 hw->stat &= ~STAT_VDEC_RUN;
1717 }
1718 /*disable mbox interrupt */
1719 WRITE_VREG(ASSIST_MBOX1_MASK, 0);
1720 wait_vmmpeg12_search_done(hw);
1721 if (vdec->parallel_dec == 1)
1722 vdec_core_finish_run(vdec, CORE_MASK_VDEC_1);
1723 else
1724 vdec_core_finish_run(vdec, CORE_MASK_VDEC_1 | CORE_MASK_HEVC);
1725 del_timer_sync(&hw->check_timer);
1726 hw->stat &= ~STAT_TIMER_ARM;
1727
1728 if (hw->vdec_cb)
1729 hw->vdec_cb(vdec, hw->vdec_cb_arg);
1730}
1731
1732static struct vframe_s *vmpeg_vf_peek(void *op_arg)
1733{
1734 struct vframe_s *vf;
1735 struct vdec_s *vdec = op_arg;
1736 struct vdec_mpeg12_hw_s *hw =
1737 (struct vdec_mpeg12_hw_s *)vdec->private;
1738 hw->peek_num++;
1739 if (kfifo_peek(&hw->display_q, &vf))
1740 return vf;
1741
1742 return NULL;
1743}
1744
1745static struct vframe_s *vmpeg_vf_get(void *op_arg)
1746{
1747 struct vframe_s *vf;
1748 struct vdec_s *vdec = op_arg;
1749 struct vdec_mpeg12_hw_s *hw =
1750 (struct vdec_mpeg12_hw_s *)vdec->private;
1751
1752 hw->get_num++;
1753 if (kfifo_get(&hw->display_q, &vf))
1754 return vf;
1755
1756 return NULL;
1757}
1758
1759static void vmpeg_vf_put(struct vframe_s *vf, void *op_arg)
1760{
1761 struct vdec_s *vdec = op_arg;
1762 struct vdec_mpeg12_hw_s *hw =
1763 (struct vdec_mpeg12_hw_s *)vdec->private;
1764
1765 hw->vfbuf_use[vf->index]--;
1766 hw->put_num++;
1767 kfifo_put(&hw->newframe_q,
1768 (const struct vframe_s *)vf);
1769 debug_print(DECODE_ID(hw), PRINT_FLAG_RUN_FLOW,
1770 "%s: index %d, use %d\n", __func__,
1771 vf->index, hw->vfbuf_use[vf->index]);
1772}
1773
1774static int vmpeg_event_cb(int type, void *data, void *private_data)
1775{
1776 return 0;
1777}
1778
1779static int vmpeg_vf_states(struct vframe_states *states, void *op_arg)
1780{
1781 unsigned long flags;
1782 struct vdec_s *vdec = op_arg;
1783 struct vdec_mpeg12_hw_s *hw =
1784 (struct vdec_mpeg12_hw_s *)vdec->private;
1785
1786 spin_lock_irqsave(&hw->lock, flags);
1787
1788 states->vf_pool_size = VF_POOL_SIZE;
1789 states->buf_free_num = kfifo_len(&hw->newframe_q);
1790 states->buf_avail_num = kfifo_len(&hw->display_q);
1791 states->buf_recycle_num = 0;
1792
1793 spin_unlock_irqrestore(&hw->lock, flags);
1794 return 0;
1795}
1796static int vmmpeg12_dec_status(struct vdec_s *vdec, struct vdec_info *vstatus)
1797{
1798 struct vdec_mpeg12_hw_s *hw =
1799 (struct vdec_mpeg12_hw_s *)vdec->private;
1800
1801 if (!hw)
1802 return -1;
1803
1804 vstatus->frame_width = hw->frame_width;
1805 vstatus->frame_height = hw->frame_height;
1806 if (hw->frame_dur != 0)
1807 vstatus->frame_rate = 96000 / hw->frame_dur;
1808 else
1809 vstatus->frame_rate = -1;
1810 vstatus->error_count = READ_VREG(AV_SCRATCH_C);
1811 vstatus->status = hw->stat;
1812 vstatus->bit_rate = gvs.bit_rate;
1813 vstatus->frame_dur = hw->frame_dur;
1814 vstatus->frame_data = gvs.frame_data;
1815 vstatus->total_data = gvs.total_data;
1816 vstatus->frame_count = gvs.frame_count;
1817 vstatus->error_frame_count = gvs.error_frame_count;
1818 vstatus->drop_frame_count = hw->drop_frame_count;
1819 vstatus->total_data = gvs.total_data;
1820 vstatus->samp_cnt = gvs.samp_cnt;
1821 vstatus->offset = gvs.offset;
1822 vstatus->ratio_control = hw->ratio_control;
1823 snprintf(vstatus->vdec_name, sizeof(vstatus->vdec_name),
1824 "%s", DRIVER_NAME);
1825
1826 return 0;
1827}
1828
1829
1830
1831/****************************************/
1832static void vmpeg12_canvas_init(struct vdec_mpeg12_hw_s *hw)
1833{
1834 int i, ret;
1835 u32 canvas_width, canvas_height;
1836 u32 decbuf_size, decbuf_y_size, decbuf_uv_size;
1837 unsigned long decbuf_start;
1838 /*u32 disp_addr = 0xffffffff;*/
1839 struct vdec_s *vdec = hw_to_vdec(hw);
1840
1841 if (buf_size <= 0x00400000) {
1842 /* SD only */
1843 canvas_width = 768;
1844 canvas_height = 576;
1845 decbuf_y_size = 0x80000;
1846 decbuf_uv_size = 0x20000;
1847 decbuf_size = 0x100000;
1848 } else {
1849 /* HD & SD */
1850 canvas_width = 1920;
1851 canvas_height = 1088;
1852 decbuf_y_size = 0x200000;
1853 decbuf_uv_size = 0x80000;
1854 decbuf_size = 0x300000;
1855 }
1856
1857 for (i = 0; i < MAX_BMMU_BUFFER_NUM; i++) {
1858
1859 unsigned canvas;
1860 if (i == (MAX_BMMU_BUFFER_NUM - 1)) /* SWAP&CCBUF&MATIRX&MV */
1861 decbuf_size = WORKSPACE_SIZE;
1862 ret = decoder_bmmu_box_alloc_buf_phy(hw->mm_blk_handle, i,
1863 decbuf_size, DRIVER_NAME, &decbuf_start);
1864 if (ret < 0) {
1865 pr_err("mmu alloc failed! size 0x%d idx %d\n",
1866 decbuf_size, i);
1867 return;
1868 }
1869
1870 if (i == (MAX_BMMU_BUFFER_NUM - 1)) {
1871 if (hw->ccbuf_phyAddress_is_remaped_nocache)
1872 codec_mm_unmap_phyaddr(hw->ccbuf_phyAddress_virt);
1873 hw->ccbuf_phyAddress_virt = NULL;
1874 hw->ccbuf_phyAddress = 0;
1875 hw->ccbuf_phyAddress_is_remaped_nocache = 0;
1876
1877 hw->buf_start = decbuf_start;
1878 hw->ccbuf_phyAddress = hw->buf_start + CTX_CCBUF_OFFSET;
1879 hw->ccbuf_phyAddress_virt
1880 = codec_mm_phys_to_virt(
1881 hw->ccbuf_phyAddress);
1882 if ((!hw->ccbuf_phyAddress_virt) && (!hw->tvp_flag)) {
1883 hw->ccbuf_phyAddress_virt
1884 = codec_mm_vmap(
1885 hw->ccbuf_phyAddress,
1886 CCBUF_SIZE);
1887 hw->ccbuf_phyAddress_is_remaped_nocache = 1;
1888 }
1889
1890 WRITE_VREG(MREG_CO_MV_START, hw->buf_start);
1891 } else {
1892 if (vdec->parallel_dec == 1) {
1893 unsigned tmp;
1894 if (canvas_u(hw->canvas_spec[i]) == 0xff) {
1895 tmp =
1896 vdec->get_canvas_ex(CORE_MASK_VDEC_1, vdec->id);
1897 hw->canvas_spec[i] &= ~(0xffff << 8);
1898 hw->canvas_spec[i] |= tmp << 8;
1899 hw->canvas_spec[i] |= tmp << 16;
1900 }
1901 if (canvas_y(hw->canvas_spec[i]) == 0xff) {
1902 tmp =
1903 vdec->get_canvas_ex(CORE_MASK_VDEC_1, vdec->id);
1904 hw->canvas_spec[i] &= ~0xff;
1905 hw->canvas_spec[i] |= tmp;
1906 }
1907 canvas = hw->canvas_spec[i];
1908 } else {
1909 canvas = vdec->get_canvas(i, 2);
1910 hw->canvas_spec[i] = canvas;
1911 }
1912
1913 hw->canvas_config[i][0].phy_addr =
1914 decbuf_start;
1915 hw->canvas_config[i][0].width =
1916 canvas_width;
1917 hw->canvas_config[i][0].height =
1918 canvas_height;
1919 hw->canvas_config[i][0].block_mode =
1920 hw->canvas_mode;
1921 hw->canvas_config[i][0].endian =
1922 (hw->canvas_mode == CANVAS_BLKMODE_LINEAR)?7:0;
1923
1924 canvas_config_config(canvas_y(canvas),
1925 &hw->canvas_config[i][0]);
1926
1927 hw->canvas_config[i][1].phy_addr =
1928 decbuf_start + decbuf_y_size;
1929 hw->canvas_config[i][1].width = canvas_width;
1930 hw->canvas_config[i][1].height = canvas_height / 2;
1931 hw->canvas_config[i][1].block_mode = hw->canvas_mode;
1932 hw->canvas_config[i][1].endian =
1933 (hw->canvas_mode == CANVAS_BLKMODE_LINEAR)?7:0;
1934
1935 canvas_config_config(canvas_u(canvas),
1936 &hw->canvas_config[i][1]);
1937 }
1938 }
1939 return;
1940}
1941
1942static void vmpeg2_dump_state(struct vdec_s *vdec)
1943{
1944 struct vdec_mpeg12_hw_s *hw =
1945 (struct vdec_mpeg12_hw_s *)(vdec->private);
1946 u32 i;
1947 debug_print(DECODE_ID(hw), 0,
1948 "====== %s\n", __func__);
1949 debug_print(DECODE_ID(hw), 0,
1950 "width/height (%d/%d),i_first %d\n",
1951 hw->frame_width,
1952 hw->frame_height,
1953 hw->first_i_frame_ready
1954 );
1955 debug_print(DECODE_ID(hw), 0,
1956 "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",
1957 vdec_frame_based(vdec),
1958 hw->eos,
1959 hw->stat,
1960 hw->dec_result,
1961 hw->dec_num,
1962 hw->put_num,
1963 hw->run_count,
1964 hw->not_run_ready,
1965 hw->input_empty
1966 );
1967
1968 for (i = 0; i < DECODE_BUFFER_NUM_MAX; i++) {
1969 debug_print(DECODE_ID(hw), 0,
1970 "index %d, used %d\n", i, hw->vfbuf_use[i]);
1971 }
1972
1973 if (vf_get_receiver(vdec->vf_provider_name)) {
1974 enum receviver_start_e state =
1975 vf_notify_receiver(vdec->vf_provider_name,
1976 VFRAME_EVENT_PROVIDER_QUREY_STATE,
1977 NULL);
1978 debug_print(DECODE_ID(hw), 0,
1979 "\nreceiver(%s) state %d\n",
1980 vdec->vf_provider_name,
1981 state);
1982 }
1983 debug_print(DECODE_ID(hw), 0,
1984 "%s, newq(%d/%d), dispq(%d/%d) vf peek/get/put (%d/%d/%d),drop=%d, buffer_not_ready %d\n",
1985 __func__,
1986 kfifo_len(&hw->newframe_q),
1987 VF_POOL_SIZE,
1988 kfifo_len(&hw->display_q),
1989 VF_POOL_SIZE,
1990 hw->peek_num,
1991 hw->get_num,
1992 hw->put_num,
1993 hw->drop_frame_count,
1994 hw->buffer_not_ready
1995 );
1996 debug_print(DECODE_ID(hw), 0,
1997 "VIFF_BIT_CNT=0x%x\n",
1998 READ_VREG(VIFF_BIT_CNT));
1999 debug_print(DECODE_ID(hw), 0,
2000 "VLD_MEM_VIFIFO_LEVEL=0x%x\n",
2001 READ_VREG(VLD_MEM_VIFIFO_LEVEL));
2002 debug_print(DECODE_ID(hw), 0,
2003 "VLD_MEM_VIFIFO_WP=0x%x\n",
2004 READ_VREG(VLD_MEM_VIFIFO_WP));
2005 debug_print(DECODE_ID(hw), 0,
2006 "VLD_MEM_VIFIFO_RP=0x%x\n",
2007 READ_VREG(VLD_MEM_VIFIFO_RP));
2008 debug_print(DECODE_ID(hw), 0,
2009 "PARSER_VIDEO_RP=0x%x\n",
2010 READ_PARSER_REG(PARSER_VIDEO_RP));
2011 debug_print(DECODE_ID(hw), 0,
2012 "PARSER_VIDEO_WP=0x%x\n",
2013 READ_PARSER_REG(PARSER_VIDEO_WP));
2014 if (vdec_frame_based(vdec) &&
2015 debug_enable & PRINT_FRAMEBASE_DATA
2016 ) {
2017 int jj;
2018 if (hw->chunk && hw->chunk->block &&
2019 hw->chunk->size > 0) {
2020 u8 *data = NULL;
2021
2022 if (!hw->chunk->block->is_mapped)
2023 data = codec_mm_vmap(hw->chunk->block->start +
2024 hw->chunk->offset, hw->chunk->size);
2025 else
2026 data = ((u8 *)hw->chunk->block->start_virt) +
2027 hw->chunk->offset;
2028
2029 debug_print(DECODE_ID(hw), 0,
2030 "frame data size 0x%x\n",
2031 hw->chunk->size);
2032 for (jj = 0; jj < hw->chunk->size; jj++) {
2033 if ((jj & 0xf) == 0)
2034 debug_print(DECODE_ID(hw),
2035 PRINT_FRAMEBASE_DATA,
2036 "%06x:", jj);
2037 debug_print(DECODE_ID(hw),
2038 PRINT_FRAMEBASE_DATA,
2039 "%02x ", data[jj]);
2040 if (((jj + 1) & 0xf) == 0)
2041 debug_print(DECODE_ID(hw),
2042 PRINT_FRAMEBASE_DATA, "\n");
2043 }
2044
2045 if (!hw->chunk->block->is_mapped)
2046 codec_mm_unmap_phyaddr(data);
2047 }
2048 }
2049}
2050
2051static void reset_process_time(struct vdec_mpeg12_hw_s *hw)
2052{
2053 if (hw->start_process_time) {
2054 unsigned process_time =
2055 1000 * (jiffies - hw->start_process_time) / HZ;
2056 hw->start_process_time = 0;
2057 if (process_time > max_process_time[DECODE_ID(hw)])
2058 max_process_time[DECODE_ID(hw)] = process_time;
2059 }
2060}
2061static void start_process_time(struct vdec_mpeg12_hw_s *hw)
2062{
2063 hw->decode_timeout_count = 10;
2064 hw->start_process_time = jiffies;
2065}
2066static void timeout_process(struct vdec_mpeg12_hw_s *hw)
2067{
2068 struct vdec_s *vdec = hw_to_vdec(hw);
2069
2070 reset_process_time(hw);
2071 amvdec_stop();
2072 debug_print(DECODE_ID(hw), PRINT_FLAG_ERROR,
2073 "%s decoder timeout, status=%d, level=%d\n",
2074 __func__, vdec->status, READ_VREG(VLD_MEM_VIFIFO_LEVEL));
2075 hw->dec_result = DEC_RESULT_DONE;
2076 hw->first_i_frame_ready = 0;
2077 vdec_schedule_work(&hw->work);
2078}
2079
2080static void check_timer_func(unsigned long arg)
2081{
2082 struct vdec_mpeg12_hw_s *hw = (struct vdec_mpeg12_hw_s *)arg;
2083 struct vdec_s *vdec = hw_to_vdec(hw);
2084 unsigned int timeout_val = decode_timeout_val;
2085
2086 if (radr != 0) {
2087 if (rval != 0) {
2088 WRITE_VREG(radr, rval);
2089 pr_info("WRITE_VREG(%x,%x)\n", radr, rval);
2090 } else
2091 pr_info("READ_VREG(%x)=%x\n", radr, READ_VREG(radr));
2092 rval = 0;
2093 radr = 0;
2094 }
2095
2096 if (((debug_enable & PRINT_FLAG_TIMEOUT_STATUS) == 0) &&
2097 (timeout_val > 0) &&
2098 (hw->start_process_time > 0) &&
2099 ((1000 * (jiffies - hw->start_process_time) / HZ)
2100 > timeout_val)) {
2101 if (hw->last_vld_level == READ_VREG(VLD_MEM_VIFIFO_LEVEL)) {
2102 if (hw->decode_timeout_count > 0)
2103 hw->decode_timeout_count--;
2104 if (hw->decode_timeout_count == 0)
2105 timeout_process(hw);
2106 }
2107 hw->last_vld_level = READ_VREG(VLD_MEM_VIFIFO_LEVEL);
2108 }
2109
2110 if (vdec->next_status == VDEC_STATUS_DISCONNECTED) {
2111 hw->dec_result = DEC_RESULT_FORCE_EXIT;
2112 vdec_schedule_work(&hw->work);
2113 pr_info("vdec requested to be disconnected\n");
2114 return;
2115 }
2116
2117 mod_timer(&hw->check_timer, jiffies + CHECK_INTERVAL);
2118}
2119
2120static int vmpeg12_hw_ctx_restore(struct vdec_mpeg12_hw_s *hw)
2121{
2122 u32 index, i;
2123 index = find_buffer(hw);
2124 if (index >= DECODE_BUFFER_NUM_MAX)
2125 return -1;
2126 if (!hw->init_flag)
2127 vmpeg12_canvas_init(hw);
2128 else {
2129 WRITE_VREG(MREG_CO_MV_START, hw->buf_start);
2130 for (i = 0; i < DECODE_BUFFER_NUM_MAX; i++) {
2131 canvas_config_config(canvas_y(hw->canvas_spec[i]),
2132 &hw->canvas_config[i][0]);
2133 canvas_config_config(canvas_u(hw->canvas_spec[i]),
2134 &hw->canvas_config[i][1]);
2135 }
2136 }
2137
2138 /* prepare REF0 & REF1
2139 points to the past two IP buffers
2140 prepare REC_CANVAS_ADDR and ANC2_CANVAS_ADDR
2141 points to the output buffer*/
2142 WRITE_VREG(MREG_REF0,
2143 (hw->refs[0] == -1) ? 0xffffffff :
2144 hw->canvas_spec[hw->refs[0]]);
2145 WRITE_VREG(MREG_REF1,
2146 (hw->refs[1] == -1) ? 0xffffffff :
2147 hw->canvas_spec[hw->refs[1]]);
2148 WRITE_VREG(REC_CANVAS_ADDR, hw->canvas_spec[index]);
2149 WRITE_VREG(ANC2_CANVAS_ADDR, hw->canvas_spec[index]);
2150
2151 debug_print(DECODE_ID(hw), PRINT_FLAG_RESTORE,
2152 "%s,ref0=0x%x, ref1=0x%x,rec=0x%x, ctx_valid=%d,index=%d\n",
2153 __func__,
2154 READ_VREG(MREG_REF0),
2155 READ_VREG(MREG_REF1),
2156 READ_VREG(REC_CANVAS_ADDR),
2157 hw->ctx_valid, index);
2158
2159 /* set to mpeg1 default */
2160 WRITE_VREG(MPEG1_2_REG,
2161 (hw->ctx_valid) ? hw->reg_mpeg1_2_reg : 0);
2162 /* disable PSCALE for hardware sharing */
2163 WRITE_VREG(PSCALE_CTRL, 0);
2164 /* for Mpeg1 default value */
2165 WRITE_VREG(PIC_HEAD_INFO,
2166 (hw->ctx_valid) ? hw->reg_pic_head_info : 0x380);
2167 /* disable mpeg4 */
2168 WRITE_VREG(M4_CONTROL_REG, 0);
2169 /* clear mailbox interrupt */
2170 WRITE_VREG(ASSIST_MBOX1_CLR_REG, 1);
2171 /* clear buffer IN/OUT registers */
2172 WRITE_VREG(MREG_BUFFEROUT, 0);
2173 /* enable mailbox interrupt */
2174 WRITE_VREG(ASSIST_MBOX1_MASK, 1);
2175 /* set reference width and height */
2176 if ((hw->frame_width != 0) && (hw->frame_height != 0))
2177 WRITE_VREG(MREG_CMD,
2178 (hw->frame_width << 16) | hw->frame_height);
2179 else
2180 WRITE_VREG(MREG_CMD, 0);
2181
2182 debug_print(DECODE_ID(hw), PRINT_FLAG_RESTORE,
2183 "0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n",
2184 hw->frame_width, hw->frame_height, hw->seqinfo,
2185 hw->reg_f_code_reg, hw->reg_slice_ver_pos_pic_type,
2186 hw->reg_mb_info);
2187
2188 WRITE_VREG(MREG_PIC_WIDTH, hw->reg_pic_width);
2189 WRITE_VREG(MREG_PIC_HEIGHT, hw->reg_pic_height);
2190 WRITE_VREG(MREG_SEQ_INFO, hw->seqinfo);
2191 WRITE_VREG(F_CODE_REG, hw->reg_f_code_reg);
2192 WRITE_VREG(SLICE_VER_POS_PIC_TYPE,
2193 hw->reg_slice_ver_pos_pic_type);
2194 WRITE_VREG(MB_INFO, hw->reg_mb_info);
2195 WRITE_VREG(VCOP_CTRL_REG, hw->reg_vcop_ctrl_reg);
2196 WRITE_VREG(AV_SCRATCH_H, hw->reg_signal_type);
2197
2198 /* clear error count */
2199 WRITE_VREG(MREG_ERROR_COUNT, 0);
2200 WRITE_VREG(MREG_FATAL_ERROR, 0);
2201 /* clear wait buffer status */
2202 WRITE_VREG(MREG_WAIT_BUFFER, 0);
2203#ifdef NV21
2204 SET_VREG_MASK(MDEC_PIC_DC_CTRL, 1<<17);
2205#endif
2206 if (!hw->ctx_valid)
2207 WRITE_VREG(AV_SCRATCH_J, hw->userdata_wp_ctx);
2208
2209 if (hw->chunk) {
2210 /*frame based input*/
2211 WRITE_VREG(MREG_INPUT,
2212 (hw->chunk->offset & 7) | (1<<7) | (hw->ctx_valid<<6));
2213 } else {
2214 /*stream based input*/
2215 WRITE_VREG(MREG_INPUT, (hw->ctx_valid<<6));
2216 }
2217 return 0;
2218}
2219
2220static void vmpeg12_local_init(struct vdec_mpeg12_hw_s *hw)
2221{
2222 int i;
2223 INIT_KFIFO(hw->display_q);
2224 INIT_KFIFO(hw->newframe_q);
2225
2226 for (i = 0; i < VF_POOL_SIZE; i++) {
2227 const struct vframe_s *vf;
2228 vf = &hw->vfpool[i];
2229 hw->vfpool[i].index = DECODE_BUFFER_NUM_MAX;
2230 kfifo_put(&hw->newframe_q, (const struct vframe_s *)vf);
2231 }
2232
2233 for (i = 0; i < DECODE_BUFFER_NUM_MAX; i++)
2234 hw->vfbuf_use[i] = 0;
2235
2236
2237 if (hw->mm_blk_handle) {
2238 decoder_bmmu_box_free(hw->mm_blk_handle);
2239 hw->mm_blk_handle = NULL;
2240 }
2241
2242 hw->mm_blk_handle = decoder_bmmu_box_alloc_box(
2243 DRIVER_NAME,
2244 0,
2245 MAX_BMMU_BUFFER_NUM,
2246 4 + PAGE_SHIFT,
2247 CODEC_MM_FLAGS_CMA_CLEAR |
2248 CODEC_MM_FLAGS_FOR_VDECODER |
2249 hw->tvp_flag);
2250 hw->eos = 0;
2251 hw->frame_width = hw->frame_height = 0;
2252 hw->frame_dur = hw->frame_prog = 0;
2253 hw->frame_force_skip_flag = 0;
2254 hw->wait_buffer_counter = 0;
2255 hw->first_i_frame_ready = 0;
2256 hw->dec_control &= DEC_CONTROL_INTERNAL_MASK;
2257 hw->refs[0] = -1;
2258 hw->refs[1] = -1;
2259 hw->disp_num = 0;
2260 hw->dec_num = 0;
2261 hw->put_num = 0;
2262 hw->run_count = 0;
2263 hw->not_run_ready = 0;
2264 hw->input_empty = 0;
2265 hw->peek_num = 0;
2266 hw->get_num = 0;
2267 hw->drop_frame_count = 0;
2268 hw->buffer_not_ready = 0;
2269 hw->start_process_time = 0;
2270 hw->init_flag = 0;
2271 hw->error_frame_skip_level = error_frame_skip_level;
2272 if (dec_control)
2273 hw->dec_control = dec_control;
2274}
2275
2276static s32 vmpeg12_init(struct vdec_mpeg12_hw_s *hw)
2277{
2278 int size;
2279 u32 fw_size = 16*0x1000;
2280 struct firmware_s *fw;
2281
2282 vmpeg12_local_init(hw);
2283
2284 fw = vmalloc(sizeof(struct firmware_s) + fw_size);
2285 if (IS_ERR_OR_NULL(fw))
2286 return -ENOMEM;
2287
2288 pr_debug("get firmware ...\n");
2289 size = get_firmware_data(VIDEO_DEC_MPEG12_MULTI, fw->data);
2290 if (size < 0) {
2291 pr_err("get firmware fail.\n");
2292 vfree(fw);
2293 return -1;
2294 }
2295
2296 fw->len = size;
2297 hw->fw = fw;
2298
2299 INIT_WORK(&hw->userdata_push_work, userdata_push_do_work);
2300 INIT_WORK(&hw->work, vmpeg12_work);
2301 INIT_WORK(&hw->notify_work, vmpeg12_notify_work);
2302
2303 if (NULL == hw->user_data_buffer) {
2304 hw->user_data_buffer = kmalloc(USER_DATA_SIZE,
2305 GFP_KERNEL);
2306 if (!hw->user_data_buffer) {
2307 pr_info("%s: Can not allocate user_data_buffer\n",
2308 __func__);
2309 return -1;
2310 }
2311 }
2312
2313 vmmpeg2_crate_userdata_manager(hw,
2314 hw->user_data_buffer,
2315 USER_DATA_SIZE);
2316
2317 amvdec_enable();
2318 init_timer(&hw->check_timer);
2319 hw->check_timer.data = (unsigned long)hw;
2320 hw->check_timer.function = check_timer_func;
2321 hw->check_timer.expires = jiffies + CHECK_INTERVAL;
2322
2323 hw->stat |= STAT_TIMER_ARM;
2324 hw->stat |= STAT_ISR_REG;
2325
2326 hw->buf_start = 0;
2327 WRITE_VREG(DECODE_STOP_POS, udebug_flag);
2328
2329 return 0;
2330}
2331
2332static unsigned long run_ready(struct vdec_s *vdec, unsigned long mask)
2333{
2334 int index;
2335
2336 struct vdec_mpeg12_hw_s *hw =
2337 (struct vdec_mpeg12_hw_s *)vdec->private;
2338 if (hw->eos)
2339 return 0;
2340 if (vdec_stream_based(vdec) && (hw->init_flag == 0)
2341 && pre_decode_buf_level != 0) {
2342 u32 rp, wp, level;
2343
2344 rp = READ_PARSER_REG(PARSER_VIDEO_RP);
2345 wp = READ_PARSER_REG(PARSER_VIDEO_WP);
2346 if (wp < rp)
2347 level = vdec->input.size + wp - rp;
2348 else
2349 level = wp - rp;
2350
2351 if (level < pre_decode_buf_level) {
2352 hw->not_run_ready++;
2353 return 0;
2354 }
2355 }
2356
2357#ifdef AGAIN_HAS_THRESHOLD
2358 if (hw->next_again_flag&&
2359 (!vdec_frame_based(vdec))) {
2360 u32 parser_wr_ptr =
2361 READ_PARSER_REG(PARSER_VIDEO_WP);
2362 if (parser_wr_ptr >= hw->pre_parser_wr_ptr &&
2363 (parser_wr_ptr - hw->pre_parser_wr_ptr) <
2364 again_threshold) {
2365 int r = vdec_sync_input(vdec);
2366 debug_print(DECODE_ID(hw), PRINT_FLAG_RUN_FLOW,
2367 "%s buf level%x\n",
2368 __func__, r);
2369 return 0;
2370 }
2371 }
2372#endif
2373
2374 index = find_buffer(hw);
2375 if (index >= DECODE_BUFFER_NUM_MAX) {
2376 hw->buffer_not_ready++;
2377 return 0;
2378 }
2379 hw->not_run_ready = 0;
2380 hw->buffer_not_ready = 0;
2381 if (vdec->parallel_dec == 1)
2382 return (unsigned long)(CORE_MASK_VDEC_1);
2383 else
2384 return (unsigned long)(CORE_MASK_VDEC_1 | CORE_MASK_HEVC);
2385}
2386
2387static unsigned char get_data_check_sum
2388 (struct vdec_mpeg12_hw_s *hw, int size)
2389{
2390 int jj;
2391 int sum = 0;
2392 u8 *data = NULL;
2393
2394 if (!hw->chunk->block->is_mapped)
2395 data = codec_mm_vmap(hw->chunk->block->start +
2396 hw->chunk->offset, size);
2397 else
2398 data = ((u8 *)hw->chunk->block->start_virt) +
2399 hw->chunk->offset;
2400
2401 for (jj = 0; jj < size; jj++)
2402 sum += data[jj];
2403
2404 if (!hw->chunk->block->is_mapped)
2405 codec_mm_unmap_phyaddr(data);
2406 return sum;
2407}
2408
2409static void run(struct vdec_s *vdec, unsigned long mask,
2410void (*callback)(struct vdec_s *, void *),
2411 void *arg)
2412{
2413 struct vdec_mpeg12_hw_s *hw =
2414 (struct vdec_mpeg12_hw_s *)vdec->private;
2415 int save_reg = READ_VREG(POWER_CTL_VLD);
2416 int size, ret;
2417 /* reset everything except DOS_TOP[1] and APB_CBUS[0]*/
2418 WRITE_VREG(DOS_SW_RESET0, 0xfffffff0);
2419 WRITE_VREG(DOS_SW_RESET0, 0);
2420 WRITE_VREG(POWER_CTL_VLD, save_reg);
2421 hw->run_count++;
2422 vdec_reset_core(vdec);
2423 hw->vdec_cb_arg = arg;
2424 hw->vdec_cb = callback;
2425
2426#ifdef AGAIN_HAS_THRESHOLD
2427 hw->pre_parser_wr_ptr =
2428 READ_PARSER_REG(PARSER_VIDEO_WP);
2429 hw->next_again_flag = 0;
2430#endif
2431
2432 size = vdec_prepare_input(vdec, &hw->chunk);
2433 if (size < 0) {
2434 hw->input_empty++;
2435 hw->dec_result = DEC_RESULT_AGAIN;
2436 vdec_schedule_work(&hw->work);
2437 return;
2438 }
2439 if (vdec_frame_based(vdec) && debug_enable) {
2440 u8 *data = NULL;
2441
2442
2443 if (hw->chunk)
2444 debug_print(DECODE_ID(hw), PRINT_FLAG_RUN_FLOW,
2445 "run: chunk offset 0x%x, size %d\n",
2446 hw->chunk->offset, hw->chunk->size);
2447
2448 if (!hw->chunk->block->is_mapped)
2449 data = codec_mm_vmap(hw->chunk->block->start +
2450 hw->chunk->offset, size);
2451 else
2452 data = ((u8 *)hw->chunk->block->start_virt) +
2453 hw->chunk->offset;
2454
2455 if (debug_enable & PRINT_FLAG_VDEC_STATUS
2456 ) {
2457 debug_print(DECODE_ID(hw), 0,
2458 "%s: size 0x%x sum 0x%x %02x %02x %02x %02x %02x %02x .. %02x %02x %02x %02x\n",
2459 __func__, size, get_data_check_sum(hw, size),
2460 data[0], data[1], data[2], data[3],
2461 data[4], data[5], data[size - 4],
2462 data[size - 3], data[size - 2],
2463 data[size - 1]);
2464 }
2465 if (debug_enable & PRINT_FRAMEBASE_DATA
2466 ) {
2467 int jj;
2468
2469 for (jj = 0; jj < size; jj++) {
2470 if ((jj & 0xf) == 0)
2471 debug_print(DECODE_ID(hw),
2472 PRINT_FRAMEBASE_DATA,
2473 "%06x:", jj);
2474 debug_print(DECODE_ID(hw),
2475 PRINT_FRAMEBASE_DATA,
2476 "%02x ", data[jj]);
2477 if (((jj + 1) & 0xf) == 0)
2478 debug_print(DECODE_ID(hw),
2479 PRINT_FRAMEBASE_DATA,
2480 "\n");
2481 }
2482 }
2483
2484 if (!hw->chunk->block->is_mapped)
2485 codec_mm_unmap_phyaddr(data);
2486 } else
2487 debug_print(DECODE_ID(hw), PRINT_FLAG_VDEC_STATUS,
2488 "%s: %x %x %x %x %x size 0x%x\n",
2489 __func__,
2490 READ_VREG(VLD_MEM_VIFIFO_LEVEL),
2491 READ_VREG(VLD_MEM_VIFIFO_WP),
2492 READ_VREG(VLD_MEM_VIFIFO_RP),
2493 READ_PARSER_REG(PARSER_VIDEO_RP),
2494 READ_PARSER_REG(PARSER_VIDEO_WP),
2495 size);
2496
2497
2498 hw->input_empty = 0;
2499 vdec_enable_input(vdec);
2500
2501 hw->dec_result = DEC_RESULT_NONE;
2502 if (vdec->mc_loaded) {
2503 /*firmware have load before,
2504 and not changes to another.
2505 ignore reload.
2506 */
2507 } else {
2508 ret = amvdec_vdec_loadmc_buf_ex(VFORMAT_MPEG12, "mmpeg12", vdec,
2509 hw->fw->data, hw->fw->len);
2510 if (ret < 0) {
2511 pr_err("[%d] %s: the %s fw loading failed, err: %x\n", vdec->id,
2512 hw->fw->name, tee_enabled() ? "TEE" : "local", ret);
2513 hw->dec_result = DEC_RESULT_FORCE_EXIT;
2514 vdec_schedule_work(&hw->work);
2515 return;
2516 }
2517 vdec->mc_loaded = 1;
2518 vdec->mc_type = VFORMAT_MPEG12;
2519 }
2520 if (vmpeg12_hw_ctx_restore(hw) < 0) {
2521 hw->dec_result = DEC_RESULT_ERROR;
2522 debug_print(DECODE_ID(hw), PRINT_FLAG_ERROR,
2523 "ammvdec_mpeg12: error HW context restore\n");
2524 vdec_schedule_work(&hw->work);
2525 return;
2526 }
2527 /*wmb();*/
2528 hw->stat |= STAT_MC_LOAD;
2529 hw->last_vld_level = 0;
2530 start_process_time(hw);
2531 amvdec_start();
2532 hw->stat |= STAT_VDEC_RUN;
2533 hw->init_flag = 1;
2534 mod_timer(&hw->check_timer, jiffies + CHECK_INTERVAL);
2535}
2536
2537static void reset(struct vdec_s *vdec)
2538{
2539 pr_info("ammvdec_mpeg12: reset.\n");
2540}
2541
2542static int ammvdec_mpeg12_probe(struct platform_device *pdev)
2543{
2544 struct vdec_s *pdata = *(struct vdec_s **)pdev->dev.platform_data;
2545 struct vdec_mpeg12_hw_s *hw = NULL;
2546
2547 pr_info("ammvdec_mpeg12 probe start.\n");
2548
2549 if (pdata == NULL) {
2550 pr_info("ammvdec_mpeg12 platform data undefined.\n");
2551 return -EFAULT;
2552 }
2553
2554 hw = vzalloc(sizeof(struct vdec_mpeg12_hw_s));
2555 if (hw == NULL) {
2556 pr_info("\nammvdec_mpeg12 decoder driver alloc failed\n");
2557 return -ENOMEM;
2558 }
2559
2560 pdata->private = hw;
2561 pdata->dec_status = vmmpeg12_dec_status;
2562 pdata->run_ready = run_ready;
2563 pdata->run = run;
2564 pdata->reset = reset;
2565 pdata->irq_handler = vmpeg12_isr;
2566 pdata->threaded_irq_handler = vmpeg12_isr_thread_fn;
2567 pdata->dump_state = vmpeg2_dump_state;
2568
2569 pdata->user_data_read = vmmpeg2_user_data_read;
2570 pdata->reset_userdata_fifo = vmmpeg2_reset_userdata_fifo;
2571 pdata->wakeup_userdata_poll = vmmpeg2_wakeup_userdata_poll;
2572
2573 if (pdata->use_vfm_path) {
2574 snprintf(pdata->vf_provider_name, VDEC_PROVIDER_NAME_SIZE,
2575 VFM_DEC_PROVIDER_NAME);
2576 hw->frameinfo_enable = 1;
2577 }
2578 else
2579 snprintf(pdata->vf_provider_name, VDEC_PROVIDER_NAME_SIZE,
2580 PROVIDER_NAME ".%02x", pdev->id & 0xff);
2581 if (pdata->parallel_dec == 1) {
2582 int i;
2583 for (i = 0; i < DECODE_BUFFER_NUM_MAX; i++)
2584 hw->canvas_spec[i] = 0xffffff;
2585 }
2586 vf_provider_init(&pdata->vframe_provider, pdata->vf_provider_name,
2587 &vf_provider_ops, pdata);
2588
2589 platform_set_drvdata(pdev, pdata);
2590 hw->canvas_mode = pdata->canvas_mode;
2591 hw->platform_dev = pdev;
2592
2593 hw->tvp_flag = vdec_secure(pdata) ? CODEC_MM_FLAGS_TVP : 0;
2594 if (pdata->sys_info)
2595 hw->vmpeg12_amstream_dec_info = *pdata->sys_info;
2596
2597 debug_print(DECODE_ID(hw), 0,
2598 "%s, sysinfo: %dx%d, tvp_flag = 0x%x\n",
2599 __func__,
2600 hw->vmpeg12_amstream_dec_info.width,
2601 hw->vmpeg12_amstream_dec_info.height,
2602 hw->tvp_flag);
2603
2604 if (vmpeg12_init(hw) < 0) {
2605 pr_info("ammvdec_mpeg12 init failed.\n");
2606 if (hw) {
2607 vfree(hw);
2608 hw = NULL;
2609 }
2610 pdata->dec_status = NULL;
2611 return -ENODEV;
2612 }
2613 if (pdata->parallel_dec == 1)
2614 vdec_core_request(pdata, CORE_MASK_VDEC_1);
2615 else {
2616 vdec_core_request(pdata, CORE_MASK_VDEC_1 | CORE_MASK_HEVC
2617 | CORE_MASK_COMBINE);
2618 }
2619#ifdef DUMP_USER_DATA
2620 amvdec_mmpeg12_init_userdata_dump(hw);
2621 reset_user_data_buf(hw);
2622#endif
2623
2624 /*INIT_WORK(&userdata_push_work, userdata_push_do_work);*/
2625 return 0;
2626}
2627
2628static int ammvdec_mpeg12_remove(struct platform_device *pdev)
2629
2630{
2631 struct vdec_mpeg12_hw_s *hw =
2632 (struct vdec_mpeg12_hw_s *)
2633 (((struct vdec_s *)(platform_get_drvdata(pdev)))->private);
2634 struct vdec_s *vdec = hw_to_vdec(hw);
2635 int i;
2636
2637 if (hw->stat & STAT_VDEC_RUN) {
2638 amvdec_stop();
2639 hw->stat &= ~STAT_VDEC_RUN;
2640 }
2641
2642 if (hw->stat & STAT_ISR_REG) {
2643 vdec_free_irq(VDEC_IRQ_1, (void *)hw);
2644 hw->stat &= ~STAT_ISR_REG;
2645 }
2646
2647 if (hw->stat & STAT_TIMER_ARM) {
2648 del_timer_sync(&hw->check_timer);
2649 hw->stat &= ~STAT_TIMER_ARM;
2650 }
2651 cancel_work_sync(&hw->userdata_push_work);
2652 cancel_work_sync(&hw->notify_work);
2653 cancel_work_sync(&hw->work);
2654
2655 if (hw->mm_blk_handle) {
2656 decoder_bmmu_box_free(hw->mm_blk_handle);
2657 hw->mm_blk_handle = NULL;
2658 }
2659 if (vdec->parallel_dec == 1)
2660 vdec_core_release(hw_to_vdec(hw), CORE_MASK_VDEC_1);
2661 else
2662 vdec_core_release(hw_to_vdec(hw), CORE_MASK_VDEC_1 | CORE_MASK_HEVC);
2663 vdec_set_status(hw_to_vdec(hw), VDEC_STATUS_DISCONNECTED);
2664
2665 if (vdec->parallel_dec == 1) {
2666 for (i = 0; i < DECODE_BUFFER_NUM_MAX; i++) {
2667 vdec->free_canvas_ex(canvas_y(hw->canvas_spec[i]), vdec->id);
2668 vdec->free_canvas_ex(canvas_u(hw->canvas_spec[i]), vdec->id);
2669 }
2670 }
2671
2672 if (hw->ccbuf_phyAddress_is_remaped_nocache)
2673 codec_mm_unmap_phyaddr(hw->ccbuf_phyAddress_virt);
2674 hw->ccbuf_phyAddress_virt = NULL;
2675 hw->ccbuf_phyAddress = 0;
2676 hw->ccbuf_phyAddress_is_remaped_nocache = 0;
2677
2678 if (hw->user_data_buffer != NULL) {
2679 kfree(hw->user_data_buffer);
2680 hw->user_data_buffer = NULL;
2681 }
2682 vmmpeg2_destroy_userdata_manager(hw);
2683
2684#ifdef DUMP_USER_DATA
2685 amvdec_mmpeg12_uninit_userdata_dump(hw);
2686#endif
2687
2688 if (hw->fw) {
2689 vfree(hw->fw);
2690 hw->fw = NULL;
2691 }
2692 if (hw) {
2693 vfree(hw);
2694 hw = NULL;
2695 }
2696 pr_info("ammvdec_mpeg12 removed.\n");
2697 memset(&gvs, 0x0, sizeof(gvs));
2698
2699 return 0;
2700}
2701
2702/****************************************/
2703#ifdef CONFIG_PM
2704static int mmpeg12_suspend(struct device *dev)
2705{
2706 amvdec_suspend(to_platform_device(dev), dev->power.power_state);
2707 return 0;
2708}
2709
2710static int mmpeg12_resume(struct device *dev)
2711{
2712 amvdec_resume(to_platform_device(dev));
2713 return 0;
2714}
2715
2716static const struct dev_pm_ops mmpeg12_pm_ops = {
2717 SET_SYSTEM_SLEEP_PM_OPS(mmpeg12_suspend, mmpeg12_resume)
2718};
2719#endif
2720
2721static struct platform_driver ammvdec_mpeg12_driver = {
2722 .probe = ammvdec_mpeg12_probe,
2723 .remove = ammvdec_mpeg12_remove,
2724 .driver = {
2725 .name = DRIVER_NAME,
2726#ifdef CONFIG_PM
2727 .pm = &mmpeg12_pm_ops,
2728#endif
2729 }
2730};
2731
2732static struct codec_profile_t ammvdec_mpeg12_profile = {
2733 .name = "mmpeg12",
2734 .profile = ""
2735};
2736
2737static struct mconfig mmpeg12_configs[] = {
2738 MC_PU32("stat", &stat),
2739 MC_PU32("radr", &radr),
2740 MC_PU32("rval", &rval),
2741 MC_PU32("dec_control", &dec_control),
2742 MC_PU32("error_frame_skip_level", &error_frame_skip_level),
2743 MC_PU32("decode_timeout_val", &decode_timeout_val),
2744};
2745static struct mconfig_node mmpeg12_node;
2746
2747static int __init ammvdec_mpeg12_driver_init_module(void)
2748{
2749 pr_info("ammvdec_mpeg12 module init\n");
2750
2751 if (platform_driver_register(&ammvdec_mpeg12_driver)) {
2752 pr_info("failed to register ammvdec_mpeg12 driver\n");
2753 return -ENODEV;
2754 }
2755 vcodec_profile_register(&ammvdec_mpeg12_profile);
2756 INIT_REG_NODE_CONFIGS("media.decoder", &mmpeg12_node,
2757 "mmpeg12", mmpeg12_configs, CONFIG_FOR_RW);
2758 return 0;
2759}
2760
2761static void __exit ammvdec_mpeg12_driver_remove_module(void)
2762{
2763 pr_info("ammvdec_mpeg12 module exit.\n");
2764 platform_driver_unregister(&ammvdec_mpeg12_driver);
2765}
2766
2767/****************************************/
2768module_param(dec_control, uint, 0664);
2769MODULE_PARM_DESC(dec_control, "\n ammvdec_mpeg12 decoder control\n");
2770module_param(error_frame_skip_level, uint, 0664);
2771MODULE_PARM_DESC(error_frame_skip_level,
2772 "\n ammvdec_mpeg12 error_frame_skip_level\n");
2773
2774module_param(radr, uint, 0664);
2775MODULE_PARM_DESC(radr, "\nradr\n");
2776
2777module_param(rval, uint, 0664);
2778MODULE_PARM_DESC(rval, "\nrval\n");
2779
2780module_param(debug_enable, uint, 0664);
2781MODULE_PARM_DESC(debug_enable,
2782 "\n ammvdec_mpeg12 debug enable\n");
2783module_param(pre_decode_buf_level, int, 0664);
2784MODULE_PARM_DESC(pre_decode_buf_level,
2785 "\n ammvdec_mpeg12 pre_decode_buf_level\n");
2786module_param(decode_timeout_val, uint, 0664);
2787MODULE_PARM_DESC(decode_timeout_val, "\n ammvdec_mpeg12 decode_timeout_val\n");
2788
2789module_param_array(max_process_time, uint, &max_decode_instance_num, 0664);
2790
2791module_param(udebug_flag, uint, 0664);
2792MODULE_PARM_DESC(udebug_flag, "\n ammvdec_mpeg12 udebug_flag\n");
2793
2794
2795#ifdef AGAIN_HAS_THRESHOLD
2796module_param(again_threshold, uint, 0664);
2797MODULE_PARM_DESC(again_threshold, "\n again_threshold\n");
2798#endif
2799
2800module_param(without_display_mode, uint, 0664);
2801MODULE_PARM_DESC(without_display_mode, "\n ammvdec_mpeg12 without_display_mode\n");
2802
2803
2804module_init(ammvdec_mpeg12_driver_init_module);
2805module_exit(ammvdec_mpeg12_driver_remove_module);
2806
2807MODULE_DESCRIPTION("AMLOGIC MULTI MPEG1/2 Video Decoder Driver");
2808MODULE_LICENSE("GPL");
2809
2810
2811