1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Coda multi-standard codec IP
5 * Copyright (C) 2012 Vista Silicon S.L.
6 * Javier Martin, <javier.martin@vista-silicon.com>
8 * Copyright (C) 2012-2014 Philipp Zabel, Pengutronix
14 #include <linux/debugfs.h>
15 #include <linux/idr.h>
16 #include <linux/irqreturn.h>
17 #include <linux/mutex.h>
18 #include <linux/kfifo.h>
19 #include <linux/videodev2.h>
20 #include <linux/ratelimit.h>
22 #include <media/v4l2-ctrls.h>
23 #include <media/v4l2-device.h>
24 #include <media/v4l2-fh.h>
25 #include <media/videobuf2-v4l2.h>
27 #include "coda_regs.h"
29 #define CODA_MAX_FRAMEBUFFERS 19
30 #define FMO_SLICE_SAVE_BUF_SIZE (32)
33 * This control allows applications to read the per-stream
34 * (i.e. per-context) Macroblocks Error Count. This value
37 #define V4L2_CID_CODA_MB_ERR_CNT (V4L2_CID_USER_CODA_BASE + 0)
56 struct coda_video_device
;
60 enum coda_product product
;
61 const struct coda_codec
*codecs
;
62 unsigned int num_codecs
;
63 const struct coda_video_device
**vdevs
;
64 unsigned int num_vdevs
;
74 struct debugfs_blob_wrapper blob
;
75 struct dentry
*dentry
;
79 struct v4l2_device v4l2_dev
;
80 struct video_device vfd
[6];
82 const struct coda_devtype
*devtype
;
84 struct vdoa_data
*vdoa
;
86 void __iomem
*regs_base
;
89 struct reset_control
*rstc
;
91 struct coda_aux_buf codebuf
;
92 struct coda_aux_buf tempbuf
;
93 struct coda_aux_buf workbuf
;
94 struct gen_pool
*iram_pool
;
95 struct coda_aux_buf iram
;
97 struct mutex dev_mutex
;
98 struct mutex coda_mutex
;
99 struct workqueue_struct
*workqueue
;
100 struct v4l2_m2m_dev
*m2m_dev
;
102 struct dentry
*debugfs_root
;
103 struct ratelimit_state mb_err_rs
;
114 struct coda_huff_tab
;
122 u8 h264_disable_deblocking_filter_idc
;
123 s8 h264_slice_alpha_c0_offset_div2
;
124 s8 h264_slice_beta_offset_div2
;
125 bool h264_constrained_intra_pred_flag
;
126 s8 h264_chroma_qp_index_offset
;
129 u8 mpeg2_profile_idc
;
135 enum v4l2_jpeg_chroma_subsampling jpeg_chroma_subsampling
;
137 u8 jpeg_restart_interval
;
138 u8
*jpeg_qmat_tab
[3];
139 int jpeg_qmat_index
[3];
140 int jpeg_huff_dc_index
[3];
141 int jpeg_huff_ac_index
[3];
143 struct coda_huff_tab
*jpeg_huff_tab
;
146 enum v4l2_mpeg_video_multi_slice_mode slice_mode
;
154 bool gop_size_changed
;
155 bool bitrate_changed
;
156 bool framerate_changed
;
157 bool h264_intra_qp_changed
;
158 bool intra_refresh_changed
;
159 bool slice_mode_changed
;
160 bool frame_rc_enable
;
164 struct coda_buffer_meta
{
165 struct list_head list
;
167 struct v4l2_timecode timecode
;
174 /* Per-queue, driver-specific private data */
178 unsigned int bytesperline
;
179 unsigned int sizeimage
;
181 struct v4l2_rect rect
;
184 struct coda_iram_info
{
186 phys_addr_t buf_bit_use
;
187 phys_addr_t buf_ip_ac_dc_use
;
188 phys_addr_t buf_dbk_y_use
;
189 phys_addr_t buf_dbk_c_use
;
190 phys_addr_t buf_ovl_use
;
191 phys_addr_t buf_btp_use
;
192 phys_addr_t search_ram_paddr
;
195 phys_addr_t next_paddr
;
198 #define GDI_LINEAR_FRAME_MAP 0
199 #define GDI_TILED_FRAME_MB_RASTER_MAP 1
203 struct coda_context_ops
{
204 int (*queue_init
)(void *priv
, struct vb2_queue
*src_vq
,
205 struct vb2_queue
*dst_vq
);
206 int (*reqbufs
)(struct coda_ctx
*ctx
, struct v4l2_requestbuffers
*rb
);
207 int (*start_streaming
)(struct coda_ctx
*ctx
);
208 int (*prepare_run
)(struct coda_ctx
*ctx
);
209 void (*finish_run
)(struct coda_ctx
*ctx
);
210 void (*run_timeout
)(struct coda_ctx
*ctx
);
211 void (*seq_init_work
)(struct work_struct
*work
);
212 void (*seq_end_work
)(struct work_struct
*work
);
213 void (*release
)(struct coda_ctx
*ctx
);
216 struct coda_internal_frame
{
217 struct coda_aux_buf buf
;
218 struct coda_buffer_meta meta
;
224 struct coda_dev
*dev
;
225 struct mutex buffer_mutex
;
226 struct work_struct pic_run_work
;
227 struct work_struct seq_init_work
;
228 struct work_struct seq_end_work
;
229 struct completion completion
;
230 const struct coda_video_device
*cvd
;
231 const struct coda_context_ops
*ops
;
239 struct coda_q_data q_data
[2];
240 enum coda_inst_type inst_type
;
241 const struct coda_codec
*codec
;
242 enum v4l2_colorspace colorspace
;
243 enum v4l2_xfer_func xfer_func
;
244 enum v4l2_ycbcr_encoding ycbcr_enc
;
245 enum v4l2_quantization quantization
;
246 struct coda_params params
;
247 struct v4l2_ctrl_handler ctrls
;
248 struct v4l2_ctrl
*h264_profile_ctrl
;
249 struct v4l2_ctrl
*h264_level_ctrl
;
250 struct v4l2_ctrl
*mpeg2_profile_ctrl
;
251 struct v4l2_ctrl
*mpeg2_level_ctrl
;
252 struct v4l2_ctrl
*mpeg4_profile_ctrl
;
253 struct v4l2_ctrl
*mpeg4_level_ctrl
;
254 struct v4l2_ctrl
*mb_err_cnt_ctrl
;
259 char vpu_header
[3][64];
260 int vpu_header_size
[3];
261 struct kfifo bitstream_fifo
;
262 struct mutex bitstream_mutex
;
263 struct coda_aux_buf bitstream
;
265 struct coda_aux_buf parabuf
;
266 struct coda_aux_buf psbuf
;
267 struct coda_aux_buf slicebuf
;
268 struct coda_internal_frame internal_frames
[CODA_MAX_FRAMEBUFFERS
];
269 struct list_head buffer_meta_list
;
270 spinlock_t buffer_meta_lock
;
272 unsigned int first_frame_sequence
;
273 struct coda_aux_buf workbuf
;
274 int num_internal_frames
;
277 struct coda_iram_info iram_info
;
279 u32 bit_stream_param
;
284 struct dentry
*debugfs_entry
;
287 struct vdoa_ctx
*vdoa
;
289 * wakeup mutex used to serialize encoder stop command and finish_run,
290 * ensures that finish_run always either flags the last returned buffer
291 * or wakes up the capture queue to signal EOS afterwards.
293 struct mutex wakeup_mutex
;
296 extern int coda_debug
;
298 #define coda_dbg(level, ctx, fmt, arg...) \
300 if (coda_debug >= (level)) \
301 v4l2_dbg((level), coda_debug, &(ctx)->dev->v4l2_dev, \
302 "%u: " fmt, (ctx)->idx, ##arg); \
305 void coda_write(struct coda_dev
*dev
, u32 data
, u32 reg
);
306 unsigned int coda_read(struct coda_dev
*dev
, u32 reg
);
307 void coda_write_base(struct coda_ctx
*ctx
, struct coda_q_data
*q_data
,
308 struct vb2_v4l2_buffer
*buf
, unsigned int reg_y
);
310 int coda_alloc_aux_buf(struct coda_dev
*dev
, struct coda_aux_buf
*buf
,
311 size_t size
, const char *name
, struct dentry
*parent
);
312 void coda_free_aux_buf(struct coda_dev
*dev
, struct coda_aux_buf
*buf
);
314 int coda_encoder_queue_init(void *priv
, struct vb2_queue
*src_vq
,
315 struct vb2_queue
*dst_vq
);
316 int coda_decoder_queue_init(void *priv
, struct vb2_queue
*src_vq
,
317 struct vb2_queue
*dst_vq
);
319 int coda_hw_reset(struct coda_ctx
*ctx
);
321 void coda_fill_bitstream(struct coda_ctx
*ctx
, struct list_head
*buffer_list
);
323 void coda_set_gdi_regs(struct coda_ctx
*ctx
);
325 static inline struct coda_q_data
*get_q_data(struct coda_ctx
*ctx
,
326 enum v4l2_buf_type type
)
329 case V4L2_BUF_TYPE_VIDEO_OUTPUT
:
330 return &(ctx
->q_data
[V4L2_M2M_SRC
]);
331 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
332 return &(ctx
->q_data
[V4L2_M2M_DST
]);
338 const char *coda_product_name(int product
);
340 int coda_check_firmware(struct coda_dev
*dev
);
342 static inline unsigned int coda_get_bitstream_payload(struct coda_ctx
*ctx
)
344 return kfifo_len(&ctx
->bitstream_fifo
);
348 * The bitstream prefetcher needs to read at least 2 256 byte periods past
349 * the desired bitstream position for all data to reach the decoder.
351 static inline bool coda_bitstream_can_fetch_past(struct coda_ctx
*ctx
,
354 return (int)(ctx
->bitstream_fifo
.kfifo
.in
- ALIGN(pos
, 256)) > 512;
357 bool coda_bitstream_can_fetch_past(struct coda_ctx
*ctx
, unsigned int pos
);
358 int coda_bitstream_flush(struct coda_ctx
*ctx
);
360 void coda_bit_stream_end_flag(struct coda_ctx
*ctx
);
362 void coda_m2m_buf_done(struct coda_ctx
*ctx
, struct vb2_v4l2_buffer
*buf
,
363 enum vb2_buffer_state state
);
365 int coda_h264_filler_nal(int size
, char *p
);
366 int coda_h264_padding(int size
, char *p
);
367 int coda_h264_profile(int profile_idc
);
368 int coda_h264_level(int level_idc
);
369 int coda_sps_parse_profile(struct coda_ctx
*ctx
, struct vb2_buffer
*vb
);
370 int coda_h264_sps_fixup(struct coda_ctx
*ctx
, int width
, int height
, char *buf
,
371 int *size
, int max_size
);
373 int coda_mpeg2_profile(int profile_idc
);
374 int coda_mpeg2_level(int level_idc
);
375 u32
coda_mpeg2_parse_headers(struct coda_ctx
*ctx
, u8
*buf
, u32 size
);
376 int coda_mpeg4_profile(int profile_idc
);
377 int coda_mpeg4_level(int level_idc
);
378 u32
coda_mpeg4_parse_headers(struct coda_ctx
*ctx
, u8
*buf
, u32 size
);
380 void coda_update_profile_level_ctrls(struct coda_ctx
*ctx
, u8 profile_idc
,
383 bool coda_jpeg_check_buffer(struct coda_ctx
*ctx
, struct vb2_buffer
*vb
);
384 int coda_jpeg_decode_header(struct coda_ctx
*ctx
, struct vb2_buffer
*vb
);
385 int coda_jpeg_write_tables(struct coda_ctx
*ctx
);
386 void coda_set_jpeg_compression_quality(struct coda_ctx
*ctx
, int quality
);
388 extern const struct coda_context_ops coda_bit_encode_ops
;
389 extern const struct coda_context_ops coda_bit_decode_ops
;
390 extern const struct coda_context_ops coda9_jpeg_encode_ops
;
391 extern const struct coda_context_ops coda9_jpeg_decode_ops
;
393 irqreturn_t
coda_irq_handler(int irq
, void *data
);
394 irqreturn_t
coda9_jpeg_irq_handler(int irq
, void *data
);
396 #endif /* __CODA_H__ */