2 * Samsung S5P Multi Format Codec v 5.0
4 * This file contains definitions of enums and structs used by the codec
7 * Copyright (C) 2011 Samsung Electronics Co., Ltd.
8 * Kamil Debski, <k.debski@samsung.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the
13 * License, or (at your option) any later version
16 #ifndef S5P_MFC_COMMON_H_
17 #define S5P_MFC_COMMON_H_
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>
26 #include "regs-mfc-v6.h"
28 /* Definitions related to MFC memory */
30 /* Offset base used to differentiate between CAPTURE and OUTPUT
32 #define DST_QUEUE_OFF_BASE (TASK_SIZE / 2)
34 #define MFC_BANK1_ALLOC_CTX 0
35 #define MFC_BANK2_ALLOC_CTX 1
37 #define MFC_BANK1_ALIGN_ORDER 13
38 #define MFC_BANK2_ALIGN_ORDER 13
39 #define MFC_BASE_ALIGN_ORDER 17
41 #include <media/videobuf2-dma-contig.h>
43 static inline dma_addr_t s5p_mfc_mem_cookie(void *a, void *b)
45 /* Same functionality as the vb2_dma_contig_plane_paddr */
46 dma_addr_t *paddr = vb2_dma_contig_memops.cookie(b);
52 #define MFC_MAX_EXTRA_DPB 5
53 #define MFC_MAX_BUFFERS 32
54 #define MFC_NUM_CONTEXTS 4
55 /* Interrupt timeout */
56 #define MFC_INT_TIMEOUT 2000
57 /* Busy wait timeout */
58 #define MFC_BW_TIMEOUT 500
59 /* Watchdog interval */
60 #define MFC_WATCHDOG_INTERVAL 1000
61 /* After how many executions watchdog should assume lock up */
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
67 #define MFC_MAX_CTRLS 70
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
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
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
102 #define mfc_read(dev, offset) readl(dev->regs_base + (offset))
103 #define mfc_write(dev, data, offset) writel((data), dev->regs_base + \
107 * enum s5p_mfc_fmt_type - type of the pixelformat
109 enum s5p_mfc_fmt_type {
116 * enum s5p_mfc_node_type - The type of an MFC device node.
118 enum s5p_mfc_node_type {
119 MFCNODE_INVALID = -1,
125 * enum s5p_mfc_inst_type - The type of an MFC instance.
127 enum s5p_mfc_inst_type {
134 * enum s5p_mfc_inst_state - The state of an MFC instance.
136 enum s5p_mfc_inst_state {
141 MFCINST_HEAD_PRODUCED,
150 MFCINST_RES_CHANGE_INIT,
151 MFCINST_RES_CHANGE_FLUSH,
152 MFCINST_RES_CHANGE_END,
156 * enum s5p_mfc_queue_state - The state of buffer queue.
158 enum s5p_mfc_queue_state {
160 QUEUE_BUFS_REQUESTED,
166 * enum s5p_mfc_decode_arg - type of frame decoding
168 enum s5p_mfc_decode_arg {
174 #define MFC_BUF_FLAG_USED (1 << 0)
175 #define MFC_BUF_FLAG_EOS (1 << 1)
180 * struct s5p_mfc_buf - MFC buffer
183 struct list_head list;
184 struct vb2_buffer *b;
196 * struct s5p_mfc_pm - power management data structure
200 struct clk *clock_gate;
202 struct device *device;
205 struct s5p_mfc_buf_size_v5 {
206 unsigned int h264_ctx;
207 unsigned int non_h264_ctx;
212 struct 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;
220 struct s5p_mfc_buf_size {
226 struct s5p_mfc_buf_align {
230 struct 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;
239 * struct s5p_mfc_priv_buf - represents internal used buffer
240 * @alloc: allocation-specific context for each buffer
241 * (videobuf2 allocator)
242 * @ofs: offset of each buffer, will be used for MFC
243 * @virt: kernel virtual address, only valid when the
244 * buffer accessed by driver
245 * @dma: DMA address, only valid when kernel DMA API used
246 * @size: size of the buffer
248 struct s5p_mfc_priv_buf {
257 * struct s5p_mfc_dev - The struct containing driver internal parameters.
259 * @v4l2_dev: v4l2_device
260 * @vfd_dec: video device for decoding
261 * @vfd_enc: video device for encoding
262 * @plat_dev: platform device
263 * @mem_dev_l: child device of the left memory bank (0)
264 * @mem_dev_r: child device of the right memory bank (1)
265 * @regs_base: base address of the MFC hw registers
267 * @dec_ctrl_handler: control framework handler for decoding
268 * @enc_ctrl_handler: control framework handler for encoding
269 * @pm: power management control
270 * @variant: MFC hardware variant information
271 * @num_inst: couter of active MFC instances
272 * @irqlock: lock for operations on videobuf2 queues
273 * @condlock: lock for changing/checking if a context is ready to be
275 * @mfc_mutex: lock for video_device
276 * @int_cond: variable used by the waitqueue
277 * @int_type: type of last interrupt
278 * @int_err: error number for last interrupt
279 * @queue: waitqueue for waiting for completion of device commands
280 * @fw_size: size of firmware
281 * @fw_virt_addr: virtual firmware address
282 * @bank1: address of the beginning of bank 1 memory
283 * @bank2: address of the beginning of bank 2 memory
284 * @hw_lock: used for hardware locking
285 * @ctx: array of driver contexts
286 * @curr_ctx: number of the currently running context
287 * @ctx_work_bits: used to mark which contexts are waiting for hardware
288 * @watchdog_cnt: counter for the watchdog
289 * @watchdog_workqueue: workqueue for the watchdog
290 * @watchdog_work: worker for the watchdog
291 * @alloc_ctx: videobuf2 allocator contexts for two memory banks
292 * @enter_suspend: flag set when entering suspend
293 * @ctx_buf: common context memory (MFCv6)
294 * @warn_start: hardware error code from which warnings start
295 * @mfc_ops: ops structure holding HW operation function pointers
296 * @mfc_cmds: cmd structure holding HW commands function pointers
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;
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;
313 spinlock_t irqlock; /* lock when operating on videobuf2 queues */
314 spinlock_t condlock; /* lock when changing/checking if a context is
315 ready to be processed */
316 struct mutex mfc_mutex; /* video_device lock */
319 unsigned int int_err;
320 wait_queue_head_t queue;
325 unsigned long hw_lock;
326 struct s5p_mfc_ctx *ctx[MFC_NUM_CONTEXTS];
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;
334 unsigned long enter_suspend;
336 struct s5p_mfc_priv_buf ctx_buf;
338 struct s5p_mfc_hw_ops *mfc_ops;
339 struct s5p_mfc_hw_cmds *mfc_cmds;
343 * struct s5p_mfc_h264_enc_params - encoding parameters for h264
345 struct 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;
350 enum v4l2_mpeg_video_h264_entropy_mode entropy_mode;
360 u16 vui_ext_sar_width;
361 u16 vui_ext_sar_height;
369 enum v4l2_mpeg_video_h264_level level_v4l2;
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;
388 u32 aso_slice_order[8];
392 * struct s5p_mfc_mpeg4_enc_params - encoding parameters for h263 and mpeg4
394 struct s5p_mfc_mpeg4_enc_params {
396 enum v4l2_mpeg_video_mpeg4_profile profile;
398 /* Common for MPEG4, H263 */
406 enum v4l2_mpeg_video_mpeg4_level level_v4l2;
411 * struct s5p_mfc_enc_params - general encoding parameters
413 struct s5p_mfc_enc_params {
418 enum v4l2_mpeg_video_multi_slice_mode slice_mode;
421 u16 intra_refresh_mb;
429 u16 rc_reaction_coeff;
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;
438 u32 rc_framerate_num;
439 u32 rc_framerate_denom;
442 struct s5p_mfc_h264_enc_params h264;
443 struct s5p_mfc_mpeg4_enc_params mpeg4;
449 * struct s5p_mfc_codec_ops - codec ops, used by encoding
451 struct s5p_mfc_codec_ops {
452 /* initialization routines */
453 int (*pre_seq_start) (struct s5p_mfc_ctx *ctx);
454 int (*post_seq_start) (struct s5p_mfc_ctx *ctx);
455 /* execution routines */
456 int (*pre_frame_start) (struct s5p_mfc_ctx *ctx);
457 int (*post_frame_start) (struct s5p_mfc_ctx *ctx);
460 #define call_cop(c, op, args...) \
461 (((c)->c_ops->op) ? \
462 ((c)->c_ops->op(args)) : 0)
465 * struct s5p_mfc_ctx - This struct contains the instance context
467 * @dev: pointer to the s5p_mfc_dev of the device
468 * @fh: struct v4l2_fh
469 * @num: number of the context that this structure describes
470 * @int_cond: variable used by the waitqueue
471 * @int_type: type of the last interrupt
472 * @int_err: error number received from MFC hw in the interrupt
473 * @queue: waitqueue that can be used to wait for this context to
475 * @src_fmt: source pixelformat information
476 * @dst_fmt: destination pixelformat information
477 * @vq_src: vb2 queue for source buffers
478 * @vq_dst: vb2 queue for destination buffers
479 * @src_queue: driver internal queue for source buffers
480 * @dst_queue: driver internal queue for destination buffers
481 * @src_queue_cnt: number of buffers queued on the source internal queue
482 * @dst_queue_cnt: number of buffers queued on the dest internal queue
483 * @type: type of the instance - decoder or encoder
484 * @state: state of the context
485 * @inst_no: number of hw instance associated with the context
486 * @img_width: width of the image that is decoded or encoded
487 * @img_height: height of the image that is decoded or encoded
488 * @buf_width: width of the buffer for processed image
489 * @buf_height: height of the buffer for processed image
490 * @luma_size: size of a luma plane
491 * @chroma_size: size of a chroma plane
492 * @mv_size: size of a motion vectors buffer
493 * @consumed_stream: number of bytes that have been used so far from the
495 * @dpb_flush_flag: flag used to indicate that a DPB buffers are being
497 * @head_processed: flag mentioning whether the header data is processed
499 * @bank1: handle to memory allocated for temporary buffers from
501 * @bank2: handle to memory allocated for temporary buffers from
503 * @capture_state: state of the capture buffers queue
504 * @output_state: state of the output buffers queue
505 * @src_bufs: information on allocated source buffers
506 * @dst_bufs: information on allocated destination buffers
507 * @sequence: counter for the sequence number for v4l2
508 * @dec_dst_flag: flags for buffers queued in the hardware
509 * @dec_src_buf_size: size of the buffer for source buffers in decoding
510 * @codec_mode: number of codec mode used by MFC hw
511 * @slice_interface: slice interface flag
512 * @loop_filter_mpeg4: loop filter for MPEG4 flag
513 * @display_delay: value of the display delay for H264
514 * @display_delay_enable: display delay for H264 enable flag
515 * @after_packed_pb: flag used to track buffer when stream is in
517 * @sei_fp_parse: enable/disable parsing of frame packing SEI information
518 * @dpb_count: count of the DPB buffers required by MFC hw
519 * @total_dpb_count: count of DPB buffers with additional buffers
520 * requested by the application
521 * @ctx: context buffer information
522 * @dsc: descriptor buffer information
523 * @shm: shared memory buffer information
524 * @mv_count: number of MV buffers allocated for decoding
525 * @enc_params: encoding parameters for MFC
526 * @enc_dst_buf_size: size of the buffers for encoder output
527 * @luma_dpb_size: dpb buffer size for luma
528 * @chroma_dpb_size: dpb buffer size for chroma
529 * @me_buffer_size: size of the motion estimation buffer
530 * @tmv_buffer_size: size of temporal predictor motion vector buffer
531 * @frame_type: used to force the type of the next encoded frame
532 * @ref_queue: list of the reference buffers for encoding
533 * @ref_queue_cnt: number of the buffers in the reference list
534 * @c_ops: ops for encoding
535 * @ctrls: array of controls, used when adding controls to the
536 * v4l2 control framework
537 * @ctrl_handler: handler for v4l2 framework
540 struct s5p_mfc_dev *dev;
547 unsigned int int_err;
548 wait_queue_head_t queue;
550 struct s5p_mfc_fmt *src_fmt;
551 struct s5p_mfc_fmt *dst_fmt;
553 struct vb2_queue vq_src;
554 struct vb2_queue vq_dst;
556 struct list_head src_queue;
557 struct list_head dst_queue;
559 unsigned int src_queue_cnt;
560 unsigned int dst_queue_cnt;
562 enum s5p_mfc_inst_type type;
563 enum s5p_mfc_inst_state state;
566 /* Image parameters */
576 unsigned long consumed_stream;
578 unsigned int dpb_flush_flag;
579 unsigned int head_processed;
581 struct s5p_mfc_priv_buf bank1;
582 struct s5p_mfc_priv_buf bank2;
584 enum s5p_mfc_queue_state capture_state;
585 enum s5p_mfc_queue_state output_state;
587 struct s5p_mfc_buf src_bufs[MFC_MAX_BUFFERS];
589 struct s5p_mfc_buf dst_bufs[MFC_MAX_BUFFERS];
592 unsigned int sequence;
593 unsigned long dec_dst_flag;
594 size_t dec_src_buf_size;
599 int loop_filter_mpeg4;
601 int display_delay_enable;
609 struct s5p_mfc_priv_buf ctx;
610 struct s5p_mfc_priv_buf dsc;
611 struct s5p_mfc_priv_buf shm;
613 struct s5p_mfc_enc_params enc_params;
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;
621 enum v4l2_mpeg_mfc51_video_force_frame_type force_frame_type;
623 struct list_head ref_queue;
624 unsigned int ref_queue_cnt;
626 enum v4l2_mpeg_video_multi_slice_mode slice_mode;
632 struct s5p_mfc_codec_ops *c_ops;
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;
641 * struct s5p_mfc_fmt - structure used to store information about pixelformats
648 enum s5p_mfc_fmt_type type;
653 * struct mfc_control - structure used to store information about MFC controls
654 * it is used to initialize the control framework.
658 enum v4l2_ctrl_type type;
659 __u8 name[32]; /* Whatever */
660 __s32 minimum; /* Note signedness */
663 __u32 menu_skip_mask;
670 /* Macro for making hardware specific calls */
671 #define s5p_mfc_hw_call(f, op, args...) \
672 ((f && f->op) ? f->op(args) : -ENODEV)
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)
678 void clear_work_bit(struct s5p_mfc_ctx *ctx);
679 void set_work_bit(struct s5p_mfc_ctx *ctx);
680 void clear_work_bit_irqsave(struct s5p_mfc_ctx *ctx);
681 void set_work_bit_irqsave(struct s5p_mfc_ctx *ctx);
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)
688 #endif /* S5P_MFC_COMMON_H_ */