[media] s5p-mfc: added support for end of stream handling in MFC encoder
[linux-2.6-block.git] / drivers / media / platform / s5p-mfc / s5p_mfc_common.h
CommitLineData
af935746
KD
1/*
2 * Samsung S5P Multi Format Codec v 5.0
3 *
4 * This file contains definitions of enums and structs used by the codec
5 * driver.
6 *
7 * Copyright (C) 2011 Samsung Electronics Co., Ltd.
8 * Kamil Debski, <k.debski@samsung.com>
9 *
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
14 */
15
16#ifndef S5P_MFC_COMMON_H_
17#define S5P_MFC_COMMON_H_
18
19#include "regs-mfc.h"
20#include <linux/platform_device.h>
21#include <linux/videodev2.h>
22#include <media/v4l2-ctrls.h>
23#include <media/v4l2-device.h>
24#include <media/v4l2-ioctl.h>
25#include <media/videobuf2-core.h>
26
27/* Definitions related to MFC memory */
28
29/* Offset base used to differentiate between CAPTURE and OUTPUT
30* while mmaping */
31#define DST_QUEUE_OFF_BASE (TASK_SIZE / 2)
32
33/* Offset used by the hardware to store addresses */
34#define MFC_OFFSET_SHIFT 11
35
36#define FIRMWARE_ALIGN 0x20000 /* 128KB */
37#define MFC_H264_CTX_BUF_SIZE 0x96000 /* 600KB per H264 instance */
38#define MFC_CTX_BUF_SIZE 0x2800 /* 10KB per instance */
39#define DESC_BUF_SIZE 0x20000 /* 128KB for DESC buffer */
40#define SHARED_BUF_SIZE 0x2000 /* 8KB for shared buffer */
41
42#define DEF_CPB_SIZE 0x40000 /* 512KB */
43
44#define MFC_BANK1_ALLOC_CTX 0
45#define MFC_BANK2_ALLOC_CTX 1
46
47#define MFC_BANK1_ALIGN_ORDER 13
48#define MFC_BANK2_ALIGN_ORDER 13
49#define MFC_BASE_ALIGN_ORDER 17
50
51#include <media/videobuf2-dma-contig.h>
52
53static inline dma_addr_t s5p_mfc_mem_cookie(void *a, void *b)
54{
55 /* Same functionality as the vb2_dma_contig_plane_paddr */
56 dma_addr_t *paddr = vb2_dma_contig_memops.cookie(b);
57
58 return *paddr;
59}
60
61/* MFC definitions */
62#define MFC_MAX_EXTRA_DPB 5
63#define MFC_MAX_BUFFERS 32
64#define MFC_NUM_CONTEXTS 4
65/* Interrupt timeout */
66#define MFC_INT_TIMEOUT 2000
67/* Busy wait timeout */
68#define MFC_BW_TIMEOUT 500
69/* Watchdog interval */
70#define MFC_WATCHDOG_INTERVAL 1000
71/* After how many executions watchdog should assume lock up */
72#define MFC_WATCHDOG_CNT 10
73#define MFC_NO_INSTANCE_SET -1
74#define MFC_ENC_CAP_PLANE_COUNT 1
75#define MFC_ENC_OUT_PLANE_COUNT 2
76#define STUFF_BYTE 4
77#define MFC_MAX_CTRLS 64
78
79#define mfc_read(dev, offset) readl(dev->regs_base + (offset))
80#define mfc_write(dev, data, offset) writel((data), dev->regs_base + \
81 (offset))
82
83/**
84 * enum s5p_mfc_fmt_type - type of the pixelformat
85 */
86enum s5p_mfc_fmt_type {
87 MFC_FMT_DEC,
88 MFC_FMT_ENC,
89 MFC_FMT_RAW,
90};
91
92/**
93 * enum s5p_mfc_node_type - The type of an MFC device node.
94 */
95enum s5p_mfc_node_type {
96 MFCNODE_INVALID = -1,
97 MFCNODE_DECODER = 0,
98 MFCNODE_ENCODER = 1,
99};
100
101/**
102 * enum s5p_mfc_inst_type - The type of an MFC instance.
103 */
104enum s5p_mfc_inst_type {
105 MFCINST_INVALID,
106 MFCINST_DECODER,
107 MFCINST_ENCODER,
108};
109
110/**
111 * enum s5p_mfc_inst_state - The state of an MFC instance.
112 */
113enum s5p_mfc_inst_state {
114 MFCINST_FREE = 0,
115 MFCINST_INIT = 100,
116 MFCINST_GOT_INST,
117 MFCINST_HEAD_PARSED,
118 MFCINST_BUFS_SET,
119 MFCINST_RUNNING,
120 MFCINST_FINISHING,
121 MFCINST_FINISHED,
122 MFCINST_RETURN_INST,
123 MFCINST_ERROR,
124 MFCINST_ABORT,
125 MFCINST_RES_CHANGE_INIT,
126 MFCINST_RES_CHANGE_FLUSH,
127 MFCINST_RES_CHANGE_END,
128};
129
130/**
131 * enum s5p_mfc_queue_state - The state of buffer queue.
132 */
133enum s5p_mfc_queue_state {
134 QUEUE_FREE,
135 QUEUE_BUFS_REQUESTED,
136 QUEUE_BUFS_QUERIED,
137 QUEUE_BUFS_MMAPED,
138};
139
140/**
141 * enum s5p_mfc_decode_arg - type of frame decoding
142 */
143enum s5p_mfc_decode_arg {
144 MFC_DEC_FRAME,
145 MFC_DEC_LAST_FRAME,
146 MFC_DEC_RES_CHANGE,
147};
148
f9f715a9
AH
149#define MFC_BUF_FLAG_USED (1 << 0)
150#define MFC_BUF_FLAG_EOS (1 << 1)
151
af935746
KD
152struct s5p_mfc_ctx;
153
154/**
155 * struct s5p_mfc_buf - MFC buffer
156 */
157struct s5p_mfc_buf {
158 struct list_head list;
159 struct vb2_buffer *b;
160 union {
161 struct {
162 size_t luma;
163 size_t chroma;
164 } raw;
165 size_t stream;
166 } cookie;
f9f715a9 167 int flags;
af935746
KD
168};
169
170/**
171 * struct s5p_mfc_pm - power management data structure
172 */
173struct s5p_mfc_pm {
174 struct clk *clock;
175 struct clk *clock_gate;
176 atomic_t power;
177 struct device *device;
178};
179
180/**
181 * struct s5p_mfc_dev - The struct containing driver internal parameters.
182 *
183 * @v4l2_dev: v4l2_device
184 * @vfd_dec: video device for decoding
185 * @vfd_enc: video device for encoding
186 * @plat_dev: platform device
187 * @mem_dev_l: child device of the left memory bank (0)
188 * @mem_dev_r: child device of the right memory bank (1)
189 * @regs_base: base address of the MFC hw registers
190 * @irq: irq resource
af935746
KD
191 * @dec_ctrl_handler: control framework handler for decoding
192 * @enc_ctrl_handler: control framework handler for encoding
193 * @pm: power management control
194 * @num_inst: couter of active MFC instances
195 * @irqlock: lock for operations on videobuf2 queues
196 * @condlock: lock for changing/checking if a context is ready to be
197 * processed
198 * @mfc_mutex: lock for video_device
199 * @int_cond: variable used by the waitqueue
200 * @int_type: type of last interrupt
201 * @int_err: error number for last interrupt
202 * @queue: waitqueue for waiting for completion of device commands
203 * @fw_size: size of firmware
204 * @bank1: address of the beggining of bank 1 memory
205 * @bank2: address of the beggining of bank 2 memory
206 * @hw_lock: used for hardware locking
207 * @ctx: array of driver contexts
208 * @curr_ctx: number of the currently running context
209 * @ctx_work_bits: used to mark which contexts are waiting for hardware
210 * @watchdog_cnt: counter for the watchdog
211 * @watchdog_workqueue: workqueue for the watchdog
212 * @watchdog_work: worker for the watchdog
213 * @alloc_ctx: videobuf2 allocator contexts for two memory banks
214 * @enter_suspend: flag set when entering suspend
215 *
216 */
217struct s5p_mfc_dev {
218 struct v4l2_device v4l2_dev;
219 struct video_device *vfd_dec;
220 struct video_device *vfd_enc;
221 struct platform_device *plat_dev;
222 struct device *mem_dev_l;
223 struct device *mem_dev_r;
224 void __iomem *regs_base;
225 int irq;
af935746
KD
226 struct v4l2_ctrl_handler dec_ctrl_handler;
227 struct v4l2_ctrl_handler enc_ctrl_handler;
228 struct s5p_mfc_pm pm;
229 int num_inst;
230 spinlock_t irqlock; /* lock when operating on videobuf2 queues */
231 spinlock_t condlock; /* lock when changing/checking if a context is
232 ready to be processed */
233 struct mutex mfc_mutex; /* video_device lock */
234 int int_cond;
235 int int_type;
236 unsigned int int_err;
237 wait_queue_head_t queue;
238 size_t fw_size;
239 size_t bank1;
240 size_t bank2;
241 unsigned long hw_lock;
242 struct s5p_mfc_ctx *ctx[MFC_NUM_CONTEXTS];
243 int curr_ctx;
244 unsigned long ctx_work_bits;
245 atomic_t watchdog_cnt;
246 struct timer_list watchdog_timer;
247 struct workqueue_struct *watchdog_workqueue;
248 struct work_struct watchdog_work;
249 void *alloc_ctx[2];
250 unsigned long enter_suspend;
251};
252
253/**
254 * struct s5p_mfc_h264_enc_params - encoding parameters for h264
255 */
256struct s5p_mfc_h264_enc_params {
257 enum v4l2_mpeg_video_h264_profile profile;
258 enum v4l2_mpeg_video_h264_loop_filter_mode loop_filter_mode;
259 s8 loop_filter_alpha;
260 s8 loop_filter_beta;
261 enum v4l2_mpeg_video_h264_entropy_mode entropy_mode;
262 u8 max_ref_pic;
263 u8 num_ref_pic_4p;
264 int _8x8_transform;
265 int rc_mb;
266 int rc_mb_dark;
267 int rc_mb_smooth;
268 int rc_mb_static;
269 int rc_mb_activity;
270 int vui_sar;
271 u8 vui_sar_idc;
272 u16 vui_ext_sar_width;
273 u16 vui_ext_sar_height;
274 int open_gop;
275 u16 open_gop_size;
276 u8 rc_frame_qp;
277 u8 rc_min_qp;
278 u8 rc_max_qp;
279 u8 rc_p_frame_qp;
280 u8 rc_b_frame_qp;
281 enum v4l2_mpeg_video_h264_level level_v4l2;
282 int level;
283 u16 cpb_size;
284};
285
286/**
287 * struct s5p_mfc_mpeg4_enc_params - encoding parameters for h263 and mpeg4
288 */
289struct s5p_mfc_mpeg4_enc_params {
290 /* MPEG4 Only */
291 enum v4l2_mpeg_video_mpeg4_profile profile;
292 int quarter_pixel;
293 /* Common for MPEG4, H263 */
294 u16 vop_time_res;
295 u16 vop_frm_delta;
296 u8 rc_frame_qp;
297 u8 rc_min_qp;
298 u8 rc_max_qp;
299 u8 rc_p_frame_qp;
300 u8 rc_b_frame_qp;
301 enum v4l2_mpeg_video_mpeg4_level level_v4l2;
302 int level;
303};
304
305/**
306 * struct s5p_mfc_enc_params - general encoding parameters
307 */
308struct s5p_mfc_enc_params {
309 u16 width;
310 u16 height;
311
312 u16 gop_size;
313 enum v4l2_mpeg_video_multi_slice_mode slice_mode;
314 u16 slice_mb;
315 u32 slice_bit;
316 u16 intra_refresh_mb;
317 int pad;
318 u8 pad_luma;
319 u8 pad_cb;
320 u8 pad_cr;
321 int rc_frame;
322 u32 rc_bitrate;
323 u16 rc_reaction_coeff;
324 u16 vbv_size;
325
326 enum v4l2_mpeg_video_header_mode seq_hdr_mode;
327 enum v4l2_mpeg_mfc51_video_frame_skip_mode frame_skip_mode;
328 int fixed_target_bit;
329
330 u8 num_b_frame;
331 u32 rc_framerate_num;
332 u32 rc_framerate_denom;
333 int interlace;
334
335 union {
336 struct s5p_mfc_h264_enc_params h264;
337 struct s5p_mfc_mpeg4_enc_params mpeg4;
338 } codec;
339
340};
341
342/**
343 * struct s5p_mfc_codec_ops - codec ops, used by encoding
344 */
345struct s5p_mfc_codec_ops {
346 /* initialization routines */
347 int (*pre_seq_start) (struct s5p_mfc_ctx *ctx);
348 int (*post_seq_start) (struct s5p_mfc_ctx *ctx);
349 /* execution routines */
350 int (*pre_frame_start) (struct s5p_mfc_ctx *ctx);
351 int (*post_frame_start) (struct s5p_mfc_ctx *ctx);
352};
353
354#define call_cop(c, op, args...) \
355 (((c)->c_ops->op) ? \
356 ((c)->c_ops->op(args)) : 0)
357
358/**
359 * struct s5p_mfc_ctx - This struct contains the instance context
360 *
361 * @dev: pointer to the s5p_mfc_dev of the device
362 * @fh: struct v4l2_fh
363 * @num: number of the context that this structure describes
364 * @int_cond: variable used by the waitqueue
365 * @int_type: type of the last interrupt
366 * @int_err: error number received from MFC hw in the interrupt
367 * @queue: waitqueue that can be used to wait for this context to
368 * finish
369 * @src_fmt: source pixelformat information
370 * @dst_fmt: destination pixelformat information
371 * @vq_src: vb2 queue for source buffers
372 * @vq_dst: vb2 queue for destination buffers
373 * @src_queue: driver internal queue for source buffers
374 * @dst_queue: driver internal queue for destination buffers
375 * @src_queue_cnt: number of buffers queued on the source internal queue
376 * @dst_queue_cnt: number of buffers queued on the dest internal queue
377 * @type: type of the instance - decoder or encoder
378 * @state: state of the context
379 * @inst_no: number of hw instance associated with the context
380 * @img_width: width of the image that is decoded or encoded
381 * @img_height: height of the image that is decoded or encoded
382 * @buf_width: width of the buffer for processed image
383 * @buf_height: height of the buffer for processed image
384 * @luma_size: size of a luma plane
385 * @chroma_size: size of a chroma plane
386 * @mv_size: size of a motion vectors buffer
387 * @consumed_stream: number of bytes that have been used so far from the
388 * decoding buffer
389 * @dpb_flush_flag: flag used to indicate that a DPB buffers are being
390 * flushed
391 * @bank1_buf: handle to memory allocated for temporary buffers from
392 * memory bank 1
393 * @bank1_phys: address of the temporary buffers from memory bank 1
394 * @bank1_size: size of the memory allocated for temporary buffers from
395 * memory bank 1
396 * @bank2_buf: handle to memory allocated for temporary buffers from
397 * memory bank 2
398 * @bank2_phys: address of the temporary buffers from memory bank 2
399 * @bank2_size: size of the memory allocated for temporary buffers from
400 * memory bank 2
401 * @capture_state: state of the capture buffers queue
402 * @output_state: state of the output buffers queue
403 * @src_bufs: information on allocated source buffers
404 * @dst_bufs: information on allocated destination buffers
405 * @sequence: counter for the sequence number for v4l2
406 * @dec_dst_flag: flags for buffers queued in the hardware
407 * @dec_src_buf_size: size of the buffer for source buffers in decoding
408 * @codec_mode: number of codec mode used by MFC hw
409 * @slice_interface: slice interface flag
410 * @loop_filter_mpeg4: loop filter for MPEG4 flag
411 * @display_delay: value of the display delay for H264
412 * @display_delay_enable: display delay for H264 enable flag
413 * @after_packed_pb: flag used to track buffer when stream is in
414 * Packed PB format
415 * @dpb_count: count of the DPB buffers required by MFC hw
416 * @total_dpb_count: count of DPB buffers with additional buffers
417 * requested by the application
418 * @ctx_buf: handle to the memory associated with this context
419 * @ctx_phys: address of the memory associated with this context
420 * @ctx_size: size of the memory associated with this context
421 * @desc_buf: description buffer for decoding handle
422 * @desc_phys: description buffer for decoding address
423 * @shm_alloc: handle for the shared memory buffer
424 * @shm: virtual address for the shared memory buffer
425 * @shm_ofs: address offset for shared memory
426 * @enc_params: encoding parameters for MFC
427 * @enc_dst_buf_size: size of the buffers for encoder output
428 * @frame_type: used to force the type of the next encoded frame
429 * @ref_queue: list of the reference buffers for encoding
430 * @ref_queue_cnt: number of the buffers in the reference list
431 * @c_ops: ops for encoding
432 * @ctrls: array of controls, used when adding controls to the
433 * v4l2 control framework
434 * @ctrl_handler: handler for v4l2 framework
435 */
436struct s5p_mfc_ctx {
437 struct s5p_mfc_dev *dev;
438 struct v4l2_fh fh;
439
440 int num;
441
442 int int_cond;
443 int int_type;
444 unsigned int int_err;
445 wait_queue_head_t queue;
446
447 struct s5p_mfc_fmt *src_fmt;
448 struct s5p_mfc_fmt *dst_fmt;
449
450 struct vb2_queue vq_src;
451 struct vb2_queue vq_dst;
452
453 struct list_head src_queue;
454 struct list_head dst_queue;
455
456 unsigned int src_queue_cnt;
457 unsigned int dst_queue_cnt;
458
459 enum s5p_mfc_inst_type type;
460 enum s5p_mfc_inst_state state;
461 int inst_no;
462
463 /* Image parameters */
464 int img_width;
465 int img_height;
466 int buf_width;
467 int buf_height;
468
469 int luma_size;
470 int chroma_size;
471 int mv_size;
472
473 unsigned long consumed_stream;
474
475 unsigned int dpb_flush_flag;
476
477 /* Buffers */
478 void *bank1_buf;
479 size_t bank1_phys;
480 size_t bank1_size;
481
482 void *bank2_buf;
483 size_t bank2_phys;
484 size_t bank2_size;
485
486 enum s5p_mfc_queue_state capture_state;
487 enum s5p_mfc_queue_state output_state;
488
489 struct s5p_mfc_buf src_bufs[MFC_MAX_BUFFERS];
490 int src_bufs_cnt;
491 struct s5p_mfc_buf dst_bufs[MFC_MAX_BUFFERS];
492 int dst_bufs_cnt;
493
494 unsigned int sequence;
495 unsigned long dec_dst_flag;
496 size_t dec_src_buf_size;
497
498 /* Control values */
499 int codec_mode;
500 int slice_interface;
501 int loop_filter_mpeg4;
502 int display_delay;
503 int display_delay_enable;
504 int after_packed_pb;
505
506 int dpb_count;
507 int total_dpb_count;
508
509 /* Buffers */
510 void *ctx_buf;
511 size_t ctx_phys;
512 size_t ctx_ofs;
513 size_t ctx_size;
514
515 void *desc_buf;
516 size_t desc_phys;
517
518
519 void *shm_alloc;
520 void *shm;
521 size_t shm_ofs;
522
523 struct s5p_mfc_enc_params enc_params;
524
525 size_t enc_dst_buf_size;
526
527 enum v4l2_mpeg_mfc51_video_force_frame_type force_frame_type;
528
529 struct list_head ref_queue;
530 unsigned int ref_queue_cnt;
531
532 struct s5p_mfc_codec_ops *c_ops;
533
534 struct v4l2_ctrl *ctrls[MFC_MAX_CTRLS];
535 struct v4l2_ctrl_handler ctrl_handler;
536};
537
538/*
539 * struct s5p_mfc_fmt - structure used to store information about pixelformats
540 * used by the MFC
541 */
542struct s5p_mfc_fmt {
543 char *name;
544 u32 fourcc;
545 u32 codec_mode;
546 enum s5p_mfc_fmt_type type;
547 u32 num_planes;
548};
549
550/**
551 * struct mfc_control - structure used to store information about MFC controls
552 * it is used to initialize the control framework.
553 */
554struct mfc_control {
555 __u32 id;
556 enum v4l2_ctrl_type type;
557 __u8 name[32]; /* Whatever */
558 __s32 minimum; /* Note signedness */
559 __s32 maximum;
560 __s32 step;
561 __u32 menu_skip_mask;
562 __s32 default_value;
563 __u32 flags;
564 __u32 reserved[2];
565 __u8 is_volatile;
566};
567
568
569#define fh_to_ctx(__fh) container_of(__fh, struct s5p_mfc_ctx, fh)
570#define ctrl_to_ctx(__ctrl) \
571 container_of((__ctrl)->handler, struct s5p_mfc_ctx, ctrl_handler)
572
573#endif /* S5P_MFC_COMMON_H_ */