1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#ifndef S5P_MFC_COMMON_H_
17#define S5P_MFC_COMMON_H_
18
19#include <linux/platform_device.h>
20#include <linux/videodev2.h>
21#include <media/v4l2-ctrls.h>
22#include <media/v4l2-device.h>
23#include <media/v4l2-ioctl.h>
24#include <media/videobuf2-core.h>
25#include "regs-mfc.h"
26#include "regs-mfc-v6.h"
27
28
29
30
31
32#define DST_QUEUE_OFF_BASE (TASK_SIZE / 2)
33
34#define MFC_BANK1_ALLOC_CTX 0
35#define MFC_BANK2_ALLOC_CTX 1
36
37#define MFC_BANK1_ALIGN_ORDER 13
38#define MFC_BANK2_ALIGN_ORDER 13
39#define MFC_BASE_ALIGN_ORDER 17
40
41#include <media/videobuf2-dma-contig.h>
42
43static inline dma_addr_t s5p_mfc_mem_cookie(void *a, void *b)
44{
45
46 dma_addr_t *paddr = vb2_dma_contig_memops.cookie(b);
47
48 return *paddr;
49}
50
51
52#define MFC_MAX_EXTRA_DPB 5
53#define MFC_MAX_BUFFERS 32
54#define MFC_NUM_CONTEXTS 4
55
56#define MFC_INT_TIMEOUT 2000
57
58#define MFC_BW_TIMEOUT 500
59
60#define MFC_WATCHDOG_INTERVAL 1000
61
62#define MFC_WATCHDOG_CNT 10
63#define MFC_NO_INSTANCE_SET -1
64#define MFC_ENC_CAP_PLANE_COUNT 1
65#define MFC_ENC_OUT_PLANE_COUNT 2
66#define STUFF_BYTE 4
67#define MFC_MAX_CTRLS 70
68
69#define S5P_MFC_CODEC_NONE -1
70#define S5P_MFC_CODEC_H264_DEC 0
71#define S5P_MFC_CODEC_H264_MVC_DEC 1
72#define S5P_MFC_CODEC_VC1_DEC 2
73#define S5P_MFC_CODEC_MPEG4_DEC 3
74#define S5P_MFC_CODEC_MPEG2_DEC 4
75#define S5P_MFC_CODEC_H263_DEC 5
76#define S5P_MFC_CODEC_VC1RCV_DEC 6
77#define S5P_MFC_CODEC_VP8_DEC 7
78
79#define S5P_MFC_CODEC_H264_ENC 20
80#define S5P_MFC_CODEC_H264_MVC_ENC 21
81#define S5P_MFC_CODEC_MPEG4_ENC 22
82#define S5P_MFC_CODEC_H263_ENC 23
83
84#define S5P_MFC_R2H_CMD_EMPTY 0
85#define S5P_MFC_R2H_CMD_SYS_INIT_RET 1
86#define S5P_MFC_R2H_CMD_OPEN_INSTANCE_RET 2
87#define S5P_MFC_R2H_CMD_SEQ_DONE_RET 3
88#define S5P_MFC_R2H_CMD_INIT_BUFFERS_RET 4
89#define S5P_MFC_R2H_CMD_CLOSE_INSTANCE_RET 6
90#define S5P_MFC_R2H_CMD_SLEEP_RET 7
91#define S5P_MFC_R2H_CMD_WAKEUP_RET 8
92#define S5P_MFC_R2H_CMD_COMPLETE_SEQ_RET 9
93#define S5P_MFC_R2H_CMD_DPB_FLUSH_RET 10
94#define S5P_MFC_R2H_CMD_NAL_ABORT_RET 11
95#define S5P_MFC_R2H_CMD_FW_STATUS_RET 12
96#define S5P_MFC_R2H_CMD_FRAME_DONE_RET 13
97#define S5P_MFC_R2H_CMD_FIELD_DONE_RET 14
98#define S5P_MFC_R2H_CMD_SLICE_DONE_RET 15
99#define S5P_MFC_R2H_CMD_ENC_BUFFER_FUL_RET 16
100#define S5P_MFC_R2H_CMD_ERR_RET 32
101
102#define mfc_read(dev, offset) readl(dev->regs_base + (offset))
103#define mfc_write(dev, data, offset) writel((data), dev->regs_base + \
104 (offset))
105
106
107
108
109enum s5p_mfc_fmt_type {
110 MFC_FMT_DEC,
111 MFC_FMT_ENC,
112 MFC_FMT_RAW,
113};
114
115
116
117
118enum s5p_mfc_node_type {
119 MFCNODE_INVALID = -1,
120 MFCNODE_DECODER = 0,
121 MFCNODE_ENCODER = 1,
122};
123
124
125
126
127enum s5p_mfc_inst_type {
128 MFCINST_INVALID,
129 MFCINST_DECODER,
130 MFCINST_ENCODER,
131};
132
133
134
135
136enum s5p_mfc_inst_state {
137 MFCINST_FREE = 0,
138 MFCINST_INIT = 100,
139 MFCINST_GOT_INST,
140 MFCINST_HEAD_PARSED,
141 MFCINST_HEAD_PRODUCED,
142 MFCINST_BUFS_SET,
143 MFCINST_RUNNING,
144 MFCINST_FINISHING,
145 MFCINST_FINISHED,
146 MFCINST_RETURN_INST,
147 MFCINST_ERROR,
148 MFCINST_ABORT,
149 MFCINST_FLUSH,
150 MFCINST_RES_CHANGE_INIT,
151 MFCINST_RES_CHANGE_FLUSH,
152 MFCINST_RES_CHANGE_END,
153};
154
155
156
157
158enum s5p_mfc_queue_state {
159 QUEUE_FREE,
160 QUEUE_BUFS_REQUESTED,
161 QUEUE_BUFS_QUERIED,
162 QUEUE_BUFS_MMAPED,
163};
164
165
166
167
168enum s5p_mfc_decode_arg {
169 MFC_DEC_FRAME,
170 MFC_DEC_LAST_FRAME,
171 MFC_DEC_RES_CHANGE,
172};
173
174#define MFC_BUF_FLAG_USED (1 << 0)
175#define MFC_BUF_FLAG_EOS (1 << 1)
176
177struct s5p_mfc_ctx;
178
179
180
181
182struct s5p_mfc_buf {
183 struct list_head list;
184 struct vb2_buffer *b;
185 union {
186 struct {
187 size_t luma;
188 size_t chroma;
189 } raw;
190 size_t stream;
191 } cookie;
192 int flags;
193};
194
195
196
197
198struct s5p_mfc_pm {
199 struct clk *clock;
200 struct clk *clock_gate;
201 atomic_t power;
202 struct device *device;
203};
204
205struct s5p_mfc_buf_size_v5 {
206 unsigned int h264_ctx;
207 unsigned int non_h264_ctx;
208 unsigned int dsc;
209 unsigned int shm;
210};
211
212struct s5p_mfc_buf_size_v6 {
213 unsigned int dev_ctx;
214 unsigned int h264_dec_ctx;
215 unsigned int other_dec_ctx;
216 unsigned int h264_enc_ctx;
217 unsigned int other_enc_ctx;
218};
219
220struct s5p_mfc_buf_size {
221 unsigned int fw;
222 unsigned int cpb;
223 void *priv;
224};
225
226struct s5p_mfc_buf_align {
227 unsigned int base;
228};
229
230struct s5p_mfc_variant {
231 unsigned int version;
232 unsigned int port_num;
233 struct s5p_mfc_buf_size *buf_size;
234 struct s5p_mfc_buf_align *buf_align;
235 char *fw_name;
236};
237
238
239
240
241
242
243
244
245
246
247
248struct s5p_mfc_priv_buf {
249 void *alloc;
250 unsigned long ofs;
251 void *virt;
252 dma_addr_t dma;
253 size_t size;
254};
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299struct s5p_mfc_dev {
300 struct v4l2_device v4l2_dev;
301 struct video_device *vfd_dec;
302 struct video_device *vfd_enc;
303 struct platform_device *plat_dev;
304 struct device *mem_dev_l;
305 struct device *mem_dev_r;
306 void __iomem *regs_base;
307 int irq;
308 struct v4l2_ctrl_handler dec_ctrl_handler;
309 struct v4l2_ctrl_handler enc_ctrl_handler;
310 struct s5p_mfc_pm pm;
311 struct s5p_mfc_variant *variant;
312 int num_inst;
313 spinlock_t irqlock;
314 spinlock_t condlock;
315
316 struct mutex mfc_mutex;
317 int int_cond;
318 int int_type;
319 unsigned int int_err;
320 wait_queue_head_t queue;
321 size_t fw_size;
322 void *fw_virt_addr;
323 dma_addr_t bank1;
324 dma_addr_t bank2;
325 unsigned long hw_lock;
326 struct s5p_mfc_ctx *ctx[MFC_NUM_CONTEXTS];
327 int curr_ctx;
328 unsigned long ctx_work_bits;
329 atomic_t watchdog_cnt;
330 struct timer_list watchdog_timer;
331 struct workqueue_struct *watchdog_workqueue;
332 struct work_struct watchdog_work;
333 void *alloc_ctx[2];
334 unsigned long enter_suspend;
335
336 struct s5p_mfc_priv_buf ctx_buf;
337 int warn_start;
338 struct s5p_mfc_hw_ops *mfc_ops;
339 struct s5p_mfc_hw_cmds *mfc_cmds;
340};
341
342
343
344
345struct s5p_mfc_h264_enc_params {
346 enum v4l2_mpeg_video_h264_profile profile;
347 enum v4l2_mpeg_video_h264_loop_filter_mode loop_filter_mode;
348 s8 loop_filter_alpha;
349 s8 loop_filter_beta;
350 enum v4l2_mpeg_video_h264_entropy_mode entropy_mode;
351 u8 max_ref_pic;
352 u8 num_ref_pic_4p;
353 int _8x8_transform;
354 int rc_mb_dark;
355 int rc_mb_smooth;
356 int rc_mb_static;
357 int rc_mb_activity;
358 int vui_sar;
359 u8 vui_sar_idc;
360 u16 vui_ext_sar_width;
361 u16 vui_ext_sar_height;
362 int open_gop;
363 u16 open_gop_size;
364 u8 rc_frame_qp;
365 u8 rc_min_qp;
366 u8 rc_max_qp;
367 u8 rc_p_frame_qp;
368 u8 rc_b_frame_qp;
369 enum v4l2_mpeg_video_h264_level level_v4l2;
370 int level;
371 u16 cpb_size;
372 int interlace;
373 u8 hier_qp;
374 u8 hier_qp_type;
375 u8 hier_qp_layer;
376 u8 hier_qp_layer_qp[7];
377 u8 sei_frame_packing;
378 u8 sei_fp_curr_frame_0;
379 u8 sei_fp_arrangement_type;
380
381 u8 fmo;
382 u8 fmo_map_type;
383 u8 fmo_slice_grp;
384 u8 fmo_chg_dir;
385 u32 fmo_chg_rate;
386 u32 fmo_run_len[4];
387 u8 aso;
388 u32 aso_slice_order[8];
389};
390
391
392
393
394struct s5p_mfc_mpeg4_enc_params {
395
396 enum v4l2_mpeg_video_mpeg4_profile profile;
397 int quarter_pixel;
398
399 u16 vop_time_res;
400 u16 vop_frm_delta;
401 u8 rc_frame_qp;
402 u8 rc_min_qp;
403 u8 rc_max_qp;
404 u8 rc_p_frame_qp;
405 u8 rc_b_frame_qp;
406 enum v4l2_mpeg_video_mpeg4_level level_v4l2;
407 int level;
408};
409
410
411
412
413struct s5p_mfc_enc_params {
414 u16 width;
415 u16 height;
416
417 u16 gop_size;
418 enum v4l2_mpeg_video_multi_slice_mode slice_mode;
419 u16 slice_mb;
420 u32 slice_bit;
421 u16 intra_refresh_mb;
422 int pad;
423 u8 pad_luma;
424 u8 pad_cb;
425 u8 pad_cr;
426 int rc_frame;
427 int rc_mb;
428 u32 rc_bitrate;
429 u16 rc_reaction_coeff;
430 u16 vbv_size;
431 u32 vbv_delay;
432
433 enum v4l2_mpeg_video_header_mode seq_hdr_mode;
434 enum v4l2_mpeg_mfc51_video_frame_skip_mode frame_skip_mode;
435 int fixed_target_bit;
436
437 u8 num_b_frame;
438 u32 rc_framerate_num;
439 u32 rc_framerate_denom;
440
441 struct {
442 struct s5p_mfc_h264_enc_params h264;
443 struct s5p_mfc_mpeg4_enc_params mpeg4;
444 } codec;
445
446};
447
448
449
450
451struct s5p_mfc_codec_ops {
452
453 int (*pre_seq_start) (struct s5p_mfc_ctx *ctx);
454 int (*post_seq_start) (struct s5p_mfc_ctx *ctx);
455
456 int (*pre_frame_start) (struct s5p_mfc_ctx *ctx);
457 int (*post_frame_start) (struct s5p_mfc_ctx *ctx);
458};
459
460#define call_cop(c, op, args...) \
461 (((c)->c_ops->op) ? \
462 ((c)->c_ops->op(args)) : 0)
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539struct s5p_mfc_ctx {
540 struct s5p_mfc_dev *dev;
541 struct v4l2_fh fh;
542
543 int num;
544
545 int int_cond;
546 int int_type;
547 unsigned int int_err;
548 wait_queue_head_t queue;
549
550 struct s5p_mfc_fmt *src_fmt;
551 struct s5p_mfc_fmt *dst_fmt;
552
553 struct vb2_queue vq_src;
554 struct vb2_queue vq_dst;
555
556 struct list_head src_queue;
557 struct list_head dst_queue;
558
559 unsigned int src_queue_cnt;
560 unsigned int dst_queue_cnt;
561
562 enum s5p_mfc_inst_type type;
563 enum s5p_mfc_inst_state state;
564 int inst_no;
565
566
567 int img_width;
568 int img_height;
569 int buf_width;
570 int buf_height;
571
572 int luma_size;
573 int chroma_size;
574 int mv_size;
575
576 unsigned long consumed_stream;
577
578 unsigned int dpb_flush_flag;
579 unsigned int head_processed;
580
581 struct s5p_mfc_priv_buf bank1;
582 struct s5p_mfc_priv_buf bank2;
583
584 enum s5p_mfc_queue_state capture_state;
585 enum s5p_mfc_queue_state output_state;
586
587 struct s5p_mfc_buf src_bufs[MFC_MAX_BUFFERS];
588 int src_bufs_cnt;
589 struct s5p_mfc_buf dst_bufs[MFC_MAX_BUFFERS];
590 int dst_bufs_cnt;
591
592 unsigned int sequence;
593 unsigned long dec_dst_flag;
594 size_t dec_src_buf_size;
595
596
597 int codec_mode;
598 int slice_interface;
599 int loop_filter_mpeg4;
600 int display_delay;
601 int display_delay_enable;
602 int after_packed_pb;
603 int sei_fp_parse;
604
605 int pb_count;
606 int total_dpb_count;
607 int mv_count;
608
609 struct s5p_mfc_priv_buf ctx;
610 struct s5p_mfc_priv_buf dsc;
611 struct s5p_mfc_priv_buf shm;
612
613 struct s5p_mfc_enc_params enc_params;
614
615 size_t enc_dst_buf_size;
616 size_t luma_dpb_size;
617 size_t chroma_dpb_size;
618 size_t me_buffer_size;
619 size_t tmv_buffer_size;
620
621 enum v4l2_mpeg_mfc51_video_force_frame_type force_frame_type;
622
623 struct list_head ref_queue;
624 unsigned int ref_queue_cnt;
625
626 enum v4l2_mpeg_video_multi_slice_mode slice_mode;
627 union {
628 unsigned int mb;
629 unsigned int bits;
630 } slice_size;
631
632 struct s5p_mfc_codec_ops *c_ops;
633
634 struct v4l2_ctrl *ctrls[MFC_MAX_CTRLS];
635 struct v4l2_ctrl_handler ctrl_handler;
636 unsigned int frame_tag;
637 size_t scratch_buf_size;
638};
639
640
641
642
643
644struct s5p_mfc_fmt {
645 char *name;
646 u32 fourcc;
647 u32 codec_mode;
648 enum s5p_mfc_fmt_type type;
649 u32 num_planes;
650};
651
652
653
654
655
656struct mfc_control {
657 __u32 id;
658 enum v4l2_ctrl_type type;
659 __u8 name[32];
660 __s32 minimum;
661 __s32 maximum;
662 __s32 step;
663 __u32 menu_skip_mask;
664 __s32 default_value;
665 __u32 flags;
666 __u32 reserved[2];
667 __u8 is_volatile;
668};
669
670
671#define s5p_mfc_hw_call(f, op, args...) \
672 ((f && f->op) ? f->op(args) : -ENODEV)
673
674#define fh_to_ctx(__fh) container_of(__fh, struct s5p_mfc_ctx, fh)
675#define ctrl_to_ctx(__ctrl) \
676 container_of((__ctrl)->handler, struct s5p_mfc_ctx, ctrl_handler)
677
678void clear_work_bit(struct s5p_mfc_ctx *ctx);
679void set_work_bit(struct s5p_mfc_ctx *ctx);
680void clear_work_bit_irqsave(struct s5p_mfc_ctx *ctx);
681void set_work_bit_irqsave(struct s5p_mfc_ctx *ctx);
682
683#define HAS_PORTNUM(dev) (dev ? (dev->variant ? \
684 (dev->variant->port_num ? 1 : 0) : 0) : 0)
685#define IS_TWOPORT(dev) (dev->variant->port_num == 2 ? 1 : 0)
686#define IS_MFCV6(dev) (dev->variant->version >= 0x60 ? 1 : 0)
687
688#endif
689