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