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