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